Spring中Bean的配置:基于注解的方式

组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件。

特定组件包括:

       -@Component:基本注解,标识一个受Spring管理的组件

       -@Respository:标识持久层组件

       -@Service:标识业务层组件

       -@Controller:标识表现层组件

对于扫描到的组件,Spring有默认的命名策略,使用非限定类名,第一个字母小写,也可以用注解中的value属性值标识组件的名称。在组件类上使用特定的注解之后,还需要在Spring的配置文件中声明<context:component-scan>,如下:

	<!-- 配置自动扫描的包: 需要加入 aop 对应的 jar 包 -->
	<context:component-scan base-package="com.atguigu.spring.annotation.generic"></context:component-scan>

  <context:component-scan>还会自动注册AutowiredAnnotationBeanPostProcessor实例,该实例了可以自动装配具有@Autowire和@Resource@Inject注解的属性。

Spring允许通过<import>将多个配置文件引入到一个文件中,进行配置文件的集成。这样在IOC容器启动时,仅需要指定这个合并好的配置文件就可以了。

原文地址:https://www.cnblogs.com/hujingwei/p/5343791.html