Spring common annotations Use annotations to construct IoC containers

  • 2020-12-18 01:49:35
  • OfStack

Use annotations to construct the IoC container

Register Bean with annotations to the Spring container. You need to register with applicationContext.xml < context: component - scan base - package = "pagkage1 [, pagkage2,..., pagkageN]" / > .

For example: specify 1 package in ES20en-ES21en


<context:component-scan base-package="cn.gacl.java"/>

Indicates that in the cn.gacl.java package and its subpackages, if a class has a specific annotation [@Component / @Repository / @Service / @Controller] on the header, this object is registered as Bean into the Spring container. Can also be < context: component - scan base - package = "/" > Specify multiple packages, such as:


<context:component-scan base-package="cn.gacl.dao.impl,cn.gacl.service.impl,cn.gacl.action"/>

Multiple packages separated by commas.

1. @ Component

@Component

The @Component annotation can be placed on the head of the class. @ES54en is not recommended.

2, @ Controller

@ES60en corresponds to Bean of the presentation layer, i.e. Action, for example:


 @Controller
 @Scope("prototype")
 public class UserAction extends BaseAction<User>{
  ... 
 }

When you identify UserAction with the @Controller annotation, you are handing UserAction over to the Spring container. In the Spring container, there will be an action named "userAction", named after the class name of UserAction. Note: If @Controller does not specify its value [@Controller], the default bean name is lowercase for the class name, and if value [@Controller (value="UserAction")] or [@Controller ("UserAction")], value is used as the name of bean.

UserAction here also uses the @Scope annotation. @Scope ("prototype") means to declare the scope of Action as a prototype. You can use the container's scope="prototype" to ensure that each request is handled by a separate Action to avoid the thread safety issues of Action in struts. spring default scope is singleton mode (scope="singleton"), so only 1 Action object will be created, each access is the same as 1Action object, the data is not secure, struts2 is required every access corresponding to a different Action, scope="prototype" can ensure that when there is a request to create 1 Action object

3, @ Service

@ES114en corresponds to business layer Bean, for example:


@Service("userService")
 public class UserServiceImpl implements UserService {
  ...... 
 }

The @ES119en ("userService") annotation tells Spring that when Spring wants to create an instance of UserServiceImpl, bean must be named "userService" so that when Action wants to use an instance of UserServiceImpl, it can be created by Spring and 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:


 //  injection userService
 @Resource(name = "userService")
 private UserService userService;

Note: In Action statement "userService" the type of the variable must be "UserServiceImpl" or its parent class "UserService", due to type 1 not cause can't injection, as a result of the statement of Action "userService" variable using the @ Resource annotations to indicate, and indicate its name = "userService", this is to tell Spring, said I Action to instantiate a "userService", you Spring instantiation for me soon, and then to me. When Spring sees the @ES153en annotation on the userService variable, it can be known from the name attribute indicated that an instance of UserServiceImpl is needed in Action. At this time, Spring will inject the instance of UserServiceImpl with the name of "userService" into the "userService" variable in Action to help Action complete the instantiation of userService. This eliminates the need for UserService userService = new UserServiceImpl(); This is the original way to instantiate userService.

If there is no Spring, then when Action needs to use UserServiceImpl, it must pass "UserService userService = new UserServiceImpl(); But after using Spring, when Action wants to use UserServiceImpl, there is no need to actively create an instance of UserServiceImpl. The creation of UserServiceImpl instance has been handed over to Spring. Spring gives the instance of UserServiceImpl created to Action, and Action can get it and use it directly.

Action can be used immediately after the original UserServiceImpl instance is created actively. Instead, Action can be used only after the UserServiceImpl instance is created by Spring and injected into Action.

This shows Action "control" for "UserServiceImpl" class "inverted" had been made, and the initiative in own hand, you want to use "UserServiceImpl" class instances, to new1 out on their own can be used immediately, but now I can't take the initiative to go new "UserServiceImpl" class instances, new "UserServiceImpl" the power of the instances of the class have been Spring away, can only Spring new "UserServiceImpl" instance of the class, And Spring such as Action can only create a good "UserServiceImpl" after class instances, "begged" Spring again to create good "UserServiceImpl" instance of the class to give him, so he can use "UserServiceImpl", this is Spring core idea "inversion of control", also known as "dependency injection", "di" is also very good understanding, Action UserServiceImpl work will be used, then the UserServiceImpl produced dependence, Spring gives UserServiceImpl the injection (also known as "give") that Acion depends on, known as "dependency injection". For Action, Action depends on something by requesting Spring injection, and for Spring, Spring actively injects what Action needs.

4, @ Repository

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


@Repository(value="userDao")
public class UserDaoImpl extends BaseDaoImpl<User> {
 ...... 
}

The @Repository (value="userDao") annotation tells Spring to create an instance of UserDaoImpl named "userDao".

When Service needs an instance of UserDaoImpl created using Spring called "userDao", it can tell Spring using the @Resource (name = "userDao") annotation and Spring will simply inject the created userDao into Service.


//  injection userDao , from the database according to the user Id This is needed to fetch the specified user 
@Resource(name = "userDao")
private BaseDao<User> userDao;

@ES264en, @ES265en, @Qualifier are all used to inject objects. Where @Resource can be injected as name or type, @AutoWired can only be injected as type and @ES272en can only be injected as name.

But there are a few subtle differences:

1. By default, @ES278en and @ES279en press byName and @ES281en press byType for automatic injection.

2. There are two important attributes of @Resource, namely name and type. If the name attribute is used, the automatic injection strategy of byName is used. When using the type attribute, the byType automatic injection policy is used.

3. @ES294en is the annotation provided by JDK and @ES296en is the annotation provided by Spring.

Think of @Resource as the boss of @AutoWired @Qualifier, lol. You have, I have, you do not have, I also have ~

@ES305en, @ES306en, @ES307en are all used to inject objects. Where @ES308en can be injected as name or type, @ES311en can only be injected as type and @ES313en can only be injected as name.

But there are a few subtle differences:

1. By default, @ES319en and @ES320en press byName for automatic injection and @ES322en for automatic injection.

2. There are two important attributes of @Resource, namely name and type. If the name attribute is used, the automatic injection strategy of byName is used. When using the type attribute, the byType automatic injection policy is used.

3. @ES335en is the annotation provided by JDK and @ES337en is the annotation provided by Spring.

Think of @Resource as the boss of @AutoWired @Qualifier lol. You have, I have, you do not have, I also have ~


Related articles: