Spring commonly uses annotations to construct IOC containers
Using annotations to construct IOC containers
Use annotations to register beans with the spring container. Need to be in ApplicationContext Register < context: component scan base package = "pagkage1 [, pagkage2,..., pagkagen] / > in XML.
For example, specify a package in base package
Indicates CN gacl. In the java package and its sub packages, if a class has a specific annotation [@ component / @ repository / @ service / @ controller] on its header, 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:
Multiple packages are separated by commas.
1、@Component
@Component
It is a common form of all spring managed components. The @ Component annotation can be placed on the head of the class, and @ component is not recommended.
2、@Controller
@The controller corresponds to the bean of the presentation layer, that is, the action, for example:
After the @ controller annotation is used to identify the useraction, it means that the useraction will be handed over to the spring container for management. There will be an action named "useraction" in the spring container, which is taken according to the useraction class name. Note: if @ controller does not specify its value [@ controller], the default bean name is the lowercase initial of the class name. If value [@ controller (value = "useraction")] or [@ controller ("useraction")] is specified, value is used as the bean name.
The useraction here also uses the @ scope annotation, @ scope ("prototype") means to declare the scope of the action as a prototype. You can use the scope = "prototype" of the container to ensure that each request has a separate action to process, so as to avoid the thread safety problem of the action in struts. The default scope of spring is the singleton mode (scope = "Singleton"), so only one action object will be created. Each access is the same action object, and the data is unsafe. Struts 2 requires that each access corresponds to a different action. Scope = "prototype" can ensure that an action object is created when there is a request
3、@ Service
@Service corresponds to a business layer bean, for example:
@The service ("userservice") annotation tells spring that when spring wants to create an instance of userserviceimpl, the bean name must be "userservice", so that when an action needs to use an instance of userserviceimpl, it can create a "userservice" by spring, Then inject it into the action: in the action, you only need to declare a variable named "userservice" to receive the "userservice" injected by spring. The specific code is as follows:
Note: the type of "userservice" variable declared in action must be "userserviceimpl" or its parent class "userservice", otherwise it cannot be injected due to inconsistent types. Because the "userservice" variable declared in action is marked with @ resource annotation and indicates its name = "userservice", this is tantamount to telling spring, I said that my action needs to instantiate a "userservice". Spring can help me instantiate it quickly, and then give it to me. When spring sees the annotation of @ resource on the userservice variable, according to the specified name attribute, it can be known that an instance of userserviceimpl needs to be used in the action, At this time, spring will inject the created instance of userserviceimpl named "userservice" into the "userservice" variable in the action to help the action complete the instantiation of userservice. In this way, there is no need to use "userservice userservice = new userserviceimpl();" in the action This is the most primitive way to instantiate userservice.
If there is no spring, when the action needs to use userserviceimpl, it must pass "userservice userservice = new userserviceimpl();" Take the initiative to create an instance object, but after using spring, when an action wants to use userserviceimpl, it doesn't have to take the initiative to create an instance of userserviceimpl. Creating a userserviceimpl instance has been handed over to spring. Spring gives the created userserviceimpl instance to action, and the action can use it directly.
The action can be used immediately after actively creating the userserviceimpl instance. Instead, it can be used only after spring creates the userserviceimpl instance and then injects it into the action.
This shows that the "control" of action over the "userserviceimpl" class has been "reversed". Originally, the initiative is in your own hands. You want to use the instance of "userserviceimpl" class. You can use it immediately after you take the initiative to go to new, but now you can't take the initiative to go to the instance of new "userserviceimpl", Spring has taken away the power of the instance of the new "userserviceimpl" class. Only spring can create an instance of the new "userserviceimpl" class, while action can only wait until spring has created an instance of the "userserviceimpl" class, and then "beg" spring to give him the created instance of the "userserviceimpl" class, so that he can use the "userserviceimpl", This is spring's core idea of "inversion of control", also known as "dependency injection". Dependency injection is also well understood. Action needs to work with userserviceimpl, so it has a dependency on userserviceimpl. Spring injects the userserviceimpl that acion needs to rely on (that is, "to") into action, which is the so-called "dependency injection". For action, spring is requested to inject what action depends on. For spring, spring actively injects what action needs.
4、@ Repository
@The repository corresponds to the data access layer bean, for example:
@The repository (value = "userdao") annotation tells spring to 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 that spring can inject the created userdao into the service.
@Resource, @ Autowired, @ qualifier are all used to inject objects. Among them, @ resource can be injected by name or type, @ Autowired can only be injected by type, and @ qualifier can only be injected by name.
But there are 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 important attributes, name and type. If the name attribute is used, the auto injection policy of byname is used. When the type attribute is used, the bytype automatic injection policy is used.
3. @ resources is the annotation provided by JDK, and @ Autowired is the annotation provided by spring.
You can regard @ resource as the boss of @ Autowired @ qualifier, ha ha. I have what you have, and I have what you don't have~
@Resource, @ Autowired, @ qualifier are all used to inject objects. Among them, @ resource can be injected by name or type, @ Autowired can only be injected by type, and @ qualifier can only be injected by name.
But there are 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 important attributes, name and type. If the name attribute is used, the auto injection policy of byname is used. When the type attribute is used, the bytype automatic injection policy is used.
3. @ resources is the annotation provided by JDK, and @ Autowired is the annotation provided by spring.
You can regard @ resource as the boss of @ Autowired @ qualifier, ha ha. I have what you have, and I have what you don't have~
In the above spring common annotations, the method of using annotations to construct IOC container is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.