spring几个相似注解的区别

1、Component、Service、Controller、Repository

这几个注解都能注册bean,只是做了一个分层:

Service:业务层;Controller控制层;Repository dao层;如果不确定属于那一层则用Component

 

2、 属性注入Resource和Autowire

Autowire默认按照类型装配,默认要求装配的对象必须存在,如果不存在需要require为false;

Resource默认按照名称装配,如果找不到该name对应的bean,才按照类型装配,当注解在字段上时默认按照字段名称寻找bean,当注解在setter方法上时按照属性名称寻找bean,如果指定了name属性,那么只能按照名称装配,推荐使用!

 

posted on 2017-02-28 20:34  _故乡的原风景  阅读(236)  评论(0编辑  收藏  举报