Use annotations to construct IoC containers
Use annotations to register beans with Spring containers. Need to register in applicationContext.xml <context:component-scan base-package=”pagkage1[,pagkage2,…,pagkageN]”/>.
For example: specify a package in base-package
<context:component-scan base-package="cn.gacl.java"/>
Indicates that if a class has a specific annotation [@Component/@Repository/@Service/@Controller] in the cn.gacl.java package and its subpackage, this object will be registered into the Spring container as a bean. You can also specify multiple packages in <context:component-scan base-package=""/>, such as:
<context:component-scan base-package="cn.gacl.dao.impl,cn.gacl.service.impl,cn.gacl.action"/>
Multiple packages are separated by commas.
1. @Component
@Component
It is a common form for all Spring managed components. The @Component annotation can be placed on the head of the class, and @Component is not recommended.
2. @Controller
@Controller corresponds to the bean of the presentation layer, that is, Action, for example:
@Controller @Scope("prototype") public class UserAction extends BaseAction<User>{ ... }After using @Controller annotation to identify UserAction, it means that the UserAction is to be handed over to the Spring container for management. There will be an action named "userAction" in the Spring container. This name is taken based on the UserAction class name. Note: If @Controller does not specify its value【@Controller】, the default bean name is lowercase in the first letter of the class name. If you specify value【@Controller(value="UserAction")】 or 【@Controller("UserAction")】, then use value as the name of the bean.
The UserAction here also uses the @Scope annotation. @Scope("prototype") means that the scope of the Action is declared as a prototype. You can use the scope="prototype" of the container to ensure that each request has a separate Action to handle it, avoiding the thread safety issues of Action in struts. spring The default scope is singleton mode (scope="singleton"), which will only create an Action object. Each access is the same Action object. The data is not safe. Struts2 requires that each access correspond to a different Action. scope="prototype" can ensure that an Action object is created when there is a request.
3. @ Service
@Service corresponds to the service layer bean, for example:
@Service("userService") public class UserServiceImpl implements UserService { …… }@Service("userService") annotation tells Spring that when Spring wants to create an instance of UserServiceImpl, the bean name must be called "userService". In this way, when the Action needs to use an instance of UserServiceImpl, the "userService" created by Spring can be injected into Action: In Action, you only need to declare a variable named "userService" to receive the "userService" injected by Spring. The specific code is as follows:
// Inject userService @Resource(name = "userService") private UserService userService;
Note: The type of the "userService" variable declared in the Action must be "UserServiceImpl" or its parent class "UserService", otherwise it cannot be injected due to inconsistent types. Because the declared "userService" variable in the Action uses the @Resource annotation and indicates its name = "userService", which is equivalent to telling Spring that I want to instantiate an "userService". Spring will help me instantiate it quickly, and then give it to me. When Spring sees the @Resource annotation on the userService variable, according to the specified name attribute, you can know that an instance of UserServiceImpl needs to be used in the Action. At this time, Spring will inject the instance of UserServiceImpl called "userService" into the "userService" variable in the Action to help the Action complete the instantiation of userService, so in the Action there is no need to use "UserService userService = new UserServiceImpl();" This is the most primitive way to instantiate the userService.
If there is no Spring, when Action needs to use UserServiceImpl, you must actively create an instance object through "UserService userService = new UserServiceImpl();". However, after using Spring, when Action wants to use UserServiceImpl, you do not have to actively create an instance of UserServiceImpl. Creating a UserServiceImpl instance has been handed over to Spring. Spring gives the created UserServiceImpl instance to the Action, and you can use it directly after you get the Action.
Action can be used immediately after actively creating the UserServiceImpl instance, but becomes passively waiting for Spring to create the UserServiceImpl instance before injecting it into Action.
This shows that Action's "control" over the "UserServiceImpl" class has been "reversed". It turns out that the initiative is in my own hands. I have to use the "UserServiceImpl" class instance. I can take the initiative to use it immediately. But now I cannot take the initiative to new instances of the "UserServiceImpl" class instance. The power of the new "UserServiceImpl" class instance has been taken away by Spring. Only Spring can new instances of the "UserServiceImpl" class instance, and Action can only wait for Spring to create "UserSe" class After the instance of rviceImpl" class, "please" Spring give it the created instance of the "UserServiceImpl" class so that it can use "UserServiceImpl". This is the core idea of Spring "control inversion", also called "dependence injection". "Dependence injection" is also easy to understand. Action needs to use UserServiceImpl to work, so it creates a dependency on UserServiceImpl. Spring injects (that is, "giving") the UserServiceImpl that the Acion needs to depend on to Action, which is called "dependence injection". For Action, if the Action depends on something, it will ask Spring to inject it. For Spring, what Action needs, Spring will take the initiative to inject it.
4. @ Repository
@Repository corresponds to the data access layer bean, for example:
@Repository(value="userDao")public class UserDaoImpl extends BaseDaoImpl<User> {………}The @Repository(value="userDao") annotation tells Spring to let Spring create a UserDaoImpl instance named "userDao".
When the Service needs to use the UserDaoImpl instance named "userDao" created by Spring, you can use the @Resource(name = "userDao") annotation to tell Spring, and Spring can inject the created userDao into the Service.
// Inject userDao, and when taking out the specified user from the database according to the user ID, you need to use @Resource(name = "userDao") private BaseDao<User> userDao;
@Resource, @AutoWired, and @Qualifier are all used to inject objects. Among them, @Resource can be injected in name or type, @AutoWired can only be injected in type, and @Qualifier can only be injected in name.
But they have some subtle differences:
1. @Resource and @Qualifier are automatically injected by byName by default, and @Autowired is automatically injected by byType by default.
2. @Resource has two properties that are more important, name and type. If the name attribute is used, the automatic injection policy of byName is used. When using the type attribute, the byType automatic injection policy is used.
3. @Resources is an annotation provided by JDK, while @Autowired is an annotation provided by Spring.
You can treat @Resource as the boss of @AutoWired @Qualifier, haha. I have what you have, and I have what you don’t have, and I have too~
@Resource, @AutoWired, and @Qualifier are all used to inject objects. Among them, @Resource can be injected in name or type, @AutoWired can only be injected in type, and @Qualifier can only be injected in name.
But they have some subtle differences:
1. @Resource and @Qualifier are automatically injected by byName by default, and @Autowired is automatically injected by byType by default.
2. @Resource has two properties that are more important, name and type. If the name attribute is used, the automatic injection policy of byName is used. When using the type attribute, the byType automatic injection policy is used.
3. @Resources is an annotation provided by JDK, while @Autowired is an annotation provided by Spring.
You can treat @Resource as the boss of @AutoWired @Qualifier, haha. I have what you have, and I have what you don’t have, and I have too~
The above common Spring annotations The method of using annotations to construct IoC containers is all the content I have shared with you. I hope you can give you a reference and I hope you can support Wulin.com more.