<context:annotation-config> is the annotations above that are used to activate beans that have already been registered in the Spring container, either through XML or through the package sanning.
<context:component-scan> in addition to having <context:annotation-config> functions,<context:component-scan> You can also scan and register JavaBean under the specified package.
Example: There are three classes of A,b,c, and B,c objects are injected into A.
Package Com.spring;public Class B {public B () { System.out.println ("Creating bean B:" + this);} } Package Com.spring;public Class C {public C () { System.out.println ("Creating bean C:" + This);} } Package Com.spring2;import Com.spring.b;import Com.spring.c;public class A { private b b; Private c C; Public A () { System.out.println ("Creating Beans A:" + this); } public void setbbb (b b) { System.out.println ("Setting a.b with" + B); this.b = b; } public void SetC (c c) { System.out.println ("Setting A.C with" + C); THIS.C = C; }}
Add the following configuration to the Applicationcontext.xml:
<bean id= "B" class= "com.spring.b"/><bean id= "C" class= "com.spring.c"/><bean id= "a" class= " com.spring2.a "> <property name=" B "ref=" B "/> <property name=" C "ref=" C "/></bean>
Loading the Applicationcontext.xml configuration file, you will get the following result:
Creating bean B: [Email protected]
Creating bean C: [Email protected]
Creating bean A: [Email protected]
Setting a.b with [email protected]
Setting A.C with [email protected]
Here's an annotated way to simplify our XML configuration file
First, we use Autowire to inject objects B and C into a:
Package Com.spring2;import Org.springframework.beans.factory.annotation.autowired;import Com.spring.B;import Com.spring.c;public class A { private b b; Private c C; Public A () { System.out.println ("Creating Beans A:" + this); } @Autowired public void Setb (b b) { System.out.println ("Setting a.b with" + B); this.b = b; } @Autowired public void SetC (c c) { System.out.println ("Setting A.C with" + C); THIS.C = C; }}
Removing attributes in the Applicationcontext.xml configuration file <property> simplifies to look like this
<bean id= "B" class= "com.spring.b"/><bean id= "C" class= "com.spring.c"/><bean id= "a" class= " com.spring2.a "/>
When we load the Applicationcontext.xml configuration file, we get the following result:
Creating bean B: [Email protected]
Creating bean C: [Email protected]
Creating bean A: [Email protected]
OK, obviously there is no injected attribute in ClassA, the result is wrong, what is it? Why is our attribute not injected into it?
Because the annotations themselves are not capable of doing anything, they are just the most basic components, and we need a processing tool that can handle these annotations.
This is what <context:annotation-config> does to activate beans that have already been registered in the Spring container .
We will modify the Applicationcontext.xml configuration file as follows:
<context:annotation-config/><bean id= "B" class= "com.spring.b"/><bean id= "C" class= "Com.spring.C"/ ><bean id= "A" class= "com.spring2.a"/>
This time, when we load the Applicationcontext.xml configuration file, we get the following result:
Creating bean B: [Email protected]
Creating bean C: [Email protected]
Creating bean A: [Email protected]
Setting a.b with [email protected]
Setting A.C with [email protected]
Consistent with the expected results
But if we modify the code as follows:
Package Com.spring;import Org.springframework.stereotype.Component, @Componentpublic class B {public B () { System.out.println ("Creating bean B:" + This);} } Package Com.spring;import Org.springframework.stereotype.Component, @Componentpublic class C {public C () { System.out.println ("Creating bean C:" + This);} } Package Com.spring2;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.component;import Com.spring.b;import com.spring.c; @Componentpublic class A { Private b b; Private c C; Public A () { System.out.println ("Creating Beans A:" + this); } @Autowired public void Setb (b b) { System.out.println ("Setting a.b with" + BBB); this.b = b; } @Autowired public void SetC (c c) { System.out.println ("Setting A.CCC with" + C); THIS.C = C; }}
The Applicationcontext.xml configuration file is modified to:
<context:annotation-config/>
When we load the Applicationcontext.xml configuration file, but there is no output, why?
That's because <context:annotation-config/> can only work on beans that have already been registered.
For beans that are not registered in the spring container, it is not capable of performing any operations.
除了具有
In addition to the functions of the <context:component-scan> <context:annotation-config/>, there is automatically a @component, @service, @ Repository objects such as annotations are registered with the spring container.
We will modify the Applicationcontext.xml configuration file as follows:
<context:component-scan base-package= "com.spring"/>
When we load the applicationcontext.xml, we get the following result:
Creating bean B: [Email protected]
Creating bean C: [Email protected]
What is the reason for this?
Because we only scanned the classes of the Com.spring package and its sub-packages, and Class A was under the Com.spring2 package, so we couldn't scan the
Let's add com.spring2 to the Applicationcontext.xml:
<context:component-scan base-package= "Com.spring,com.spring2"/>
Then load the applicationcontext.xml and you will get the following result:
Creating bean B: [Email protected]
Creating bean C: [Email protected]
Creating bean A: [Email protected]
Setting a.b with [email protected]
Setting A.C with [email protected]
Looking back at our Applicationcontext.xml file, has been simplified to two lines context:component-scan, is not very simple?
So if we manually add the following configuration to the Applicationcontext.xml,
In other words, both the Applicationcontext.xml and the object of an instance are registered manually in the Component-scan, and the objects of B,c are scanned and registered by the following
<context:component-scan base-package= "com.spring"/><bean id= "a" class= "com.spring2.a"/>
The result is still correct:
Creating bean B: [Email protected]
Creating bean C: [Email protected]
Creating bean A: [Email protected]
Setting a.b with [email protected]
Setting A.C with [email protected]
Although Class A is not registered in a container by scanning,
However, the processor tool that the <context:component-scan> generates to handle those annotations will handle all the beans that are bound to the container, whether manually registered through XML or through the scanning scan.
So, what if we go through the following way? We have configured the <context:annotation-config/>, and also configured the <context:component-scan base-package= "com.spring"/> They all have the ability to handle annotations within the bean registered in the container. Will there be repeated injections of the situation?
<context:annotation-config/><context:component-scan base-package= "com.spring"/><bean id= "a" class= "Com.spring2.a"/>
Do not worry, will not appear, the result is as follows:
Creating bean B: [Email protected]
Creating bean C: [Email protected]
Creating bean A: [Email protected]
Setting a.bbb with [email protected]
Setting A.CCC with [email protected]
Because <context:annotation-config/> and <context:component-scan> exist at the same time, the former is ignored.
That is, those @autowire, @resource and so on, will only be injected once.
Even if you manually register multiple processors, spring will still only process it once:
<context:annotation-config/><context:component-scan base-package= "com.spring"/><bean id= "a" class= "com.spring2.a"/><bean id= "B1" class= " Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/><bean id=" B2 "class=" Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/><bean id=" B3 "class=" Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/><bean id=" B4 "class=" Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>
The result is still correct:
Creating bean B: [Email protected]
Creating bean C: [Email protected]
Creating bean A: [Email protected]
Setting a.b with [email protected]
Setting A.C with [email protected]
The role of Spring <context:annotation-config> and <context-component-scan>