spring4.0中 @Component @ComponentScan @Configuration @Bean @Autowired在自动装配中的使用


1:自动扫描

@Component 注解表面该类会作为组建类。并告知spring要为这个类创建bean ,需要注意的是组建扫描默认是不开启的。需要配置扫描,让扫描组建去扫描带有@Component的注解   

spring4.0中 @Component @ComponentScan @Configuration @Bean @Autowired在自动装配中的使用

@ComponentScan就是扫描组建,默认情况下该注解只会扫描同包中的注解。

spring4.0中 @Component @ComponentScan @Configuration @Bean @Autowired在自动装配中的使用




2:自动装配

@Bean注解是在javaConfig中的运用,正常情况下Bean要配合@Configuration 一起使用。

spring4.0中 @Component @ComponentScan @Configuration @Bean @Autowired在自动装配中的使用


3:  @Autowired注解是实例化bean中的对象,可以提供一个已经实例化好的对象。  @Autowired是从spring容器中获取的对象。