Showing posts with label dependency injection. Show all posts
Showing posts with label dependency injection. Show all posts

Thursday, 28 September 2023

Dependency Injection

Found a good explanation of Dependency Injection, one of the pillars of CDI.

Of course, the original text by Martin Fowler2 is also well worth the read, but if you want to get into details of JEE, [1] is very good.

It works great, of course sometimes I screw it up, and then I get errors that need to be resolved, for instance:

org.jboss.weld.exceptions.DeploymentException: Exception List with 8 exceptions:
Exception 0 :
org.jboss.weld.exceptions.DeploymentException: WELD-001443: Pseudo scoped bean has circular dependencies. Dependency path: 
  - Managed Bean [class com.mrbear.OrderService] with qualifiers [@Any @Default],
  - [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject public com.mrbear.OrderService(AddressService, x, y, z),
  - Managed Bean [class com.mrbear.AddressService] with qualifiers [@Any @Default],
  - [BackedAnnotatedParameter] Parameter 11 of [BackedAnnotatedConstructor] @Inject public com.mrbear.AddressService(a, b, c, d, e, f, g, h, I, j, OrderSupplier),
  - Managed Bean [class com.mrbear.OrderSupplier with qualifiers [@Any @Default],
  - [BackedAnnotatedParameter] Parameter 7 of [BackedAnnotatedConstructor] @Inject public com.mrbear.OrderFactory(a, b, c, d, e, f, CityTownVillageSearchService),
  - Managed Bean [class com.mrbear.CityTownVillageSearchService] with qualifiers [@Any @Default],
  - [BackedAnnotatedField] @Inject private com.mrbear.CityTownVillageSearchService.OrderSupplier,
  - Managed Bean [class com.mrbear.OrderFactory] with qualifiers [@Any @Default]
	at org.jboss.weld.bootstrap.Validator.reallyValidatePseudoScopedBean(Validator.java:924)
	at org.jboss.weld.bootstrap.Validator.validatePseudoScopedInjectionPoint(Validator.java:971)
	at org.jboss.weld.bootstrap.Validator.reallyValidatePseudoScopedBean(Validator.java:933)
	at org.jboss.weld.bootstrap.Validator.validatePseudoScopedInjectionPoint(Validator.java:971)
	at org.jboss.weld.bootstrap.Validator.reallyValidatePseudoScopedBean(Validator.java:933)
	at org.jboss.weld.bootstrap.Va

Funnily enough, circular dependencies seemed to work fine when using Enterprise Beans or some such. I guess it depends a little on the implementation.

References

[1] JBoss Weld Reference - Chapter 4. Dependency injection and programmatic lookup
https://docs.jboss.org/weld/reference/1.1.0.Final/en-US/html/injection.html
[2] MartinFowler.com - Inversion of Control Containers and the Dependency Injection pattern
https://martinfowler.com/articles/injection.html

Wednesday, 3 February 2021

Using CDI to extend functionality

I recently wanted to change the behaviour of the framework.

Now, usually this means having to change code in the framework, but as this is something I'd rather not do, I found a way around this.

The framework, just as the code I write, uses CDI.

So it was a simple case of telling CDI to inject my Bean, instead of the Framework Bean.

And it worked right out of the box.

I was pretty surprised.

References

Contexts & Dependency Injection for Java
http://cdi-spec.org/