@Component诠注

@Component注解

Spring自带的@Component注解及扩展

 

@Component:定义Spring管理Bean

 

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

@Component  
@Aspect  
public class TestAspect {  
    @Pointcut(value="execution(* *(..))")  
    private void pointcut() {}  
	
    @Before(value="pointcut()")  
    public void before() {  
        System.out.println("=======before");  
    }  
}  

 

通过@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)实现跨越多个方法实现事务传播。