Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - What are the comments in spring?
What are the comments in spring?
Multiple packages are separated by commas.

1 ,@ component

@ component

Is a common form of all components managed by Spring. The @Component annotation can be placed at the beginning of the class, but it is not recommended to use @Component.

2. Controller

The @Controller corresponds to the Bean of the presentation layer, that is, the action, such as:

1 @ controller

2 @ range ("prototype")

3 public class UserAction extends BaseAction & lt user & gt{

4 ……

5 }

After using the @Controller annotation to identify UserAction, it means that UserAction should be handed over to the Spring container for management. In the Spring container, there will be an action named "userAction", which is named after the class name of UserAction. Note: If @Controller doesn't specify its value @Controller, the default bean name is the class name of this class, with lowercase initials. If you specify value @ controller (value = "useraction") or @Controller("UserAction "), then value will be used as the bean name.

The UserAction here also uses the annotation @Scope, @ Scope(“prototype ") to indicate 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, thus avoiding the thread safety problem of the action in struts. The default scope of spring is singleton mode (scope="singleton "), which only creates one Action object. Every visit is the same action object, and the data is not safe. Struts2 requires each visit to correspond to a different action, and scope="prototype "can ensure that an action object is created when requested.

3. @ Service

@Service corresponds to the business layer Bean, for example:

1 @ service ("user service")

2 public class UserServiceImpl implements UserService {

3 ………

4 }

Annotation @ service ("userService") tells Spring that when Spring wants to create an instance of UserServiceImpl, the name of the bean must be called "userservice", so that when an action needs to use an instance of UserServiceImpl, You can create a good "userService" by Spring and then inject it into Action: in Action, you only need to declare a variable named "userService" to receive the "user Service" injected by Spring. The specific code is as follows:

1 // Inject user services

2 @Resource(name = "userService ")

3 private UserService user service;

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 "userService" variable declared in the action is marked with @Resource annotation. And point out its name = "userService ",which is equivalent to telling Spring that my action should instantiate a" userService ". Please instantiate it for me quickly and give it to me. When Spring sees the comment of @Resource on the userService variable, it can know it according to its specified name attribute. You need to use an instance of UserServiceImpl in your operation. At this point, Spring will inject its own userServiceimpl instance named "userService" into the "userService" variable in the action to help the action complete the instantiation of userservice. In this way, "userserviceuserservice = newuserserviceimpl ();" Not in action. This primitive way of instantiating userService. If there is no Spring, then when the action needs to use UserServiceImpl, it must pass "userserviceuserservice = newuserservicempl ();" Actively create instance objects, but after using Spring, when Action wants to use UserServiceImpl, there is no need to actively create an instance of UserServiceImpl. The created instance of UserServiceImpl has been handed over to Spring, and Spring handed over the created instance of UserServiceImpl to Action, which can be used directly. You can use Action immediately after actively creating an instance of UserServiceImpl, but it has become passive. Wait for Spring to create an instance of UserServiceImpl and inject Action, and then you can use Action. This shows that the "control" of Action on the "UserServiceImpl" class has been "reversed". Originally, I took the initiative in my own hands and wanted to use an instance of the "UserServiceImpl" class. I can use it immediately if I take the initiative to go to new, but now I can't take the initiative to go to the instance of the "userserviceimpl" class. The instantiation ability of the new "UserServiceImpl" class has been taken away by Spring. Only Spring can create a new instance of the "userserviceimpl" class, and Action can only wait for Spring to create an instance of the "userserviceimpl" class. Then "implore" Spring to give him an instance of the created "UserServiceImpl" class and let him use "UserServiceImpl", which is the core idea of Spring's "control inversion", also called "dependency injection", and it is easy to understand. The operation requires UserServiceImpl to work. Then there is the dependence on UserServiceImpl. Spring injects (that is, "endows") the UserServiceImpl that Acion needs to rely on into the Action, which is called "dependency injection". For action, if action depends on something, please inject it into him in spring. For spring, if action needs something, spring will take the initiative to inject it into him.

4. @ Warehouse

@Repository corresponds to the data access layer Bean, for example:

1 @ Repository(value = " userDao ")

2 public class UserDaoImpl extends basedaoimpl < user > {

3 ………

4 }

The comment @Repository(value="userDao ") tells Spring to create an instance of userDao impl named" userdao ".

When a service needs to use an instance of userDao impl named "userDao" created by Spring, you can use annotation @ resource (name = "userdao") to tell Spring that Spring can inject the created userdao into the service.

1 // inject userDao, and you need to retrieve the specified user from the database according to the user Id.

2 @Resource(name = "userDao ")

3 private BaseDao & lt users & gtuserDao