Content Table

Component Scan

使用 <context:component-scan> 特性,可以自动扫描 base-package 下类名有注解 @Component@Service 或者 @Controller 的类,为其在 Spring 容易里创建一个对象。

@Service@Controller@Component 在语法上作用是一样的,区别是各自有自己的语义,例如 SpringMVC 里用 @Controller 表明类是 Controller

spring-beans.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

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

House

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.xtuer.beans;

import org.springframework.stereotype.Component;

// 生成的 Bean 的 ID 是 house
@Component
public class House {
private String description;

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}
}

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import com.xtuer.beans.House;
import com.xtuer.util.CommonUtils;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ComponentScanTest {
private static ApplicationContext context;

@BeforeClass
public static void setup() {
context = new ClassPathXmlApplicationContext("spring-beans.xml");
}

@Test
public void test() {
House house1 = context.getBean(House.class);
CommonUtils.output(house1);

House house2 = context.getBean("house", House.class);
CommonUtils.output(house2);

Assert.assertEquals(house1, house2);
}
}

输出

1
2
3
4
5
6
{
"description" : null
}
{
"description" : null
}