注:网上很多帖子都是包含案例的讲解,长篇大论非常麻烦,而我更喜欢直接了解知识点的用法与结构,简洁,所以本帖无任何测试案例,只为整理注解的功能与用法
1.Spring家族注解
1.1 @Component、@Repository、@Service、@Controller区别
如果 Web 应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用 @Repository、@Service 和 @Controller 对分层中的类进行注释,而用 @Component 对那些比较中立的类进行注释,@Repository是作用在Mapper(Dao)层上的,由于引入Mybatis后,sql都写在了xml文件中,所以@Repository注解可以不使用。 。
@Repository //持久层Pojo
@Controller //控制层Controller
@Service //业务层Service
@Component //无法确定是否为以上三类是使用
使用@Component、@Repository、@Service、@Controller注解的前提 - - 配置包扫描
在spring的配置文件application.xml中添加如下配置
<context:component-scan base-package="xxx.xxx.*"/>
<-- base-package属性值即为想要扫描的包 -->