context:annotation-config 和 context:component-scan 的区别

annotation-config和component-scan的区别

  • < context:annotation-config > 是用于激活那些已经在spring容器里注册过的bean

    (无论是通过xml的方式还是通过package sanning的方式)上面的注解

  • < context:component-scan >除了具有< context:annotation-config >的功能之外,

    < context:component-scan >还可以在指定的package下扫描以及注册javabean

有三个class A,B,C,并且B,C的对象被注入到A中

package com.zr.pojo;


public class B {
    public B() {
        System.out.println("creating bean B: " + this);
    }
}
package com.zr.pojo;

public class C {
    public C() {
        System.out.println("creating bean C: " + this);
    }
}
package com.zr.pojo1;

import com.zr.pojo.B;
import com.zr.pojo.C;

public class A {

    private B bbb;
    private C ccc;

    public A() {
        System.out.println("creating bean 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;
    }
}

在applicationContext.xml中加入下面的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="bBean" class="com.zr.pojo.B"/>
    <bean id="cBean" class="com.zr.pojo.C"/>

    <bean id="aBean" class="com.zr.pojo1.A">
        <property name="bbb" ref="bBean"/>
        <property name="ccc" ref="cBean"/>
    </bean>

</beans>

加载applicationContext.xml配置文件,将得到下面的结果:

creating bean B: com.zr.pojo.B@6483f5ae
creating bean C: com.zr.pojo.C@282003e1
creating bean A: com.zr.pojo1.A@7fad8c79
setting A.bbb with com.zr.pojo.B@6483f5ae
setting A.ccc with com.zr.pojo.C@282003e1

完全通过xml的方式,太过时了

下面通过注解的方式来简化我们的xml配置文件

首先,我们使用@Autowired的方式将对象bbb和ccc注入到A中

package com.zr.pojo1;

import com.zr.pojo.B;
import com.zr.pojo.C;
import org.springframework.beans.factory.annotation.Autowired;

public class A {

    private B bbb;
    private C ccc;

    public A() {
        System.out.println("creating bean 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;
    }

}

然后applicationContext.xml配置文件去除属性< property >

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="bBean" class="com.zr.pojo.B"/>
    <bean id="cBean" class="com.zr.pojo.C"/>
    <bean id="aBean" class="com.zr.pojo1.A"/>

</beans>

加载applicationContext.xml配置文件之后,得到下面的结果

creating bean B: com.zr.pojo.B@768b970c
creating bean C: com.zr.pojo.C@290dbf45
creating bean A: com.zr.pojo1.A@12028586

ClassA中显然没有注入属性,结果是错误的的,究竟是因为什么呢?为什么属性没有被注入进去呢?

是因为注解本身并不能够做任何事情,它们只是最基本的组成部分

我们需要能够处理这些注解的处理工具来处理这些注解

这就是< context:annotation-config > 所做的事情,用于激活那些已经在spring容器里注册过的bean的注解

将applicationContext.xml配置文件作如下修改

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    
    <context:annotation-config/>
    
    <bean id="bBean" class="com.zr.pojo.B"/>
    <bean id="cBean" class="com.zr.pojo.C"/>
    <bean id="aBean" class="com.zr.pojo1.A"/>

</beans>

加载applicationContext.xml配置文件之后,将得到下面的结果:

creating bean B: com.zr.pojo.B@36c88a32
creating bean C: com.zr.pojo.C@1d119efb
creating bean A: com.zr.pojo1.A@35047d03
setting A.ccc with com.zr.pojo.C@1d119efb
setting A.bbb with com.zr.pojo.B@36c88a32

结果正确了

但是如果将代码作如下修改

package com.zr.pojo;

import org.springframework.stereotype.Component;

@Component
public class B {
    public B() {
        System.out.println("creating bean B: " + this);
    }
}
package com.zr.pojo;

import org.springframework.stereotype.Component;

@Component
public class C {
    public C() {
        System.out.println("creating bean C: " + this);
    }
}
package com.zr.pojo1;

import com.zr.pojo.B;
import com.zr.pojo.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class A {

    private B bbb;

    private C ccc;

    public A() {
        System.out.println("creating bean 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;
    }

}

applicationContext.xml配置文件修改为:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config/>
</beans>

当加载applicationContext.xml配置文件之后,却没有任何输出,这是为什么呢?

因为<context:annotation-config />仅能够在已经在已经注册过的bean上面起作用

对于没有在spring容器中注册的bean,它并不能执行任何操作

但是不用担心,< context:component-scan >除了具有<context:annotation-config />的功能之外,还具有自动将带有@component,@service,@Repository等注解的对象注册到spring容器中的功能

我们将applicationContext.xml配置文件作如下修改:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.zr.pojo"/>
    <context:component-scan base-package="com.zr.pojo1"/>
</beans>

加载applicationContext.xml就会得到下面的结果:

creating bean B: com.zr.pojo.B@6b81ce95
creating bean C: com.zr.pojo.C@16293aa2
creating bean A: com.zr.pojo1.A@2d7275fc
setting A.bbb with com.zr.pojo.B@6b81ce95
setting A.ccc with com.zr.pojo.C@16293aa2

结果正确

回头看下applicationContext.xml文件,已经简化为两行context:component-scan了,是不是很简单?

那如果在applicationContext.xml手动加上下面的配置,也就是说

既在applicationContext.xml中手动的注册了A的实例对象,

同时,通过component-scan去扫描并注册B,C的对象,如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.zr.pojo"/>
    <bean id="aBean" class="com.zr.pojo1.A"/>
</beans>

结果正确:

creating bean B: com.zr.pojo.B@6b81ce95
creating bean C: com.zr.pojo.C@16293aa2
creating bean A: com.zr.pojo1.A@2d7275fc
setting A.ccc with com.zr.pojo.C@16293aa2
setting A.bbb with com.zr.pojo.B@6b81ce95

虽然class A并不是通过扫描的方式注册到容器中的 ,

但是< context:component-scan > 所产生的的处理那些注解的处理器工具,会处理所有绑定到容器上面的bean,不管是通过xml手动注册的还是通过scanning扫描注册的。

那么,如果我们通过下面的方式呢?我们既配置了<context:annotation-config />,又配置了<context:component-scan base-package=“com.xxx” />,它们都具有处理在容器中注册的bean里面的注解的功能。会不会出现重复注入的情况呢?

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config />
    <context:component-scan base-package="com.zr.pojo"/>
    <bean id="aBean" class="com.zr.pojo1.A"/>
</beans>

结果如下,没有出现:

creating bean B: com.zr.pojo.B@37afeb11
creating bean C: com.zr.pojo.C@2d7275fc
creating bean A: com.zr.pojo1.A@79e2c065
setting A.bbb with com.zr.pojo.B@37afeb11
setting A.ccc with com.zr.pojo.C@2d7275fc

因为<context:annotation-config />和 < context:component-scan >同时存在的时候,前者会被忽略

也就是那些@autowire,@resource等注入注解只会被注入一次

哪怕是你手动的注册了多个处理器,spring仍然只会处理一次:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config />
    <context:component-scan base-package="com.zr.pojo"/>
    <bean id="aBean" class="com.zr.pojo1.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" />
</beans>

结果仍是正确的:

creating bean B: com.zr.pojo.B@515aebb0
creating bean C: com.zr.pojo.C@399f45b1
creating bean A: com.zr.pojo1.A@3a93b025
setting A.ccc with com.zr.pojo.C@399f45b1
setting A.bbb with com.zr.pojo.B@515aebb0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值