Circular Dependencies of Session Beans via Manual EJB Lookup

In my last two posts (Circular Dependencies of Session Beans and Circular injection of Util Classes in EJB 3.0) I wrote, that it is possible to have circular dependencies between session beans via interceptors and manual EJB lookup. In this post I will sketch out how.

In the InjectionInterceptor we have to add the possibility to add the beans by mapping name:

protected void setBeans(final Object...beans) {
	if(this.beans == null) {
		this.beans = new ArrayList<Object>(beans.length);
	}
	this.beans.addAll(Arrays.asList(beans));
}
protected void setBeansFromMappingNames(final Class<?>... beanClasses) throws Exception {
	if(this.beans == null) {
		this.beans = new ArrayList<Object>(beanClasses.length);
	}
	final InitialContext ctx = new InitialContext();
	for(final Class<?> beanClass : beanClasses) {
		final LocalBinding localBinding = beanClass.getAnnotation(LocalBinding.class);
		if(localBinding != null) {
			this.beans.add(ctx.lookup(localBinding.jndiBinding()));
		}
	}
}

Then we have to annotate the beans with the annotation @LocalBinding. At last we have to change the interceptor implementation (called FooBarInterceptor this time):

public class FooBarInterceptor extends InjectionInterceptor {
	@PostConstruct
	@Override
	protected void postConstruct(final InvocationContext ctx) throws Exception {
		this.setBeansFromMappingNames(FooBean.class, BarBean.class);
		this.setUtils(FooUtil.class, BarUtil.class);
		super.postConstruct(ctx);
	}
}

You may download the whole source in the project package inject-dependencies-0.0.3. The output should look like this:

17:19:59,319 SEVERE [FooBean] FooBean#foo(): de.jn.sandbox.ejb.injection.FooBean@4f3bc9e6
17:19:59,320 SEVERE [FooUtil] FooUtil#foo(String): de.jn.sandbox.ejb.injection.FooUtil@5e4443ad msg: this.fooUtil#foo(String) called from de.jn.sandbox.ejb.injection.FooBean@4f3bc9e6
17:19:59,320 SEVERE [BarUtil] BarUtil#bar(): de.jn.sandbox.ejb.injection.BarUtil@53b74334 msg: this.barUtil#bar(String) called from de.jn.sandbox.ejb.injection.FooUtil@5e4443ad
17:19:59,320 SEVERE [FooUtil] FooUtil#bar(String): de.jn.sandbox.ejb.injection.FooUtil@5e4443ad msg: this.fooUtil#bar(String) called from de.jn.sandbox.ejb.injection.BarUtil@53b74334
17:19:59,340 SEVERE [BarBean] BarBean#bar(): de.jn.sandbox.ejb.injection.BarBean@3f10a5ce, msg: this.bar#bar(String) called from de.jn.sandbox.ejb.injection.FooUtil@5e4443ad
17:19:59,349 SEVERE [FooBean] FooBean#bar(): de.jn.sandbox.ejb.injection.FooBean@5c4d9ecb, msg: this.foo#bar(String) called from de.jn.sandbox.ejb.injection.BarBean@3f10a5ce
17:19:59,357 SEVERE [BarBean] BarBean#bar(): de.jn.sandbox.ejb.injection.BarBean@3f10a5ce, msg: this.bar#bar(String) called from de.jn.sandbox.ejb.injection.FooBean@4f3bc9e6
17:19:59,357 SEVERE [FooBean] FooBean#bar(): de.jn.sandbox.ejb.injection.FooBean@5c4d9ecb, msg: this.foo#bar(String) called from de.jn.sandbox.ejb.injection.BarBean@3f10a5ce

Leave a Reply

Your email address will not be published. Required fields are marked *