Note implementation of Bean management
- Classpath Scanning and component management
- Automatic detection of classes and registration of beans
- <context:annotation-config/>
- @Component, @Repository, @Service, @Controller
- @Required
- @Autowired
- @Qualifier
- @Resource
(i) Classpath scanning and component management
- Starting with Spring3.0, the Spring Javaconfig project offers a number of features, including the use of Java rather than XML-defined beans, such as: @Configuration, @Bean, @Import, @DependsOn
- @Component is a generic annotation that can be used with any bean
- @Repository, @Service, @Controller is a more targeted note.
-@Repository commonly used to annotate DAO classes, which are persistent layers
-@Service is typically used to annotate service classes, which are services tiers
-@Controller is typically used to annotate the Controller class, which is the control layer
(ii) Automatic detection of classes and the registration of beans
Spring can automatically detect classes and register beans in ApplicationContext
(c) <context:annotation-config/>
- Typically, the XML-based spring configuration is labeled as follows (note that the context namespace is included)
- <context:annotation-config/> only looks for bean annotations in the same applicationcontext
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema /context " 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 "> <context: Annotation-config/> <beans/>
(iv) Automatic detection of classes and the registration of Beans (supplemental)
To be able to detect these classes and register the appropriate beans, you need the following:
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "http:/ Www.springframework.org/schema/beans "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xmlns:context=" H Ttp://www.springframework.org/schema/context "xsi:schemalocation=" Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-4.1.xsd Http://www.springframework.org/schema/co ntext http://www.springframework.org/schema/context/spring-context-4.1.xsd "> <CONTEXT:COMPONENT-SC An base-package= "Com.mypackage" ></CONTEXT:COMPONENT-SCAN></BEANS>
The configuration implicitly registers multiple processors that parse the annotations, such as:
Autowiredannotationbeanpostprocessor
Commonannotationbeanpostprocessor
Persistenceannotationbeanpostprocessor
Requiredannotationbeanpostprocessor
In fact, the annotations themselves do not do anything, as with XML, only the role of configuration, mainly in the back of the powerful processor, including the <context:annotation-config/> configuration item inside the annotations used by the processor, so configured < Context:component-scan base-package= "" > No need to configure <context:annotation-config>
(v) Use filters for custom scanning
- By default, classes are automatically discovered and registered with a bean by using @component, @Repository, @Service, @Controller annotations, or custom annotations that use @component
- The above behavior can be modified through filters, such as: The XML configuration for the following example ignores all @repository annotations and replaces them with "stubs".
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "http:/ Www.springframework.org/schema/beans "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xmlns:context=" H Ttp://www.springframework.org/schema/context "xsi:schemalocation=" Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-4.1.xsd Http://www.springframework.org/schema/co ntext http://www.springframework.org/schema/context/spring-context-4.1.xsd "> <CONTEXT:COMPONENT-SC An base-package= "Com.mypackage" > <!--contains filters--<context:include-filter type= "regex" expression= ". * Stub.*repository "/> <!--excluded Filters--<context:exclude-filter type=" annotation "expression=" org.spring Framework.stereotype.Repository "/> </context:component-scan></beans>
- You can also use use-default-filters= "false" to disable Autodiscover and register
(vi) Defining beans
The component is automatically detected during the scan, then the bean name is Beannamegenerator generated (@Component, @Repository, @Service, @Controller there will be a Name property for explicitly setting the bean Name)
@Component ("Examplebean") public class examplebean{ //...} ------------------------------@Service ("Examplebean") public class examplebean{ /...} ------------------------------//Although not explicitly declared, it is automatically generated according to Beannamegenerator rule: Based on the class name, the first letter of the class name is lowercase @repositorypublic class examplebean{ //...}
You can customize the Bean naming policy, implement the Beannamegenerator interface, and be sure to include a non-parametric constructor
<!--custom naming conventions, such as: class names all lowercase, all uppercase ..... To achieve--><context:component-scan base-package= "Com.mypackage" name-generator= "in Your Own way Com.mypackage.MyBeanNameGenerator "> </context:component-scan>
(vii) Scope
Typically, the spring component is automatically found, and its scope is singleton,spring provides an annotation that identifies Scope @scope
@Scope ("prototype") @Repositorypublic class Oneinterfaceimpl extends Oneinterface { //...}
You can also customize the scope policy, implement the Scopemetadataresolver interface, and provide an argument-free constructor
<context:component-scan base-package= "Com.mypackage" scope-resolver= "Com.mypackage.MyScopeMetadataResolver" ></context:component-scan>
(eight) Agent mode
You can use the Scope-proxy property to specify a proxy with three values to choose from: No,interfaces,targetclass
<context:component-scan base-package= "Com.mypackage" scoped-proxy= "Interfaces" >
</context:component-scan>
Spring Learning (5) Definition of---Bean and annotation implementation of its scope