Spring 常用注解
-
@Repository
用于标注数据访问层,也可以说用于标注数据访问组件,即DAO组件。 -
@Service
用于标注服务层,主要用来进行业务的逻辑处理。 -
@Controller
用于标注控制层,相当于struts中的action层。 -
@Component
不属于@Controller、@Services、@Repository的时候,我们就可以用@Component来标注这个类。
@Controller、@Services、@Repository是对@Component的扩展。 -
@Autowired (中文: 自动连线)
注入时使用。 -
@Autowired
作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了。@Resource有两个属性是比较重要的,分是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。
参考: @Autowired 与@Resource的区别 -
相关链接
Spring注解@Component、@Repository、@Service、@Controller区别
Spring中@component的使用
Spring中@Component的作用
@Component注解的含义
@Autowired用法详解