<context:annotation-config><context:component-scan>是Spring Core里面的两个基础概念,每个使用者都有必要理解怎么使用它们以及这两个概念之间的区别。

annotation-config : Annotation config 的主要任务是激活注解。关键点是仍然需要注册bean。
component-scan : Component scan 能做任何annotaton config 能做的事情, 额外的还能够通过@Component , @Service ,@Repository etc 这些注解注册java bean。

还是来看代码比较直观:

SuperUser.java:

package com.lotusmedia.typesuper;

import com.lotusmedia.typenormal.NormalUser;
import com.lotusmedia.typenormal.TempUser;

public class SuperUser {

    private NormalUser normalUser;
    private TempUser tempUser;

    public SuperUser(){
        System.out.println("Super User Created ->"+this);
    }

    public void setNormalUser(NormalUser normalUser) {
        System.out.println("Setting Normal User ->"+normalUser);
        this.normalUser = normalUser;
    }

    public void setTempUser(TempUser tempUser) {
        System.out.println("Setting Temp User ->"+tempUser);
        this.tempUser = tempUser;
    }
}

NormalUser.java :

package com.lotusmedia.typenormal;

public class NormalUser {

    public NormalUser(){
        System.out.println("Normal User Created->"+this);
    }
}

TempUser.java :

package com.lotusmedia.typenormal;

public class TempUser {

    public TempUser(){
        System.out.println("Temporary User Created->"+this);
    }
}

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
       ">

 <bean id="normalUser" class="com.lotusmedia.typenormal.NormalUser"></bean>
 <bean id="tempUser" class="com.lotusmedia.typenormal.TempUser"></bean>

 <bean id="superUser" class="com.lotusmedia.typesuper.SuperUser">
    <property name="normalUser" ref="normalUser"></property>
    <property name="tempUser" ref="tempUser"></property>
 </bean>
</beans>

Executor.java :

package com.lotusmedia.run;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Executor {

    public static void main(String args[]){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    }
}

执行程序,我们可以看到我们期望的输出如下:

Normal User Created->com.lotusmedia.typenormal.NormalUser@75e845c2
Temporary User Created->com.lotusmedia.typenormal.TempUser@1cec6b00
Super User Created ->com.lotusmedia.typesuper.SuperUser@6564dbd5
Setting Normal User ->com.lotusmedia.typenormal.NormalUser@75e845c2
Setting Temp User ->com.lotusmedia.typenormal.TempUser@1cec6b00

接下去我们采用最小替换法来观察并理解这里的变化。

package com.lotusmedia.typesuper;

import org.springframework.beans.factory.annotation.Autowired;

import com.lotusmedia.typenormal.NormalUser;
import com.lotusmedia.typenormal.TempUser;

public class SuperUser {

    private NormalUser normalUser;
    private TempUser tempUser;

    public SuperUser(){
        System.out.println("Super User Created ->"+this);
    }
        @Autowired
    public void setNormalUser(NormalUser normalUser) {
        System.out.println("Setting Normal User ->"+normalUser);
        this.normalUser = normalUser;
    }
        @Autowired
    public void setTempUser(TempUser tempUser) {
        System.out.println("Setting Temp User ->"+tempUser);
        this.tempUser = tempUser;
    }
}

在配置文件里,我们移除属性的注入:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
       ">

 <bean id="normalUser" class="com.lotusmedia.typenormal.NormalUser"></bean>
 <bean id="tempUser" class="com.lotusmedia.typenormal.TempUser"></bean>

 <bean id="superUser" class="com.lotusmedia.typesuper.SuperUser"></bean>

</beans>

现在我们执行查看输出:

Normal User Created->com.lotusmedia.typenormal.NormalUser@4cc39a20
Temporary User Created->com.lotusmedia.typenormal.TempUser@485fcf29
Super User Created ->com.lotusmedia.typesuper.SuperUser@a19b1de

