1 Automatic assembly of JavaBean
Automatic injection, reducing configuration information for XML files.
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <!--constraints into the XML file -3 <Beansxmlns= "Http://www.springframework.org/schema/beans"4 xmlns:p= "http://www.springframework.org/schema/p"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"5 xsi:schemalocation= "Http://www.springframework.org/schema/beans6 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd ">7 <!--1 instantiating DAO object ID: Completing a reference to an object class: Specifies the fully qualified name of the class that corresponds to the object to be created -8 <BeanID= "Usersdao"class= "Org.guangsoft.dao.impl.UsersDaoImpl">9 </Bean>Ten <!--2 Instantiate service Autowire: The function of the property, complete the automatic assembly of the object dependencies No (default execution) ByName: match the name of the set method with the property that needs to be injected and the ID of the object in the spring container, if it matches, For automatic injection One Bytype: Match the Set method parameter type of the property that needs to be injected with the type of the object in the spring container, and if it matches, auto-inject constructor: Choose between ByName and Bytype (first byname, If ByName does not match then Bytype) A actual use: ByName - - <BeanID= "Usersservice"class= "Org.guangsoft.service.impl.UsersServiceImpl" - Autowire= "Bytype"> the </Bean> - <!--3 Instantiating the Action object - - <BeanID= "Usersaction"class= "Org.guangsoft.action.UsersAction" - Autowire= "Bytype"> + </Bean> - </Beans> +
2 spring's scanning annotations
Use spring's scanning annotations to refactor the three-layer structure. Configure less content
In the Applicationcontext.xml file, import the scanned XSD
L Turn on annotation scanning
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <!--constraints into the XML file -3 <Beansxmlns= "Http://www.springframework.org/schema/beans"4 Xmlns:context= "Http://www.springframework.org/schema/context"[A1]5 xmlns:p= "http://www.springframework.org/schema/p"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"6 xsi:schemalocation= "Http://www.springframework.org/schema/beans7 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd8 Http://www.springframework.org/schema/context9 http://www.springframework.org/schema/context/spring-context-4.1.xsdTen "> One <!--Turn on Annotation scanning base-package properties: Specify the packages that need to be scanned, use between multiple packages, and separate A.B.C a.b.d A.B.E - A <Context:component-scan - Base-package= "Org.guangsoft.dao.impl, - org.guangsoft.service.impl,org.guangsoft.action "></Context:component-scan> the </Beans>
Summary of annotations
Class Annotations:
@controller (annotations to the web layer)
@service (annotations to the SERIVCE layer)
@repository (annotations to the DAO layer)
Above three annotations: the corresponding class is included in the spring container corresponding to the
Id: First letter of Class name lowercase (default)
If you need to specify an ID yourself, you need to add a parameter to the string class for three annotations
@controller ("Uaction") id=uaction
@resouce (annotations for object attributes that need to be relied upon)
An injection that requires dependency properties is done through automatic assembly.
Parameter: Name: Automatic assembly according to ByName
Parameter: Type: Automatic assembly according to Bytype
Annotation execution Process
1, loading the container of spring
2, scan specified package in spring container
3, scans the specified package, adds three class annotations to the class, and then incorporates the class into the spring container
4, <bean id= "" class= "" >
5, the properties of the @resource annotation are added to the scan class, and then the relationship is established according to the automatic assembly method.
6, Autowrie
Spring Automatic assembly and scanning annotations