Spring注解小结

Spring是怎么知道要管理这个Dao或Service的?

  让Spring用注解方式在Impl的来管理事务的步骤:

  1. 在spring-hibernate.xml 配置好扫描方式:

         <!—自动扫描Dao和Service包(自动注入)-->

         <context:component-scan base-package=”sy.dao,sy.service” /> 含义:Spring自动扫描这两个包及其下面的所有文件,如果这两个包下面的文件里包含     @Repository、@Service、@Component说明这个文件需要有Spring来管理。实际就是生成了<Bean>…</Bean>

      2.在spring-hibernate.xml 配置SessionFactory;

      3.在Impl的文件的类名上加入注解(Spring扫描后使之变为Bean):

         1)  Dao的Impl类名上注解为@Repository;

         2)  Service的Impl类名上的注解为@Service;

         3)  如果不好界定是Dao或Service的,注解可以为@Component;

      4.注入Session:

         1)  在类里声明 private SessionFactory sessionFactory ;

         2)    设置setSessionFactory方法,在set方法上设置注解@AutoWired 把sessionFactory注入进来 ;

 

   另外,说明一点,事务是在service层的impl上配置而不是Dao

posted @ 2014-10-05 20:54  孤独的战斗笋  阅读(226)  评论(0编辑  收藏  举报