首页  

guice循环依赖处理     所属分类 guice 浏览量 169
guice 构造注入 循环依赖会报错

Tried proxying dyyx.cycle.failcase.ServiceA to support a circular dependency

可使用 字段注入 或者 Provider 


构造注入 import javax.inject.Inject; import javax.inject.Singleton; import com.google.inject.Guice; import com.google.inject.Injector; @Singleton class ServiceA{ private final ServiceB serviceB; // 构造函数注入 循环依赖报错 // Tried proxying dyyx.cycle.failcase.ServiceA to support a circular dependency, but it is not an interface. @Inject public ServiceA(ServiceB serviceB) { this.serviceB = serviceB; } public void hello() { System.out.println("ServiceA say hello,serviceA="+this+",serviceB="+serviceB); } } @Singleton class ServiceB{ private final ServiceA serviceA; @Inject public ServiceB(ServiceA serviceA) { this.serviceA = serviceA; } public void hello() { System.out.println("ServiceB say hello,serviceB="+this+",serviceA="+serviceA); } } @Singleton public class CycleFailDemo { @Inject private ServiceA serviceA; @Inject private ServiceB serviceB; public static void main(String[] args) throws Exception { Injector injector = Guice.createInjector(); CycleFailDemo cycleFailDemo = injector.getInstance(CycleFailDemo.class); cycleFailDemo.hello(); } public void hello() { serviceA.hello(); serviceB.hello(); } }
字段注入 import javax.inject.Inject; import javax.inject.Singleton; import com.google.inject.Guice; import com.google.inject.Injector; @Singleton class ServiceA{ @Inject private ServiceB serviceB; public void hello() { System.out.println("ServiceA say hello,serviceA="+this+",serviceB="+serviceB); } } @Singleton class ServiceB{ @Inject private ServiceA serviceA; public void hello() { System.out.println("ServiceB say hello,serviceB="+this+",serviceA="+serviceA); } } @Singleton public class CycleSuccess1Demo { @Inject private ServiceA serviceA; @Inject private ServiceB serviceB; public static void main(String[] args) throws Exception { Injector injector = Guice.createInjector(); CycleSuccess1Demo cycleFailDemo = injector.getInstance(CycleSuccess1Demo.class); cycleFailDemo.hello(); } public void hello() { serviceA.hello(); serviceB.hello(); } }
Provider import javax.inject.Inject; import javax.inject.Provider; import javax.inject.Singleton; import com.google.inject.Guice; import com.google.inject.Injector; @Singleton class ServiceA{ private final Provider<ServiceB> serviceB; // 构造函数注入 循环依赖 使用 Provider @Inject public ServiceA(final Provider<ServiceB> serviceB) { this.serviceB = serviceB; } public void hello() { System.out.println("ServiceA say hello,serviceA="+this+",serviceB="+serviceB); } } @Singleton class ServiceB{ private final Provider<ServiceA> serviceA; @Inject public ServiceB(final Provider<ServiceA> serviceA) { this.serviceA = serviceA; } public void hello() { System.out.println("ServiceB say hello,serviceB="+this+",serviceA="+serviceA); } } @Singleton public class CycleSuccess2Demo { @Inject private ServiceA serviceA; @Inject private ServiceB serviceB; public static void main(String[] args) throws Exception { Injector injector = Guice.createInjector(); CycleSuccess2Demo cycleFailDemo = injector.getInstance(CycleSuccess2Demo.class); cycleFailDemo.hello(); } public void hello() { serviceA.hello(); serviceB.hello(); } }
完整代码 https://gitee.com/dyyx/hellocode/tree/master/demo/guicedemo/src/main/java/dyyx/cycle

上一篇     下一篇
pf4j简介及例子运行说明

pf4j 例子应用 类加载机制验证

Play2.6.x开始使用Akka HTTP作为默认服务后端

guice根据名字注入消除歧义

scala函数式与非函数式写法比较

scala Int 与 Integer的区别