当前位置: 代码迷 >> 综合 >> 简析 @Component @Autowired @Resource @Named等等的用法
  详细解决方案

简析 @Component @Autowired @Resource @Named等等的用法

热度:35   发布时间:2024-01-04 10:02:23.0

本文参考自简析 @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
  相关解决方案