English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Detailed Explanation of Spring Auto-configuration and Annotation Scanning Code

1 Automatic assembly of javabean

Automatic injection, reduces the configuration information of xml files.

<?xml version="1.0" encoding="UTF-8"?>
<!-- import constraints into the xml file -->
<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 Instantiation of Dao object id: complete the reference of the object class: specify the fully qualified name of the class corresponding to the object to be created -->
  <bean id="usersDao" class="org.guangsoft.dao.impl.UsersDaoImpl">
  </bean>
  <!-- 2Instantiation of service autowire: the role of properties, to complete automatic assembly of object dependencies no (default execution) byName: use the method name of the set corresponding to the injected attribute and match it with the id of the object in the spring container. If it matches, automatic injection is performed 
    byType: Matches the parameter type of the set method that needs to be injected with the type of the object in the spring container. If they match, automatic injection is performed constructor: Choose between byName and byType (first byName, if byName does not match, then byType) 
    Actual usage: byName -->
  <bean id="usersService" class="org.guangsoft.service.impl.UsersServiceImpl">
    autowire="byType">
  </bean>
  <!-- 3Instantiate the Action object -->
  <bean id="usersAction" class="org.guangsoft.action.UsersAction">
    autowire="byType">
  </bean>
</beans>

2 Spring's scanning annotations

Use spring's scanning annotations to refactor the three-tier structure. Configure less content

Import the scanned xsd in the applicationContext.xml file

l Enable annotation scanning

<?xml version="1.0" encoding="UTF-8"?>
<!-- import constraints into the xml file -->
<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: specifies the packages to be scanned, separated by commas. For example, a.b.c, a.b.d, a.b.e -->
  <context:component-scan
    base-package="org.guangsoft.dao.impl,
    org.guangsoft.service.impl,org.guangsoft.action"></context:component-scan>
</beans>

Summary of annotations

Class annotation:

@controller(Annotation added to web layer)

@service(Annotation added to serivce layer)

@repository(Annotation added to dao layer)

@component(Annotation added to java class, only one annotation in old version of spring)

The above three annotations: put the corresponding class into the corresponding spring container

Id: the first letter of the class name is lowercase (default)

If you need to specify the id yourself, you need to add the parameter of String class to the three annotations

@controller(“uAction”)id=uAction

@resouce(Annotation added to the dependent object property)

The injection of required dependent properties is completed through automatic assembly.

Parameter: name: automatic assembly by name

Parameter: type: automatic assembly by type

Annotation execution process

1,load the spring container

2,scan the specified package in the spring container

3,scan the classes annotated with three class annotations in the specified package, and then add the class to the spring container

4,<beanid=””class=””>

5,scan the properties annotated with @resource in the scanned class, and then establish relationships in the way of automatic assembly

6,Autowrie

Summary

That's all about 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 Annotations

Analysis of the problem of spring configuration scanning multiple packages

If there is anything lacking, please leave a message to point it out. Thank you for your friends' support of this site!

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit the content manually, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3If you find any infringement, please send an email to notice#w with the '#' replaced by '@' and provide relevant evidence. Once verified, this site will immediately delete the suspected infringing content.

You May Also Like