1 Automatic assembly of javabean
Automatic injection to reduce the configuration information of the xml file.
<?xml version="1.0" encoding="UTF-8"?><!-- Constraints to enter xml files--><beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"> <!-- 1 Instantiated Dao object id: Complete reference to the object class: Specify the fully qualified name of the class corresponding to the object that needs to be created --> <bean id="usersDao"> </bean> <!-- 2 Instantiated service autowire: The function of the attribute, complete the automatic assembly between object dependencies no (default execution) byName: Use the method name of the set corresponding to the attributes that need to be injected to match the id of the object in the spring container. If it can be matched, automatically inject byType: Use the method parameter type of the set corresponding to the attributes that need to be injected to match the type of the object in the spring container. If it can be matched, automatically inject constructor: Choose between byName and byType (first byName, if byName does not match, then byType) Actual use: byName --> <bean id="usersService" autowire="byType"> </bean> <!-- 3 Instantiate the Action object--> <bean id="usersAction" autowire="byType"> </bean></beans>
2 Scan annotations for spring
Use spring's scanning annotations to reconstruct the three-layer structure. Configure less content
In the applicationContext.xml file, import the scanned xsd
l Turn on annotation scanning
<?xml version="1.0" encoding="UTF-8"?><!-- Constraints to enter xml files--><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" [ A1 ] xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd "> <!-- Enable annotation scanning base-package attribute: Specify the package that needs to be scanned, used between multiple packages, separated abc abd abe --> <context:component-scan base-package="org.guangsoft.dao.impl, org.guangsoft.service.impl,org.guangsoft.action"></context:component-scan></beans>
Summary of the annotations
Class annotation:
@controller (annotation to web layer)
@service (annotation added to the servce layer)
@repository(annotation added to dao layer)
@component (Add to the java class, the old version of spring only has this one annotation)
The above three notes: Include the corresponding class into the corresponding spring container
Id: lowercase of the first letter of the class name (default)
If you need to specify the id yourself, you need to add the parameters of the String class to the three annotations
@controller("uAction")id=uAction
@resouce (annotation to the object attributes that need to be depended on)
The injection of dependency attributes is completed through automatic assembly.
Parameter: name: Automatic assembly according to byName
Parameters: type: Automatic assembly according to byType
Annotation execution process
1. Load the spring container
2. Scan the specified package in the spring container
3. Scan the specified package, add three class annotations, and then include the class into the spring container
4,<beanid=””class=””>
5. Scan the attributes that are added to the @resource annotation in the class, and then establish the relationship according to the automatic assembly method.
6, Autowrie
Summarize
The above is all the detailed explanation of Spring automatic assembly and scanning annotation code in this article, I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
Detailed explanation of component scanning based on annotation
Analysis of problem of scanning multiple packages in spring configuration
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!