组件扫描(component scanning): Spring 能够从 classpath 下自动扫描, 侦测和实例化具有特定注解的组件.
特定组件包括:
@Component: 基本注解, 标识了一个受 Spring 管理的组件
@Respository: 一般标识持久层组件
@Service: 一般标识服务层(业务层)组件
@Controller: 一般标识表现层组件
特定组件包括:
@Component: 基本注解, 标识了一个受 Spring 管理的组件
@Respository: 一般标识持久层组件
@Service: 一般标识服务层(业务层)组件
@Controller: 一般标识表现层组件
对于扫描到的组件, Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称
如下代码示例:
- 使用@Component标识一个bean,在不知道这个bean是干什么的时候
@Component
public class Province {
private int id;
private String priviceNumber;
private String priviceName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPriviceNumber() {
return priviceNumber;
}
public void setPriviceNumber(String priviceNumber) {
this.priviceNumber = priviceNumber;
}
public String getPriviceName() {
return priviceName;
}
public void setPriviceName(String priviceName) {
this.priviceName = priviceName;
}
public Province() {
}
public Province(int id, String priviceNumber, String priviceName) {
this.id = id;
this.priviceNumber = priviceNumber;
this.priviceName = priviceName;
}
@Override
public String toString() {
return String.format("Province{id=%d, priviceNumber='%s', priviceName='%s'}", id, priviceNumber, priviceName);
}
}
2. 一个普通的dao层
public interface ProvinceDao {
public void say();
}
import com.dadi.dao.ProvinceDao;
import org.springframework.stereotype.Repository;
@Repository(value = "provinceDao")
public class ProvinceDaoImpl implements ProvinceDao {
@Override
public void say() {
System.out.println("ProvinceDaoImpl...say");
}
}
3. 一个普通的servicen层:@Autowired注解相当于对应的属性创建了一个set方法
public interface ProvinceService {
public void say();
}
import com.dadi.dao.ProvinceDao;
import com.dadi.service.ProvinceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service(value = "provinceService")
public class ProvinceServiceImpl implements ProvinceService {
@Autowired
private ProvinceDao provinceDao;
@Override
public void say() {
System.out.println("ProviceServiceImpl...say进入");
provinceDao.say();
}
}
4.一个普通的controller层
@Controller
public class ControllerProvince {
@Autowired
private ProvinceService provinceService;
public void say(){
provinceService.say();
}
}
5. spring的配置文件:applicationContext.xml,其中下面这个配置是自动扫描指定包下面的bean加载到IOC容器中的意思
<context:component-scan base-package="com.dadi"></context:component-scan>
<?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">
<!-- 自动扫描指定路径下面标注注解的bean -->
<context:component-scan base-package="com.dadi"></context:component-scan>
</beans>
6. 测试方法
@Test
public void test() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
ControllerProvince controllerProvince = (ControllerProvince) ac.getBean("controllerProvince");
controllerProvince.say();
System.out.println("///");
String[] beanDefinitionNames = ac.getBeanDefinitionNames();
Arrays.stream(beanDefinitionNames).forEach(System.out::println);
}
7.运行结果