zak*_*zak 3 spring spring-aop dynamic-proxy cglib spring-transactions
我的问题是关于内部方法调用的 AOP Spring 行为。
@Service
class Service {
@Transactional
public void method1() {
method1();
}
@Transactional
public void method2() {}
}
Run Code Online (Sandbox Code Playgroud)
如果我们从外部调用method1(),method1()会以事务模式执行,但由于内部调用method2(),method2()内部的代码不会以事务模式执行。
同时,对于 Configuration 类,通常我们应该有相同的行为:
@Configuration
class MyConfiguration{
@Bean
public Object1 bean1() {
return new Object1();
}
@Bean
public Object1 bean2() {
Object1 b1 = bean1();
return new Object2(b1);
}
}
Run Code Online (Sandbox Code Playgroud)
通常,如果我理解得很好,从 bean2() 调用 bean1() 方法不应该被代理对象拦截,因此,如果我们多次调用 bean1(),我们每次都应该得到不同的对象。
首先,您能否从技术上解释为什么代理对象没有拦截内部调用,其次检查我对第二个示例的理解是否正确。
@Component小号有关普通 Spring (AOP) 代理或动态代理 (JDK、CGLIB) 在一般情况下如何工作的说明,请参阅我的其他答案以及说明性示例代码。先阅读一下,你就会明白为什么不能通过 Spring AOP 拦截这些类型的代理的自调用。
@Configuration 班级至于@Configuration类,它们的工作方式不同。为了避免已经创建的 Spring bean 仅仅因为它们的@Bean工厂方法被再次外部或内部调用而再次创建,Spring 为它们创建了特殊的 CGLIB 代理。
我的配置类之一如下所示:
package spring.aop;
import org.springframework.context.annotation.*;
@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class ApplicationConfig {
@Bean(name = "myInterfaceWDM")
public MyInterfaceWithDefaultMethod myInterfaceWithDefaultMethod() {
MyClassImplementingInterfaceWithDefaultMethod myBean = new MyClassImplementingInterfaceWithDefaultMethod();
System.out.println("Creating bean: " + myBean);
return myBean;
}
@Bean(name = "myTestBean")
public Object myTestBean() {
System.out.println(this);
myInterfaceWithDefaultMethod();
myInterfaceWithDefaultMethod();
return myInterfaceWithDefaultMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
相应的应用程序如下所示:
package spring.aop;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext appContext = SpringApplication.run(DemoApplication.class, args);
MyInterfaceWithDefaultMethod myInterfaceWithDefaultMethod =
(MyInterfaceWithDefaultMethod) appContext.getBean("myInterfaceWDM");
System.out.println(appContext.getBean("myTestBean"));
}
}
Run Code Online (Sandbox Code Playgroud)
此打印件(已编辑以删除我们不想看到的内容):
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.2.RELEASE)
2019-07-07 08:37:55.750 INFO 22656 --- [ main] spring.aop.DemoApplication : Starting DemoApplication on (...)
(...)
Creating bean: spring.aop.MyClassImplementingInterfaceWithDefaultMethod@7173ae5b
spring.aop.ApplicationConfig$$EnhancerBySpringCGLIB$$8b4ed8a@72456279
Run Code Online (Sandbox Code Playgroud)
运行应用程序时,myInterfaceWithDefaultMethod()即使在myTestBean(). 为什么?
如果您在其中的一个myInterfaceWithDefaultMethod()调用上放置一个断点myTestBean()并让调试器在那里停止,您将了解更多信息。然后您可以通过评估代码来检查情况:
System.out.println(this);
spring.aop.ApplicationConfig$$EnhancerBySpringCGLIB$$8b4ed8a@72456279
Run Code Online (Sandbox Code Playgroud)
所以 config 类确实是一个 CGLIB 代理。但是它有哪些方法呢?
for (Method method: this.getClass().getDeclaredMethods()) {
System.out.println(method);
}
public final java.lang.Object spring.aop.ApplicationConfig$$EnhancerBySpringCGLIB$$8b4ed8a.myTestBean()
public final spring.aop.MyInterfaceWithDefaultMethod spring.aop.ApplicationConfig$$EnhancerBySpringCGLIB$$8b4ed8a.myInterfaceWithDefaultMethod()
public final void spring.aop.ApplicationConfig$$EnhancerBySpringCGLIB$$8b4ed8a.setBeanFactory(org.springframework.beans.factory.BeanFactory) throws org.springframework.beans.BeansException
final spring.aop.MyInterfaceWithDefaultMethod spring.aop.ApplicationConfig$$EnhancerBySpringCGLIB$$8b4ed8a.CGLIB$myInterfaceWithDefaultMethod$1()
public static void spring.aop.ApplicationConfig$$EnhancerBySpringCGLIB$$8b4ed8a.CGLIB$SET_THREAD_CALLBACKS(org.springframework.cglib.proxy.Callback[])
public static void spring.aop.ApplicationConfig$$EnhancerBySpringCGLIB$$8b4ed8a.CGLIB$SET_STATIC_CALLBACKS(org.springframework.cglib.proxy.Callback[])
public static org.springframework.cglib.proxy.MethodProxy spring.aop.ApplicationConfig$$EnhancerBySpringCGLIB$$8b4ed8a.CGLIB$findMethodProxy(org.springframework.cglib.core.Signature)
final void spring.aop.ApplicationConfig$$EnhancerBySpringCGLIB$$8b4ed8a.CGLIB$setBeanFactory$6(org.springframework.beans.factory.BeanFactory) throws org.springframework.beans.BeansException
static void spring.aop.ApplicationConfig$$EnhancerBySpringCGLIB$$8b4ed8a.CGLIB$STATICHOOK4()
private static final void spring.aop.ApplicationConfig$$EnhancerBySpringCGLIB$$8b4ed8a.CGLIB$BIND_CALLBACKS(java.lang.Object)
final java.lang.Object spring.aop.ApplicationConfig$$EnhancerBySpringCGLIB$$8b4ed8a.CGLIB$myTestBean$0()
static void spring.aop.ApplicationConfig$$EnhancerBySpringCGLIB$$8b4ed8a.CGLIB$STATICHOOK3()
Run Code Online (Sandbox Code Playgroud)
这看起来有点乱,让我们打印方法名称:
for (Method method: this.getClass().getDeclaredMethods()) {
System.out.println(method.name);
}
myTestBean
myInterfaceWithDefaultMethod
setBeanFactory
CGLIB$myInterfaceWithDefaultMethod$1
CGLIB$SET_THREAD_CALLBACKS
CGLIB$SET_STATIC_CALLBACKS
CGLIB$findMethodProxy
CGLIB$setBeanFactory$6
CGLIB$STATICHOOK4
CGLIB$BIND_CALLBACKS
CGLIB$myTestBean$0
CGLIB$STATICHOOK3
Run Code Online (Sandbox Code Playgroud)
该代理是否实现了任何接口?
for (Class<?> implementedInterface : this.getClass().getInterfaces()) {
System.out.println(implementedInterface);
}
interface org.springframework.context.annotation.ConfigurationClassEnhancer$EnhancedConfiguration
Run Code Online (Sandbox Code Playgroud)
不错,有意思。让我们阅读一些 Javadoc。实际上类ConfigurationClassEnhancer是包范围的,所以我们必须阅读源代码中的 Javadoc :
通过生成与 Spring 容器交互的 CGLIB 子类来增强配置类,以尊重 @Bean 方法的 bean 范围语义。每个这样的@Bean 方法都将在生成的子类中被覆盖,只有在容器实际请求构造新实例时才委托给实际的 @Bean 方法实现。否则,对此类@Bean 方法的调用将作为对容器的引用,通过名称获取相应的 bean。
内部接口EnhancedConfiguration实际上是公开的,但 Javadoc 仍然只在源代码中:
所有@Configuration CGLIB 子类要实现的标记接口。通过检查候选类是否已经可以分配给它,例如已经被增强,从而促进用于增强的幂等行为。还扩展了 BeanFactoryAware,因为所有增强的 @Configuration 类都需要访问创建它们的 BeanFactory。
请注意,此接口仅供框架内部使用,但必须保持公共状态,以允许访问从其他包(即用户代码)生成的子类。
现在,如果我们进入myInterfaceWithDefaultMethod()呼叫,我们会看到什么?生成的代理方法调用方法ConfigurationClassEnhancer.BeanMethodInterceptor.intercept(..),该方法的Javadoc说:
增强 @Bean 方法以检查提供的 BeanFactory 是否存在此 bean 对象。
在那里你可以看到其余的魔法正在发生,但描述真的超出了这个已经很长的答案的范围。