Spring Learning (2) common Spring annotations
Common Spring annotations
Construct an IoC Container Using annotations
Use annotations to register beans with Spring containers. Register in applicationContext. xml .
For example, specify a package in base-package.
1
IndicatesCn. gacl. javaIn the package and its sub-packages, if a class has a specific annotation [@ Component/@ Repository/@ Service/@ Controller ], the object is registered as a Bean in the Spring container. You can also Multiple packages, such:
1
Multiple packages are separated by commas.
1. @ Component
@ Component
Is a common form of all Spring management components. The @ Component annotation can be placed on the class head, And @ Component is not recommended.
2. @ Controller
@ Controller corresponds to the Bean of the presentation layer, that is, Action, for example:
1 @ Controller
2 @ Scope ("prototype ")
3 public class UserAction extends BaseAction {
4 ......
5}
After the @ Controller annotation is used to identify UserAction, it indicates that the UserAction should be handed over to the Spring container for management. There will be an action named "userAction" in the Spring container, this name is obtained based on the UserAction class name. NOTE: If@ ControllerDo not specifyValue [@ Controller ],The default bean name is the first letter of the class name in lowercase.Value [@ Controller (value = "UserAction ")]Or[@ Controller ("UserAction ")],Then, use value as the bean name..
Here, the UserAction also uses the @ Scope annotation. @ Scope ("prototype") indicates that the Action range is declared as a prototype,The scope = "prototype" of the container can be used to ensure that each request has a separate Action for processing, avoiding the thread security issue of the Action in struts. By default, scope in spring is a singleton mode (scope = "singleton"). In this way, only one Action object is created, and each access is the same Action object, causing insecure data, struts2 requires different actions for each access. scope = "prototype" ensures that an Action object is created when a request is sent.
3. @ Service
@ Service corresponds to the business layer Bean, for example:
1 @ Service ("userService ")
2 public class UserServiceImpl implementsUserService {
3 .........
4}
The @ Service ("userService") annotation tells Spring that when Spring wants to create an instance of UserServiceImpl, the bean name must be "userService". In this way, when Action needs to use an instance of UserServiceImpl, the "userService" created by Spring can be injected to the Action. In the Action, you only need to declare a variable named "userService" to receive the "userService" injected by Spring, the Code is as follows:
1 // inject userService
2 @ Resource (name = "userService ")
3 private UserService userService;
Note: The type of the "userService" variable declared in the Action must be "UserServiceImpl" or its parent class "UserService". Otherwise, injection cannot be performed due to inconsistent types, because the declared "userService" variable in the Action uses the @ Resource annotation to mark and specifies its name = "userService", this is equivalent to telling Spring, let's say that I want to instantiate a "userService" for the Action. You can instantiate it for me in Spring and give it to me. When Spring sees the @ Resource Annotation on the userService variable, according to the name attribute specified by this parameter, an instance of UserServiceImpl must be used in the Action, at this time, Spring will inject the UserServiceImpl instance named "userService" into the "userService" variable in the Action to help the Action complete use RService instantiation, so that in the Action, you do not need to instantiate UserService in the original way "userService = new UserServiceImpl. If there is no Spring, when the Action needs to use UserServiceImpl, you must use "UserService userService = new UserServiceImpl ();" to create an instance object, but after using Spring, to use UserServiceImpl for an Action, you do not need to create the UserServiceImpl instance. The UserServiceImpl instance has been created by Spring. Spring will give the created UserServiceImpl instance to the Action, the Action can be used directly. The Action can be used immediately after an active UserServiceImpl instance is created. Instead, the Action can be used only when the UserServiceImpl instance is created by Spring and then injected to the Action. This indicates that the "control" of the Action on the "UserServiceImpl" class has been "Reversed". In the past, the initiative was in the hands of the user, and the user needs to use the "UserServiceImpl" class instance, you can use it immediately after you take the initiative to create a new instance, but now you cannot take the initiative to go to the new "UserServiceImpl" class instance, the power of the new "UserServiceImpl" class instance has been removed by Spring. Only Spring can be used for the new "UserServiceImpl" class instance, action can only wait for Spring to create the "UserServiceImpl" class instance, and then "Beg" Spring to give it the created "UserServiceImpl" class instance, in this way, he can use "UserServiceImpl", which is the core idea of Spring"Control reversal", Also called"Dependency Injection"," Dependency injection "is also easy to understand. Actions need to work with UserServiceImpl, which is to generate dependency on UserServiceImpl. Spring injects the UserServiceImpl that Acion needs to depend on (that is," give ") for Action, this is the so-called "dependency injection ". For an Action, when an Action depends on something, Spring is requested to inject it to it. For Spring, Spring actively injects the Action to it.
4. @ Repository
@ Repository corresponds to the data access layer Bean, for example:
1 @ Repository (value = "userDao ")
2 public class UserDaoImpl extendsBaseDaoImpl {
3 .........
4}
@ Repository (value = "userDao") annotation tells Spring to create a UserDaoImpl instance named "userDao.
When the Service needs to use the UserDaoImpl instance created by Spring, the @ Resource (name = "userDao") annotation can be used to notify Spring, spring injects the created userDao into the Service.
1 // inject userDao, which is used when a specified user is retrieved from the database based on the user ID
2 @ Resource (name = "userDao ")
3 private BaseDao UserDao;