zoukankan      html  css  js  c++  java
  • spring杂记

    AOP(Aspect-Oriented Programming,面向切面编程)是一种编程思想,并不是一种具体的实现,谈到实现一般有Filter和代理模式两种常见的使用方式。Java中常见的AOP技术有两个,分别是Filter和代理模式(也可以称为过滤器和拦截器),Filter是基于回调函数(请看《Java回调机制解析》),代理模式是基于Java反射技术,代理模式又分为静态代理和动态代理,动态代理就是拦截器的简单实现。(过滤器和拦截器的区别可参见《过滤器和拦截器的区别》)他们各自实现的功能不同,原理如出一辙。

    Spring AOP’s approach to AOP differs from that of most other AOP frameworks. The aim is not to provide the most complete AOP implementation (although Spring AOP is quite capable); it is rather to provide a close integration between AOP implementation and Spring IoC to help solve common problems in enterprise applications.

    Thus, for example, the Spring Framework’s AOP functionality is normally used in conjunction with the Spring IoC container.

    spring AOP是为结合IoC容器设计的,不是为了提供全面的AOP功能。

    Due to the proxy-based nature of Spring’s AOP framework, protected methods are by definition not intercepted, neither for JDK proxies (where this isn’t applicable) nor for CGLIB proxies (where this is technically possible but not recommendable for AOP purposes). As a consequence, any given pointcut will be matched against public methods only!

    If your interception needs include protected/private methods or even constructors, consider the use of Spring-driven native AspectJ weaving instead of Spring’s proxy-based AOP framework. 

    spring AOP不能用于“私有/保护”成员函数以及“构造函数”。

    @Pointcut("execution(public * *(..))")
    private void anyPublicOperation() {}
    
    @Pointcut("within(com.xyz.someapp.trading..*)")
    private void inTrading() {}
    
    @Pointcut("anyPublicOperation() && inTrading()")
    private void tradingOperation() {}

    It is a best practice to build more complex pointcut expressions out of smaller named components as shown above. When referring to pointcuts by name, normal Java visibility rules apply (you can see private pointcuts in the same type, protected pointcuts in the hierarchy, public pointcuts anywhere and so on). Visibility does not affect pointcut matching.

    建议复杂的pointcut使用简单的pointcut的逻辑组合。pointcut的private/protected/public影响pointcut的可见性(pointcut调用另一个pointcut),但不影响匹配时的可见性。

    AspectJ follows Java’s rule that annotations on interfaces are not inherited.

    注解一定要放在实现类上,因为在接口上的注解不会被实现类继承。

    使用@Transaction注解事务管理需要特别注意以下几点:

    • 如果在接口、实现类或方法上都指定了@Transactional 注解,则优先级顺序为方法>实现类>接口;
    • 建议只在实现类或实现类的方法上使用@Transactional,而不要在接口上使用,这是因为如果使用JDK代理机制是没问题,因为其使用基于接口的代理;而使用使用CGLIB代理机制时就会遇到问题,因为其使用基于类的代理而不是接口,这是因为接口上的@Transactional注解是“不能继承的”
    • 在JDK代理机制下,“自我调用”同样不会应用相应的事务属性,其语义和<tx:tags>中一样;
    • 默认只对RuntimeException异常回滚;
    • 在使用Spring代理时,默认只有在public可见度的方法的@Transactional 注解才是有效的,其它可见度(protected、private、包可见)的方法上即使有@Transactional 注解也不会应用这些事务属性的,Spring也不会报错,如果你非要使用非公共方法注解事务管理的话,可考虑使用AspectJ。
  • 相关阅读:
    SerializationUtility
    ExtendHelper
    AutoTransformHandler
    Parameter Config
    Tools Function
    谈谈对C#中反射的一些理解和认识(上)
    WPF程序中App.Config文件的读与写
    WPF实现Windows资源管理器(附源码)
    安装VC++2015运行库时出现0x80240037错误
    对C#调用C++的dll的一点思考
  • 原文地址:https://www.cnblogs.com/drizzlewithwind/p/5778327.html
Copyright © 2011-2022 走看看