本文参考自简析 @Component @Autowired @Resource @Named等等的用法
查看原文: 原文地址
之前是了解了@Controller注解的用法,通过<context:component-scan />让Bean定义注解工作起来
(1)关于@Component
@Component是所有受Spring管理组件的通用形式,不好区别时用这个。
Spring还提供了更加细化的注解形式:
@Repository、 @Service、@Controller,
它们分别对应存储层Bean,业务层Bean,和展示层Bean。
<context:component-scan
base-package="com.etc" //去com.etc为前缀的所有包下下找到所有的类
resource-pattern
>
</context:component-scan>
(2)关于@Autowired的用法
,注入bean 写在applicationContext.xml里面
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
Spring 通过一个 BeanPostPro