Spring uses strategy mode to implement multiple scenario login methods (strategy mode)

Spring uses strategy mode to implement multiple scenario login methods

The @Autowired annotation can help us automatically inject the beans we want.

If you simply use @Autowired, you will encounter multiple implementations of an interface in the spring IOC container. Spring cannot identify the specific implementation class. If it is not a strategy mode, we can specifically specify @Qualifier and @primary to avoid bean conflicts. Case. But this is not possible in strategy mode.

In addition to this basic function, @Autowired also has more powerful functions. It can also inject arrays of specified types, List/Set collections, and even Map objects.

Simulate login examples in various scenarios (suitable for any strategy mode: multiple payment methods, etc.)

A number is added to each specific implementation class for easy identification. The specific implementation class can be selected according to the scenario. This is just a simulation.

Login service

@Service
public class LoginService {

    @Autowired
    Set<Login> loginSet;//使用了Set 

    Map<Integer,Login> loginMap;

    public User login(User userLogin) {
        Login login=loginMap.get(userLogin.getChannelNo());
        return login.login(userLogin);
    }

    @PostConstruct
    public void init() {
        loginMap = new HashMap<>();
        for (Login login : loginSet) {
            loginMap.put(login.channel(), login);
        }

    }
}

Source code policy interface

@Component
public interface Login {
    User login(User userLogin);
    Integer channel();
}

Specific implementation class—user password login

@Component
public class PasswordLogin implements Login {
    @Autowired
    LoginDao loginDao;

    @Override
    public User login(User userLogin) {

        return loginDao.PasswordLogin(userLogin.getUsername(),userLogin.getPassword());
    }

    @Override
    public Integer channel() {
        return 2;
    }
}

Specific implementation class—email login


@Component
public class EmailLogin implements Login {
    @Autowired
    LoginDao loginDao;

    @Override
    public User login(User userLogin) {
        return loginDao.EmailLogin(userLogin.getEmail(),userLogin.getPassword());
    }

    @Override
    public Integer channel() {
        return 3;
    }
}

Specific implementation class—email login

@Component
public class PhoneLogin implements Login {
    @Autowired
    LoginDao loginDao;

    @Override
    public User login(User userLogin) {
        return loginDao.PhoneLogin(userLogin.getPhone(),userLogin.getPassword());
    }

    @Override
    public Integer channel() {
        return 1;
    }
}

Simple simulation of login SQL

@Mapper
@Repository
public interface LoginDao {

    @Select("select * from user where phone=#{phone} and password=#{password}")
    User PhoneLogin(String phone,String password);

    @Select("select * from user where username=#{username} and password=#{password}")
    User PasswordLogin(String username,String password);

    @Select("select * from user where email=#{email} and password=#{password}")
    User EmailLogin(String email,String password);

}

Guess you like

Origin blog.csdn.net/qq_44961149/article/details/112360220