三、AOP依赖注入
修改MyConfig
package com.woniu; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; @ComponentScan(value = {"com.woniu"}) @EnableAspectJAutoProxy @Configuration public class MyConfig { } 复制代码
创建MyAop类
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Component @Aspect public class MyAop { //用来定义切入点表达式的方法,方法名就是切入点表达式的名称 @Pointcut("execution(* com.woniu.service..*.*(..))") public void pt1(){ } @Before("pt1()") public void Before() throws Throwable { System.out.println("before aop..."); } } 复制代码
Eservice
package com.woniu.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class Eservice { @Autowired private Fservice fservice; } 复制代码
Fservice
package com.woniu.service; import org.springframework.stereotype.Component; @Component public class Fservice { public void print(){ System.out.println("fservice"); } } 复制代码
启动类
package com.woniu; import com.woniu.service.*; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.EnableAspectJAutoProxy; public class Application { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class); } }
此时fservice的原生bean已经实例已经创建完成,initializeBean这个方法将会做初始化,在这里,将会给fservice生成一个代理对象
这个BeanPostProcessor会为Fservice生成代理对象,并且放置在二级缓存
earlyProxyReferences可以帮助判断是否已经生成过代理对象,wrapIfNecessary这个方法就是生成代理对象的方法
创建代理工厂
fservice的代理对象已经被创建出来
initializeBean方法调用完毕,Fservicve代理对象创建完毕
此时的一级缓存如下
至此Eservice对象的initializeBean方法执行完毕,fservice的代理对象已经被注入到eservice中
四、循环依赖
- 非单例V.S非单例
- 单例构造函数V.S单例构造函数
- 单例构造函数V.S单例普通依赖注入
- 单例普通依赖注入V.S单例普通依赖注入
- AOP单例普通依赖注入V.SAOP单例普通依赖注入****