1. Classpath扫描与组件管理

从Spring3.0开始,Spring JavaConfig项目提供了很多特性,包括使用java而不是XML定义bean。

比如@configuration, @Bean, @Import, @DependsOn

@Component是一个通用注解,可用于任何Bean

@Repository, @Service, @Controller是更有针对性的注解

——@Repository通常用于注解DAO类,即持久层

——@Service通常用于注解Service类,即服务层

——@Controller通常用于Controller类,即控制层(MVC)

 

2. 类的自动检测与注册Bean

Spring可以自动检测类并注册Bean到ApplicationContext中

 

3. <context:annotation-config/>

通过基于XML的Spring配置标签

<context:annotation-config/>仅会查找在同一个applicationContext中的bean注解

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

 

 

4. @Component, @Repository, @Service, @Controller

 

5. @Required

 

6. @Autowired

 

7. @Qualifier

 

8. @Resource