服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|

服务器之家 - 编程语言 - JAVA教程 - 详解Spring框架注解扫描开启之配置细节

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

2020-12-15 15:09Javahih JAVA教程

本篇文章主要介绍了详解Spring框架注解扫描开启之配置细节,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

前言

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

?
1
2
3
4
5
6
7
package com.test;
pubic class B{
 
  public B(){
    System.out.println("B类");
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
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注册

?
1
2
3
4
<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配置注解开启方式

?
1
2
3
4
5
6
7
package com.test;
pubic class B{
 
  public B(){
    System.out.println("B类");
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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,不开启扫描

?
1
2
<bean id="bBean"class="com.test.B"/>
<bean id="aBean"class="com.test.A"/>

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

?
1
<context:annotation-config/>

这时加载Application.xml配置

输出:
类B
类A

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

?
1
2
3
4
5
@Autowired
  public void setBClass(B bClass){
    this.bClass = bClass;
    System.out.println("通过set的方式注入B类");
  }

解决方法:

修改Application.xml配置文件

?
1
2
3
<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配置注解开启方式

?
1
2
3
4
5
6
7
package com.test;
pubic class B{
 
  public B(){
    System.out.println("B类");
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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扫描

?
1
<context:annotation-config />

加载配置文件:

输出:
类B
类A

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

修改配置文件:

?
1
<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等注入注解只会被注入一次,也即只加载一次

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.jianshu.com/p/cd0091aacf89

延伸 · 阅读

精彩推荐