我们发现属性注入并没有被执行。这是因为虽然我们加了注解,但是注解的功能没有被开启,所以什么都不会发生。我们修改配置文件。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
       ">

 <context:annotation-config/>
 <bean id="normalUser" class="com.lotusmedia.typenormal.NormalUser"></bean>
 <bean id="tempUser" class="com.lotusmedia.typenormal.TempUser"></bean>

 <bean id="superUser" class="com.lotusmedia.typesuper.SuperUser"></bean>

</beans>

这次执行我们得到了期望的结果:

Normal User Created->com.lotusmedia.typenormal.NormalUser@679bfb30
Temporary User Created->com.lotusmedia.typenormal.TempUser@7977b9b
Super User Created ->com.lotusmedia.typesuper.SuperUser@37fd6bea
Setting Normal User ->com.lotusmedia.typenormal.NormalUser@679bfb30
Setting Temp User ->com.lotusmedia.typenormal.TempUser@7977b9b

目前看上去不错,现在让我们开始使用@Compent的注解。首先移除配置文件中bean的注册。
applicationContext.xml :

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
       ">
 <context:annotation-config/>
 </beans>

SuperUser.java :

package com.lotusmedia.typesuper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lotusmedia.typenormal.NormalUser;
import com.lotusmedia.typenormal.TempUser;
@Component
public class SuperUser {

    private NormalUser normalUser;

    private TempUser tempUser;

    public SuperUser(){
        System.out.println("Super User Created ->"+this);
    }

    @Autowired
    public void setNormalUser(NormalUser normalUser) {
        System.out.println("Setting Normal User ->"+normalUser);
        this.normalUser = normalUser;
    }

    @Autowired
    public void setTempUser(TempUser tempUser) {
        System.out.println("Setting Temp User ->"+tempUser);
        this.tempUser = tempUser;
    }
}

NormalUser.java :

package com.lotusmedia.typenormal;

import org.springframework.stereotype.Component;

@Component
public class NormalUser {

    public NormalUser(){
        System.out.println("Normal User Created->"+this);
    }
}

TempUser.java :

package com.lotusmedia.typenormal;

import org.springframework.stereotype.Component;

@Component
public class TempUser {

    public TempUser(){
        System.out.println("Temporary User Created->"+this);
    }
}

再次执行程序,我们发现bean都没有,更别说里面的属性了,也就是什么注入都没有执行。这个时候我们就知道annotation-config对@Compent是不起作用的。该是compents scan出场的时刻了。

修改配置文件,打开compents-scan

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
       ">

     <context:component-scan base-package="com.lotusmedia.typenormal,com.lotusmedia.typesuper"/>
</beans>

再执行就ok了。

Super User Created ->com.lotusmedia.typesuper.SuperUser@426295eb
Normal User Created->com.lotusmedia.typenormal.NormalUser@207f5580
Setting Normal User ->com.lotusmedia.typenormal.NormalUser@207f5580
Temporary User Created->com.lotusmedia.typenormal.TempUser@4f4db0e3
Setting Temp User ->com.lotusmedia.typenormal.TempUser@4f4db0e3

再一次印证了上面的小结,conpents scan能做所有annotaion config 能够做的事情。

