Spring代理的创建时机
spring代理的创建时机分为两种,第一个是初始化之后创建代理,第二种是依赖注入之前创建,这种情况是因为发生了循环依赖,代理被提前创建。
1、初始化之后
package org.springframework.aop.framework.autoproxy;
import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
import org.springframework.context.support.GenericApplicationContext;import javax.annotation.PostConstruct;/*** @author zhou* @version 1.0* @description TODO* @date 2025/11/12 22:44*/
public class a1 {public static void main(String[] args) {GenericApplicationContext context = new GenericApplicationContext();context.registerBean(ConfigurationClassPostProcessor.class);context.registerBean(Config.class);context.refresh();context.close();}@Configurationstatic class Config{@Bean //解析@Aspect 产生代理public AnnotationAwareAspectJAutoProxyCreator annotationAwareAspectJAutoProxyCreator() {return new AnnotationAwareAspectJAutoProxyCreator();}@Bean //解析@PostConstructpublic CommonAnnotationBeanPostProcessor commonAnnotationBeanPostProcessor(){return new CommonAnnotationBeanPostProcessor();}@Bean //解析@Autowiredpublic AutowiredAnnotationBeanPostProcessor autowiredAnnotationBeanPostProcessor(){return new AutowiredAnnotationBeanPostProcessor();}@Beanpublic MethodInterceptor advice(){return invocation -> {System.out.println("before");Object proceed = invocation.proceed();System.out.println("after");return proceed;};}@Beanpublic Advisor advisor(MethodInterceptor advice){AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();pointcut.setExpression("execution(* foo())");return new DefaultPointcutAdvisor(pointcut,advice);}@Beanpublic Bean1 bean1(){return new Bean1();}@Beanpublic Bean2 bean2(){return new Bean2();}}static class Bean1{public void foo(){}public Bean1(){System.out.println("Bean1()");}@PostConstructpublic void init(){System.out.println("Bean1 init");}}static class Bean2{public Bean2(){System.out.println("Bean2()");}@Autowiredpublic void setBean1(Bean1 bean1){System.out.println("Bean2 setBean1(bean1) class is: "+bean1.getClass());}@PostConstructpublic void init(){System.out.println("Bean2 init");}}
}
结果:

Bean1需要Aop,其代理在初始化之后创建。上面日志并未打印代理的创建,可能由于spring版本问题。
2.依赖注入之前
package org.springframework.aop.framework.autoproxy;
import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
import org.springframework.context.support.GenericApplicationContext;import javax.annotation.PostConstruct;/*** @author zhou* @version 1.0* @description TODO* @date 2025/11/12 22:44*/
public class a1 {public static void main(String[] args) {GenericApplicationContext context = new GenericApplicationContext();context.registerBean(ConfigurationClassPostProcessor.class);context.registerBean(Config.class);context.refresh();context.close();}@Configurationstatic class Config{@Bean //解析@Aspect 产生代理public AnnotationAwareAspectJAutoProxyCreator annotationAwareAspectJAutoProxyCreator() {return new AnnotationAwareAspectJAutoProxyCreator();}@Bean //解析@PostConstructpublic CommonAnnotationBeanPostProcessor commonAnnotationBeanPostProcessor(){return new CommonAnnotationBeanPostProcessor();}@Bean //解析@Autowiredpublic AutowiredAnnotationBeanPostProcessor autowiredAnnotationBeanPostProcessor(){return new AutowiredAnnotationBeanPostProcessor();}@Beanpublic MethodInterceptor advice(){return invocation -> {System.out.println("before");Object proceed = invocation.proceed();System.out.println("after");return proceed;};}@Beanpublic Advisor advisor(MethodInterceptor advice){AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();pointcut.setExpression("execution(* foo())");return new DefaultPointcutAdvisor(pointcut,advice);}@Beanpublic Bean1 bean1(){return new Bean1();}@Beanpublic Bean2 bean2(){return new Bean2();}}static class Bean1{public void foo(){}public Bean1(){System.out.println("Bean1()");}@Autowiredpublic void setBean2(Bean2 bean2){System.out.println("Bean1 setBean2(bean2) class is: "+bean2.getClass());}@PostConstructpublic void init(){System.out.println("Bean1 init");}}static class Bean2{public Bean2(){System.out.println("Bean2()");}@Autowiredpublic void setBean1(Bean1 bean1){System.out.println("Bean2 setBean1(bean1) class is: "+bean1.getClass());}@PostConstructpublic void init(){System.out.println("Bean2 init");}}
}
Bean1与Bean2产生了循环依赖,在Bean1依赖注入之前,Bean1的代理已经被创建。并且在Bean1依赖注入之前,Bean1的代理被Bean2使用了。
结果:

