Spring component scans the automatic assembly and Bean

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_43687990/article/details/102594678

Scanning component (component scanning): Spring able to automatically scan from the classpath, detection and specific examples of the component having annotations

Specific components include:

@Component:基本注解,标识一个受Spring管理的组件
@Respository:标识持久层
@Service:标识服务层(业务层)组件
@Controller:标识表现层组件

To the scanning assembly, Spring have default naming policy: a non-qualified class name, a first letter in lower case, can be identified by the value attribute name of the component in the annotation.

When used on a specific annotation component class, but also need to be declared in the configuration file Spring context: Component-Scan
-> Base-Package property: specify a base class needs to scan a package, the container will scan Spring base class package classes and all sub-packet, when the need to scan a plurality of packages may be separated by commas
context: include-filter child node representing the target class to include
context: exclude-filter child node representing the target class to be excluded
context: component-scan the next can have a number of context: include-filter and context: exclude-filter subnode

context: include-filter and context: exclude-filter child node supports multiple types of filter expressions
Here Insert Picture Description
Sample Code

<!-- base-package 属性指定一个需要扫描的基类包,Spring容器会扫描该基类包及其子包下的所有类 -->
	 <context:component-scan base-package="基类包的路径"></context:component-scan> 
	
	<!-- resource-pattern:指定扫描的资源:如下指定的是tow包下的所有类 -->
<!-- 	<context:component-scan base-package="基类包的路径" resource-pattern="tow/*.class"></context:component-scan>
 -->
 	
 	<!--context:exclude-filter type="annotation":子节点指定排除那些指定表达式的组件,即指定那些注解被排除  
	如下是排除了Service注解所标识的类
-->
 	<!-- <context:component-scan base-package="基类包的路径" >
 		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
 	</context:component-scan>
 --> 	
 
 	
 	<!--context:include-filter type="annotation":子节点指定只包含那些表达式的组件,即指定只包含那些注解  ,与use-default-filters搭配使用  ,如下是只向ioc容器中加入Service注解所标识的bean-->
 <!-- 	<context:component-scan base-package="基类包的路径" use-default-filters="false">
 		<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
 	</context:component-scan> -->
 	
 	<!-- context:exclude-filter type="assignable":子节点下指定那个类被排除 -->
 	<!-- <context:component-scan base-package="基类包的路径">
 		<context:exclude-filter type="assignable" expression="被排除类的全类名"/>
 	</context:component-scan> -->
 	
 	<!--context:include-filter type="assignable":指定只包含那个类,与use-default-filters搭配使用  -->
 	<!-- <context:component-scan base-package="基类包的路径" use-default-filters="false">
 		<context:include-filter type="assignable" expression="只包括类的全类名"/>
 	</context:component-scan> -->

context: component-sacn element also automatically registered AutowircedAnnotationBeanPostProcessor instance, which can be automatically loaded and even having @Autowired @ Resource, @ Inject annotation properties

Bean @Autowired annotation single attribute automatic assembly of a compatible type
constructors, the normal field (even non-public), the method having all parameters can be applied @Authwired annotation
default, all the attributes are used @Authwired annotations to be set. when Spring can not find matching properties Bean assembly, will throw an exception, if a property is not allowed to be set, you can set the required properties @Authwired annotation is false
by default, there are multiple types of compatible when IOC container Bean, it will not work by the automatic assembly type. at this time, the name may be provided in Bean @Qualifier annotation in. the method allows the Spring of the parameters have been specified name annotation @Qualifiter Bean injection of
@Authwired annotations may also be applied in an array type on the property, this time will all match Spring Bean for automatic assembly.
@Authwired annotations may also be applied in collection property, then reads type information Spring set, then the automatic assembly of all compatible Bean.
@Authwired notes when used in java.util.Map, if the key for the Map String, then S The automatic assembling pring Map value type compatible with Bean, Bean name as a key at this time

Spring supports @Resource @Inject and annotations, and the annotation function both @Autowired annotations similar
@Resource Bean annotation request a name attribute, if the attribute is empty, or automatically using the method of the variable name as a label at the Bean the name
@Inject and @Autowired annotation as also by type of match injected Bean, but not reqired properties
recommended @Autowired comment

Guess you like

Origin blog.csdn.net/qq_43687990/article/details/102594678