Spring文档指出:
CGLIB代理仅拦截公共方法调用!不要打电话 这种代理上的非公开方法。他们没有被委派给 实际作用域目标对象。
但是经过观察,我认为我的实验(下面的代码)不好,或者这种行为随着时间而改变了。
我观察到只有最终方法或私有方法被绕过了。
这里是实验:(春季版:5.1.3
)
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
@Autowired
private StudentService studentService;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
StudentService studentService() {
ProxyFactory proxyFactory = new ProxyFactory(new StudentService());
proxyFactory.setProxyTargetClass(true);
proxyFactory.addAdvice(new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("method " + invocation.getMethod() + " is called on " +
invocation.getThis() + " with args " + invocation.getArguments());
Object ret = invocation.proceed();
System.out.println("method " + invocation.getMethod() + " returns " + ret);
return ret;
}
});
return (StudentService) proxyFactory.getProxy();
}
@Override
public void run(String... args) throws Exception {
studentService.doIt();
}
class StudentService {
void doIt() {
System.out.println("doIt");
}
}
输出:
method void com.example.demo.DemoApplication$StudentService.doIt() is called on com.example.demo.DemoApplication$StudentService@127a7a2e with args [Ljava.lang.Object;@14008db3
doIt
method void com.example.demo.DemoApplication$StudentService.doIt() returns null
-通过实验-CGLIB库(不使用spring,使用Enhancer类)也允许代理包级方法。
更新
我有另一个观察结果(与上面相反)。 在典型的jdbc应用程序中:
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
@Autowired
private ExampleService exampleService;
@Autowired
private XExampleService xExampleService;
@Autowired
private XXExampleService xxExampleService;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println(exampleService.getClass());
System.out.println(xExampleService.getClass());
System.out.println(xxExampleService.getClass());
}
@Service
class ExampleService {
void tranx() {
}
}
@Service
class XExampleService {
@org.springframework.transaction.annotation.Transactional
void tranx() {
}
}
@Service
class XXExampleService {
@Transactional
public void tranx() {
}
}
}
输出:
class com.example.demo.DemoApplication$ExampleService
class com.example.demo.DemoApplication$XExampleService
class com.example.demo.DemoApplication$XXExampleService$$EnhancerBySpringCGLIB$$2b1603e8
这意味着,如果在春季以我的行为创建代理-就像TransactionInterceptor中一样-CGLIB代理仅针对公共方法创建。
UPDATE2
我认为我发现这种接受公共方法的行为仅在哪里发生。
它发生在PointCut(AnnotationTransactionAttributeSource
)使用的TransactionAttributeSourcePointcut
上。
来自代码:
/**
* Create a custom AnnotationTransactionAttributeSource, supporting
* public methods that carry the {@code Transactional} annotation
* or the EJB3 {@link javax.ejb.TransactionAttribute} annotation.
* @param publicMethodsOnly whether to support public methods that carry
* the {@code Transactional} annotation only (typically for use
* with proxy-based AOP), or protected/private methods as well
* (typically used with AspectJ class weaving)
*/
public AnnotationTransactionAttributeSource(boolean publicMethodsOnly) {
答案 0 :(得分:0)
可能的答案是,由于基于Java的代理不能与package-private
方法一起使用(因为Java实现类不能为接口实现的方法分配较弱的访问特权)。
因此,即使CGLib可以代理它们,这也可能阻止spring团队使用程序包私有方法。