Spring 注解自动装配Bean

引言

上文:Spring自动装配Bean实现hello world 介绍了Spring使用xml配置实现自动装配Bean,但是通过xml配置自动装配的方式,会显得比较累赘、比较麻烦,那接下来的注解自动装配将会改善这个问题。

开启自动化装配Bean

通过xml开启

需要开启注解装配功能,则需要在web.xml中加入配置<context:annotation-config />或者< component-scan />,配置代码的如下。

1
2
3
4
5
6
7
8
9
10
11
<beans 
//...
xmlns:context="http://www.springframework.org/schema/context"
//...
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
//...

<context:annotation-config /> //或者<component-scan />
//...
</beans>

这两者的区别如下:

<context:annotation-config>:
: 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式),是对已注册Bean的进行操作的配置,也就是说,Bean需要首先通过某种方式(比如Xml配置,或者其他注解)被注册,然后使用这个配置,可以对已注册的Bean进行进一步操作(比如注入到某个类的内部),也就是说,这个配置是用于“激活”已注册的Bean的,让已注册的Bean开始工作。

<context:component-scan>:
: 除了具有<context:annotation-config>的功能之外,<context:component-scan>还可以在指定的package下扫描以及注册javabean

另外compontent-scan可以扫描类、方法、属性上面的注解; compontent-config只扫描属性上面的注解。

通过JAVA代码开启

创建Config.java,Config配置类,首先需要添加@Configuration ,声明这个类为配置类,然后添加@ComponentScan()
例如:

1
2
3
4
5
6
7
8
9
package cn.zhenta.www.service.impl.Config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan()
public class Config {
}

在这里插入图片描述
@Configuration
: 表明这个类是配置类,这个类相当于web.xml的作用。@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文)。

@ComponentScan()
: 作用是,开启组件扫描,去寻找带有@Component注解的类,并且为其创建Bean。ComponentScan()可以扫描指定的包,例如@ComponentScan(basePackages = "cn.zhenta.www.service.impl")则会扫描cn.zhenta.www.service.impl这个包以及这个包下的所有子包。

通过注解声明组件类

在需要声明为组件类的(只适用于类)上,添加@Component注解,这个注解表明该类会作为组件类(ComponentScan() 既通过这个注解扫描),并告知Spring要为这个类创建Bean。
例子:

1
2
3
4
5
6
7
8
9
10
11
package cn.zhenta.www.service.impl.TestA;

import org.springframework.stereotype.Component;

@Component()
public class TestA
{
public void pri(){
System.out.println("hi " );
}
}

在这里插入图片描述
其中不只有@Component这个注解,还有如下:
@Service用于标注业务层组件
@Controller用于标注控制层组件(如struts中的action)
@Repository用于标注数据访问组件,即DAO组件
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

通过注解实现自动装配

简单来说,自动装配就是让Spring自动满足Bean依赖的一种方法,在满足依赖的过程中,会在Spring应用上下文中寻找匹配某个Bean需求的其他Bean。为了声明要进行自动装配,我们可以借助Spring的@Autowired注解。

其中@Autowired可以作用在多个地方

  1. setter方法
  2. 构造方法
  3. 成员属性
  4. 任意方法

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package cn.zhenta.www.service.impl.TestB;

import cn.zhenta.www.service.impl.TestA.TestA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TestB {
@Autowired
private TestA testA;
public void priTestA(){
this.testA = testA;
testA.pri();
}
}

在这里插入图片描述

测试类

以上已经开启自动化装配、注解声明组件类、注解实现自动化装配,那么我们该如何在测试这些配置好了没有呢,这就需要用到我们的测试类了。首先创建一个TestC.java 作为我们的测试类,在类上添加@ContextConfiguration(classes = cn.zhenta.www.service.impl.TestB.TestB.class)其中classes = 你的配置类的命名空间,再添加@RunWith(SpringJUnit4ClassRunner.class),那这两个注解有什么作用呢?

@ContextConfiguration()
: @ContextConfiguration()注释标签是 Spring test context 提供的,用来指定 Spring 配置信息的来源,支持指定 XML 文件位置或者 Spring 配置类的命名空间。

@RunWith()
: @RunWith() 注释标签是 Junit 提供的,用来说明此测试类的运行者,这里用了 SpringJUnit4ClassRunner,这个类是一个针对 Junit 运行环境的自定义扩展,用来标准化在 Spring 环境中 Junit4.5 的测试用例,例如支持的注释标签的标准化。

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package cn.zhenta.www.service.impl.TestC;

import cn.zhenta.www.service.impl.TestB.TestB;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@ContextConfiguration(classes = cn.zhenta.www.service.impl.Config.Config.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class TestC {

@Autowired
private TestB testB = new TestB();

@Test
public void dd(){
testB.priTestA();
}

}

在这里插入图片描述

运行测试类,最后输出:hi。