spring常用注解

来源:互联网 发布:启明英语听说软件 编辑:程序博客网 时间:2024/06/07 06:56

Spring自带的@Component注解及扩展

 

@Component:定义Spring管理Bean

 

@AspectJ风格的切面可以通过@Compenent注解标识其为Spring管理Bean,而@Aspect注解不能被Spring自动识别并注册为Bean,必须通过@Component注解来完成

Java代码  收藏代码
  1. @Component    
  2. @Aspect    
  3. public class TestAspect {    
  4.     @Pointcut(value="execution(* *(..))")    
  5.     private void pointcut() {}    
  6.       
  7.     @Before(value="pointcut()")    
  8.     public void before() {    
  9.         System.out.println("=======before");    
  10.     }    
  11. }    

 

通过@Component将切面定义为Spring管理Bean。

 

@Repository

@Component扩展,被@Repository注解的POJO类表示DAO层实现,从而见到该注解就想到DAO层实现,使用方式和@Component相同;

 

@Service

@Component扩展,被@Service注解的POJO类表示Service层实现,从而见到该注解就想到Service层实现,使用方式和@Component相同;

 

@Controller

@Component扩展,被@Controller注解的类表示Web层实现,从而见到该注解就想到Web层实现,使用方式和@Component相同;

 

 

在使用Spring代理时,默认只有在public可见度的方法的@Transactional 注解才是有效的,其它可见度(protected、private、包可见)的方法上即使有@Transactional 注解也不会应用这些事务属性的,Spring也不会报错,如果你非要使用非公共方法注解事务管理的话,可考虑使用AspectJ。

 

 

Spring声明式事务实现其实就是Spring AOP+线程绑定实现,利用AOP实现开启和关闭事务,利用线程绑定(ThreadLocal)实现跨越多个方法实现事务传播。

原创粉丝点击