当前位置: 首页 > news >正文

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使用了。

结果:

http://www.dtcms.com/a/602856.html

相关文章:

  • 哈尔滨网页设计网站模板射击官网
  • 【LVS高可用】Keepalived配置详解
  • 岳阳网站建设公司西安网页设计培训班价格
  • 加快网站访问速度上海新闻报道
  • 二 网站建设的重要性微信小程序推广方法
  • Flutter核心技术目录
  • 网站备案中心肇庆网站建设制作公司
  • 在中国建设银行的网站上可以转账吗江苏镇江
  • 【免费】MySQL自动化巡检工具,一键生成WORD巡检报告
  • 网站的目标网站建设分析方法
  • Linux 服务器 Mysql 8.4.6 安装
  • 泸州本地网站建设文化建设的名言警句
  • 网站版权信息模板网易邮箱163登录
  • 建站公司用的服务器广东省建设工程安全协会网站
  • 做家务的男人网站it项目网站开发的需求文档
  • 做化工外贸需要那些网站网站建设_制作_设计
  • 深入讲解C++ 智能指针:原理、使用与实践
  • 【OpenCV + VS】图像的像素位运算
  • 惠州最专业的网站建设公司wordpress七牛云图床
  • 小企业如何建网站北京建设网站官网
  • 模板建站是什么做团购网站怎样赚钱
  • 网站开发需要学什么语言网站设计联系方式
  • CVPR 2025|基于全客户端信息的联邦学习隐私泄露攻击方法
  • 精品成品网站入口h5页面制作平台
  • 频繁查找用哈希,顺序访问用列表
  • 模型蒸馏(Knowledge Distillation)
  • 电商网站合作网页设计图片主流尺寸
  • 投资网站建设及推广北京建设网服务大厅
  • 郑州建设网站的公司阳城seo排名
  • 宜春市城市建设网站苍南县住房和城乡规划建设局网站