1 The relationship between Bean and spring container
The bean configuration information defines the bean's implementation and dependencies, and the spring container builds the Bean definition registry within the container based on various forms of bean configuration information, then loads, instantiates, and builds the bean and bean dependencies based on the registry. Finally, these ready-to-use beans are placed in the bean cache pool for the outer application to invoke.
1 Bean Configuration
There are three methods of bean configuration:
- Configuring beans based on XML
- Defining Beans with annotations
- Providing bean definition information based on Java classes
1.1 XML-based configuration bean
1.2 Defining beans with annotations
We know that the three main elements of the successful launch of the spring container are the bean definition information, the Bean implementation class, and spring itself. In the case of XML-based configuration, the bean definition information is separate from the bean implementation class itself, and when the annotation-based configuration is used, the bean definition information is implemented by annotating annotations on the Bean implementation class.
The following is a bean that defines a DAO using annotations:
Package Com.baobaotao.anno; Import org.springframework.stereotype.Component; Import org.springframework.stereotype.Repository; // ① defining a DAO bean by repository @Component ("Userdao") Public class Userdao {}
At ①, we use the @component annotation to label the class at the Userdao class declaration, which can be identified by the spring container, and the spring container automatically converts the Pojo into a container-managed bean.
It is equivalent to the following XML configuration:
<id= "Userdao" class= "Com.baobaotao.anno.UserDao"/>
In addition to @component, Spring provides 3 functional basic and @component equivalent annotations, which are used to annotate the controller of DAO, service, and web layers, respectively, so they are also known as the Bean's Yan-type annotations:
- @Repository: Used to annotate DAO implementation classes;
- @Service: Used to annotate Service implementation classes;
- @Controller: Used to annotate the Controller implementation class;
The reason to provide these three special annotations outside of @component is to make the annotation classes themselves clear, and spring will give them some special features.
1.2.1 Using annotation configuration information to start the spring container
Spring provides a context namespace that provides a way to define a bean by scanning the class package to apply annotations:
<?XML version= "1.0" encoding= "UTF-8"?><!--① declaring a context namespace -<Beansxmlns= "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-3.0.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/c Ontext/spring-context-3.0.xsd " > <!--② Scan class package to apply annotation-defined beans - <Context:component-scanBase-package= "Com.baobaotao.anno"/> <Beanclass= "Com.baobaotao.anno.LogonService"></Bean> <!--context:component-scan base-package= "Com.baobaotao" resource-pattern= "Anno/*.class"/ - <!--context:component-scan base-package= "Com.baobaotao" > <context:include-filter type= "regex" expression= "Co M\.baobaotao\.anno.*dao "/> <context:include-filter type=" regex "expression=" Com\.baobaotao\.anno.*service "/ > <context:exclude-filter type= "aspectj" expression= "Com.baobaotao. *controller+ "/> </context:component-scan -</Beans>
Declare the context namespace at ①, where you can specify a base class package that needs to be scanned through the Base-package attribute of the Component-scan of the context namespace, and the spring container will scan all classes in the base class package. And gets the bean's definition information from the class's annotation information.
If you want to scan only specific classes, not all classes under the base package, you can use the Resource-pattern property to filter specific classes as follows:
<base-package= "Com.baobaotao" resource-pattern= "Anno/*.class" >
Here we set the base class package to Com.baobaotao, by default the value of the Resource-pattern property is "**/*.class", which is all classes in the base class package. If we set this to "Anno/*.class" here, spring will only scan the classes in the Anno child package in the base package.
1.3 Providing bean definitions based on Java classes
As long as the @configuration annotations are annotated in the ordinary Pojo class, the bean-defined information can be provided for the spring container, and each class method labeled @bean is equivalent to providing a bean's definition information.
Packagecom.baobaotao.conf;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;//① labeling a pojo as a configuration class for defining beans@Configuration Public classappconf {//② The following two methods define two beans to provide the bean instantiation logic@Bean PublicUserdao Userdao () {return NewUserdao (); } @Bean PublicLogdao Logdao () {return NewLogdao (); } //③ defines a bean for logonservice@Bean PublicLogonservice Logonservice () {Logonservice Logonservice=NewLogonservice (); //④ injection of ② and ③-defined beans into the logonservice beanLogonservice.setlogdao (Logdao ()); Logonservice.setuserdao (Userdao ()); returnLogonservice; }}
① the @configuration annotation at the definition of the Appconf class, stating that this class can be used to provide spring with the bean's definition information. The @bean annotation can be annotated at the method of the class, the type of the bean is determined by the method return value type, the name is the same as the method name, and the name of the bean can be specified by the import parameter, such as @bean (name= "Userdao"). The instantiation logic of the bean is provided directly in the method indicated by @bean.
At ②, the Userdao () and Logdao () methods define a Userdao and a logdao bean whose bean names are Userdao and Logdao, respectively. At ③, a logonservice bean is defined, and the two beans defined in ② are injected at ④.
Therefore, the configuration above and the following XML configuration are equivalent:
<BeanID= "Userdao"class= "Com.baobaotao.anno.UserDao"/><BeanID= "Logdao"class= "Com.baobaotao.anno.LogDao"/><BeanID= "Logservice"class= "Com.baobaotao.conf.LogonService"P:logdao-ref= "Logdao"P:userdao-ref= "Userdao"/>
Java-based classes are configured in a way that is more flexible than XML-based or annotation-based configuration to implement bean instantiation and assembly between beans, but both are configured declaratively to be less flexible, but more simple to configure.
Bean configuration and bean injection in spring