annotation-config 和 component-scan 的区别

<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 的区别)