前言
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等注入注解只会被注入一次,也即只加载一次
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://www.jianshu.com/p/cd0091aacf89
相关文章
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-25 41
-
2025-05-25 41
-
2025-05-25 35
-
2025-05-27 62
-
2025-05-29 53