详解Spring框架注解扫描开启之配置细节

今天通过做的一个结合ssh的crm项目,最后用到了spring注解配置,最后需要开启配置,查找了很长时间,找了一篇比较有质量的博客文章,就转载过来,希望能够给与大家一定帮助,另外也希望博主的文章有更多的人看到。

原文地址:https://blog.csdn.net/u014427391/article/details/72722797

原文地址:https://www.jb51.net/article/121113.htm

前言

Spring框架对Bean进行装配提供了很灵活的方式,下面归纳一下主要的方式:

  • 在XML中进行显示配置
  • 在Java中进行显示配置
  • 隐式的bean发现机制和自动装配

而自动装配实现就需要注解扫描,这时发现了两种开启注解扫描的方式,即<context:annotation-config/>和<context:component-scan>

下面归纳一下这两种方式的异同点:

<context:annotation-config>:注解扫描是针对已经在Spring容器里注册过的Bean

<context:component-scan>:不仅具备<context:annotation-config>的所有功能,还可以在指定的package下面扫描对应的bean

Demo:

Demo:XML注册Bean方式

下面给出两个类,类A和类B

package com.test;

pubic class B{


  public B(){

    System.out.println("B类");

  }

}
 
package com.test;

public class A {

  private B bClass;


  public void setBClass(B bClass){

    this.bClass = bClass;

    System.out.println("通过set的方式注入B类");

  }


  public A(){

    System.out.println("A类");

  }

}
 

如何我们这时可以通过传统的xml配置在Application.xml里进行bean注册

<bean id="bBean"class="com.test.B"/>

<bean id="aBean"class="com.test.A">

 <property name="bClass" ref="bBean"/>

</bean>
 

启动加载Application.xml

输出:
类B
类A
通过set的方式注入B类

Demo:annotation配置注解开启方式

package com.test;

pubic class B{


  public B(){

    System.out.println("B类");

  }

}
 
package com.test;

public class A {

  private B bClass;


  @Autowired

  public void setBClass(B bClass){

    this.bClass = bClass;

    System.out.println("通过set的方式注入B类");

  }


  public A(){

    System.out.println("A类");

  }

}
 

然后我们需要在Application.xml里注册Bean,假如我们先这样配置,仅仅注册Bean,不开启扫描

<bean id="bBean"class="com.test.B"/>

<bean id="aBean"class="com.test.A"/>
 

或者仅仅开启扫描,不注册Bean

<context:annotation-config/>

这时加载Application.xml配置

输出:
类B
类A

我们会发现下面的@Autowired的方法是不能被加载的

@Autowired

  public void setBClass(B bClass){

    this.bClass = bClass;

    System.out.println("通过set的方式注入B类");

  }
 

解决方法:

修改Application.xml配置文件

<context:annotation-config/>

<bean id="bBean"class="com.test.B"/>

<bean id="aBean"class="com.test.A"/>
 

重新加载配置文件,输出正常了

输出:
类B
类A
通过set的方式注入B类

归纳:<context:annotation-config>:注解扫描是针对已经在Spring容器里注册过的Bean

Demo:component配置注解开启方式

package com.test;

pubic class B{


  public B(){

    System.out.println("B类");

  }

}
 
package com.test;

@Component

public class A {

  private B bClass;


  @Autowired

  public void setBClass(B bClass){

    this.bClass = bClass;

    System.out.println("通过set的方式注入B类");

  }


  public A(){

    System.out.println("A类");

  }

}
 

然后我们配置一下application.xml,开启annotaion-config扫描

<context:annotation-config />

加载配置文件:

输出:
类B
类A

原因:<context:annotation-config>:注解扫描是针对已经在Spring容器里注册过的Bean,Bean并没有注册过,所以即使开启了@Autowired、@Component注解 和配置开启了annotaion-config扫描还是加载不到

修改配置文件:

<context:component-scan base-package="com.test"/>

重新加载配置文件:

输出:
类B
类A
通过set的方式注入B类

归纳:

<context:annotation-config>:注解扫描是针对已经在Spring容器里注册过的Bean

<context:component-scan>:不仅具备<context:annotation-config>的所有功能,还可以在指定的package下面扫描对应的bean

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

即使注册Bean,同时开启<context:annotation-config />扫描,@autowire,@resource等注入注解只会被注入一次,也即只加载一次

以上就是本文的全部内容,希望对大家的学习有所帮助

猜你喜欢

转载自blog.csdn.net/Angel_guoo/article/details/84726427