annotation-config 和 component-scan 的区别的更多相关文章

  1. [Spring Boot] Use Component Scan to scan for Bean

    Component Scan is important concept when we want to create Bean. Currently we know what, for the cla ...

  2. org.springframework.stereotype.Service和com.alibaba.dubbo.config.annotation.Service两种service的区别

    这两个Service,都可以在service类头上使用@Service的注解,于是我就写错了,查了半天才发现.他们的区别大概是这个样子的: org.springframework.stereotype ...

  3. Spring ---annotation (重点)--Resource, Component 重要!!!

    beans.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="ht ...

  4. Spring中的注解@Service @Component @Controller @Repository区别

    @Service用于标注业务层组件, @Controller用于标注控制层组件(如struts中的action), @Repository用于标注数据访问组件,即DAO组件, @Component泛指 ...

  5. jmeter(四)参数化之CSV Data Set Config 和 CSVRead函数的区别

    jmeter的参数化可以添加配置元件CSV Data Set Config,也可以使用函数助手CSVRead.下面我就介绍一下2者的区别. 1.CSVRead函数,有2个参数值(路径和序号): 特点: ...

  6. component和bean区别

    @Component and @Bean do two quite different things, and shouldn't be confused. @Component (and @Serv ...

  7. Index Seek和Index Scan的区别

    Index Seek是Sql Server执行查询语句时利用建立的索引进行查找,索引是B树结构,Sql Server先查找索引树的根节点,一级一级向下查找,在查找到相应叶子节点后,取出叶子节点的数据. ...

  8. iis / asp.net 使用 .config 和 .xml 文件的区别

    由于在项目中有同学使用后缀为 .xml 的文件作为配置文件,而配置文件中有一些敏感信息被记录,如接口地址,Token,甚至还有数据库连接字符串. 以前都没想过为何微软会使用.config 的后缀在作为 ...

  9. 微信小程序template模板与component组件的区别和使用

    前言: 除了component,微信小程序中还有另一种组件化你的方式template模板,这两者之间的区别是,template主要是展示,方法则需要在调用的页面中定义.而component组件则有自己 ...

  10. @configuration和@component之间的区别

    @configuration和@component之间的区别是:@Component注解的范围最广,所有类都可以注解,但是@Configuration注解一般注解在这样的类上:这个类里面有@Value ...

随机推荐

  1. javascript最容易混淆的作用域、提升、闭包

    一.函数作用域 1.函数作用域 就是作用域在一个“Function”里,属于这个函数的全部变量都可以在整个函数的范围内使用及复用. function foo(a) { var b = 2; funct ...

  2. jquery学习——遍历

    1.each() $(selector).each(function(index,element)) var arr = [ "a", "bb", " ...

  3. IP转换成LONG 的 问题

    如何将四个字段以点分开的IP网络址协议地址转换成整数呢?PHP里有这么一个函数ip2long.比如 <?php echo ip2long("10.2.1.3"); ?> ...

  4. CentOS用yum安装、配置MariaDB

    .创建/etc/yum.repos.d/MariaDB.repo文件,这里用到了刚刚发布正式版的10. [mariadb] name = MariaDB baseurl = http://yum.ma ...

  5. LINUX优化得很好的sysctl.conf配置

    最近找了个不错的sysctl.conf的优化参数,在网站响应上已经算不错了的,time超时连接据说几乎为0了.  系统:centos 5.x sysctl.conf配置参数: kernel.msgmn ...

  6. NPOI教程

    NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目. NPOI 官方网站:http://npoi.codeplex.com/( ...

  7. splice 很好用

    splice 类似一个 copy pos 和 delete的组合拳,用起来很方便. arrayObject.splice(index,howmany,item1,.....,itemX) // ind ...

  8. 解读Unity中的CG编写Shader系列十 (光滑的镜面反射(冯氏着色))

    前文完成了最基本的镜面反射着色器,单平行光源下的逐顶点着色(per-vertex lighting),又称为古罗着色(Gouraud shading).这篇文章作为后续讨论更光滑的镜面反射方式,逐像素 ...

  9. C Primer Plus_第三章_数据和C_复习题与编程练习

    Review long代替int类型变量的原因是什么? 在您的系统中,long可以容纳比int更大的数:如果您确实需要处理更大的值,那么使用一种在所有系统上都保证至少是32位的类型会使程序的可移植性更 ...

  10. JS里设定延时:js中SetInterval与setTimeout用法

     js中SetInterval与setTimeout用法 JS里设定延时: 使用SetInterval和设定延时函数setTimeout 很类似.setTimeout 运用在延迟一段时间,再进行某项操 ...