Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement multiple scenarios login by using policy mode in spring

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how spring uses strategy mode to achieve login in multiple scenarios. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

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

If you simply use @ Autowired, you will encounter a situation where there are multiple implementations of an interface in the spring IOC container, and spring cannot recognize the specific implementation class. If it is not a policy pattern, we can specify @ Qualifier and @ primary to avoid bean conflicts. But not in strategic mode.

In addition to this basic functionality, @ Autowired has more powerful capabilities, such as injecting arrays of specified types, List/Set collections, and even Map objects.

Add a number for each specific implementation class to facilitate identification, which can be selected according to the scene, here is just a simulation.

Log in to service

@ Servicepublic class LoginService {@ Autowired Set loginSet;// uses Set Map 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

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

Specific implementation class-user password login

@ Componentpublic 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-mailbox login

@ Componentpublic 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-mailbox login

@ Componentpublic 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 simulated login to SQL

@ Mapper@Repositorypublic 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) } this is the end of sharing how spring uses policy mode to implement login methods for multiple scenarios. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report