Spring opens annotation and interpretations and differences

Source: Internet
Author: User
Transferred from http://www.cnblogs.com/leiOOlei/p/3713989.html

Spring opens annotation <context:annotation-config> and <context:component-scan> interpretations and differences

The difference between <context:annotation-config> and <context:component-scan>

<context:annotation-config> is the annotations that are used to activate the beans that have been registered in the Spring container, either through XML or through package sanning.

<context:component-scan> in addition to functions with <context:annotation-config>,<context:component-scan> You can also scan and register JavaBean under the specified package.

Let's look at their differences in detail by example, with three class a,b,c and B,c objects injected into A.

Package com.xxx;
public class B {public
  B () {
    System.out.println ("Creating bean B:" + this);
  }
}

Package com.xxx;
public class C {public
  C () {
    System.out.println ("Creating beans C:" + this);
  }
}

Package com.yyy;
Import com.xxx.b;
Import com.xxx.c;
public class A { 
  private B bbb;
  Private C CCC;
  Public A () {
    System.out.println ("Creating Beans A:" + this);
  }
  public void setbbb (B-BBB) {
    System.out.println ("Setting a.bbb with" + BBB);
    THIS.BBB = BBB;
  public void SETCCC (C CCC) {
    System.out.println ("Setting A.CCC with" + CCC);
    THIS.CCC = CCC; 
  }

Add the following configuration to the Applicationcontext.xml:

<bean id= "Bbean" class= "com.xxx.b"/> <bean id= "Cbean"
class= "com.xxx.c"/> <bean id= "ABean"
class= "Com.yyy.a" >
  <property name= "BBB" ref= "Bbean"/> "<property" name= "
  CCC" ref= "Cbean"/>
</bean>

Loading the Applicationcontext.xml configuration file will result in the following:

Creating Bean B:com.xxx.b@c2ff5
Creating Bean C:com.xxx.c@1e8a1f6
Creating Bean A:COM.YYY.A@1E152C5
Setting A.BBB with Com.xxx.b@c2ff5
Setting A.CCC with Com.xxx.c@1e8a1f6

OK, there's nothing to say about this, but it's all in the way of XML, but it's too outdated to simplify our XML configuration file by annotating the following ways

Let's see how to use <context:annotation-config> and <context:component-scan>

First, we inject the BBB and CCC into a by using the Autowire method:

Package com.yyy;
Import org.springframework.beans.factory.annotation.Autowired;
Import com.xxx.b;
Import com.xxx.c;
public class A { 
  private B bbb;
  Private C CCC;
  Public A () {
    System.out.println ("Creating Beans A:" + this);
  }
  @Autowired public 
  void setbbb (B-BBB) {
    System.out.println ("Setting a.bbb with" + BBB);
    THIS.BBB = BBB;
  @Autowired public 
  void SETCCC (C CCC) {
    System.out.println ("Setting A.CCC with" + CCC);
    THIS.CCC = CCC;
  }

Then the Applicationcontext.xml profile removal Properties <property> is simplified to look like the following

<bean id= "Bbean" class= "com.xxx.b"/> <bean id= "Cbean"
class= "com.xxx.c"/> <bean id= "ABean"
class= "Com.yyy.a"/>

After we load the Applicationcontext.xml configuration file, we get the following result:

Creating Bean B:com.xxx.b@5e5a50
Creating Bean c:com.xxx.c@54a328
Creating Bean A:COM.YYY.A@A3D4CF

OK, ClassA obviously did not inject the attribute, the result is wrong, exactly is because of what. Why our attributes are not injected into it.

Because the annotations themselves are not capable of doing anything, they are only the most basic component, and we need the processing tools that can handle these annotations to handle these annotations.

This is what <context:annotation-config> does to activate the beans that have been registered in the spring container

We modify the Applicationcontext.xml configuration file as follows:

<context:annotation-config/>
<bean id= "Bbean" class= "com.xxx.b"/> <bean id= "CBean" class= "
Com.xxx.c "/> <bean id= Abean" class= "com.yyy.a"/>

This time, when we load the Applicationcontext.xml configuration file, we get the following result:

Creating Bean b:com.xxx.b@15663a2
Creating Bean c:com.xxx.c@cd5f8b
Creating Bean A:com.yyy.a@157aa53
Setting A.BBB with COM.XXX.B@15663A2
Setting A.CCC with com.xxx.c@cd5f8b

OK, the results are correct.

But if we modify the code as follows:

Package com.xxx;
Import org.springframework.stereotype.Component;
@Component public
Class B {public
  B () {
    System.out.println (] creating bean B: "+ this);
  }
}

Package com.xxx;
Import org.springframework.stereotype.Component;
@Component public
Class C {public
  C () {
    System.out.println ("Creating beans C:" + this);
  }

package com.yyy;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.stereotype.Component;
Import com.xxx.b;
Import com.xxx.c;
@Component public
class A { 
  private B bbb;
  Private C CCC;
  Public A () {
    System.out.println ("Creating Beans A:" + this);
  }
  @Autowired public 
  void setbbb (B-BBB) {
    System.out.println ("Setting a.bbb with" + BBB);
    THIS.BBB = BBB;
  @Autowired public 
  void SETCCC (C CCC) {
    System.out.println ("Setting A.CCC with" + CCC);
    THIS.CCC = CCC;
  }

The Applicationcontext.xml configuration file is modified to:

<context:annotation-config/>

Why do we not have any output after loading the applicationcontext.xml configuration file?

That's because <context:annotation-config/> can only work on a bean that has already been registered.

For a bean that is not registered in the spring container, it does not perform any action.

But don't worry,<context:component-scan> in addition to the <context:annotation-config/> feature, it also has automatic @component, @service, @ A feature that is registered in the spring container for repository such as annotations.

We modify the Applicationcontext.xml configuration file as follows:

<context:component-scan base-package= "Com.xxx"/>

When we load the applicationcontext.xml, we get the following result:

Creating Bean b:com.xxx.b@1be0f0a
Creating Bean c:com.xxx.c@80d1ff

What is the reason for that?

Because we only scanned the COM.XXX package and its child package class, and Class A was under the COM.YYY package, so we couldn't scan it.

Here we add com.yyy to the Applicationcontext.xml:

<context:component-scan base-package= "com.xxx"/>
<context:component-scan base-package= "com.xxx, Com.yyy "/>

Then loading the applicationcontext.xml will get the following result:

Creating Bean b:com.xxx.b@cd5f8b
Creating Bean C:COM.XXX.C@15AC3C9
Creating Bean A:com.yyy.a@ec4a87
Setting A.BBB with com.xxx.b@cd5f8b
Setting A.CCC with COM.XXX.C@15AC3C9

Wow, the results are correct.

Looking back at our Applicationcontext.xml file, has been simplified to two lines of Context:component-scan, is not very simple.

So if we manually add the following configuration to the Applicationcontext.xml,

This means that both the instance object of a is manually registered in the Applicationcontext.xml, and the object of B,c is scanned and registered through Component-scan, as follows

<context:component-scan base-package= "com.xxx"/> <bean id= "Abean" class= "com.yyy.a"/>

The results are still correct:

Creating Bean B:com.xxx.b@157aa53
Creating Bean C:com.xxx.c@ec4a87
Creating Bean A:com.yyy.a@1d64c37
Setting A.BBB with Com.xxx.b@157aa53
Setting A.CCC with Com.xxx.c@ec4a87

Although Class A is not registered in the container by scanning,

But the processor tools that <context:component-scan> generates to handle those annotations process all the beans that are bound to the container, either manually registered through XML or registered through scanning scans.

Well, what if we go through the following way. We have not only configured the <context:annotation-config/>, but also configured the <context:component-scan base-package= "com.xxx"/> They all have the ability to handle annotations within the bean registered in the container. Will there be a repeat injection?

<context:annotation-config/>
<context:component-scan base-package= "com.xxx"/> <bean
"id=" Abean "class=" com.yyy.a "/>

Don't worry, it won't happen, the result is as follows:

Creating Bean B:com.xxx.b@157aa53
Creating Bean C:com.xxx.c@ec4a87
Creating Bean A:com.yyy.a@1d64c37
Setting A.BBB with Com.xxx.b@157aa53
Setting A.CCC with Com.xxx.c@ec4a87

Because <context:annotation-config/> and <context:component-scan> exist at the same time, the former will be ignored.

That is, those @autowire, @resource, etc. injected annotation will only be injected once

Even if you manually register multiple processors, spring will still be processed only once:

<context:annotation-config/>
<context:component-scan base-package= "com.xxx"/> <bean
"id=" Abean "class=" com.yyy.a "/> <bean id=" bla "class="
Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>
<bean id=" Bla1 " class= "Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id= "Bla2" class= "Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
< Bean id= "Bla3" class= "Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

The results are still correct:

Creating Bean B:com.xxx.b@157aa53
Creating Bean C:com.xxx.c@ec4a87
Creating Bean A:COM.YYY.A@25D2B2
Setting A.BBB with Com.xxx.b@157aa53
Setting A.CCC with Com.xxx.c@ec4a87

Category: Spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.