1

I'm relativly new to EE / CDI for DI so maybe one of you could provide me with some tips for my problem:

I've got a 3rd party class which I would like to inject into other beans. In spring just needs some <bean> declaration and it's done. Using cdi it now takes a factory class:

public class XProducer {    
    @Produces
    @ApplicationScoped
    public X createX(){
        return new X();
    }
}

Unfortunatly X`s constructor invokes rather expensive business logic and every time a proxy is created by cdi it's called but I can't change X's behaviour. This leaves me with the following options I don't really like:

  • wrapping X in some ugly Holder-class,
  • creating some facade and have to delgate methods to X

Are there any other options left?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
J. Doe
  • 165
  • 1
  • 1
  • 6

1 Answers1

2

This is probably a case where you want to use @javax.inject.Singleton since its a third party, you won't need interceptor support, and singleton does not generate a proxy, so the constructor will only be called once.

John Ament
  • 11,595
  • 1
  • 36
  • 45
  • Okay, using EJB then :) I initially rejected `@Singleton` due to container managed concurrency creating a bottleneck having overlooked it can be disabled with `@ConcurrencyManagement(BEAN)`. – J. Doe Apr 22 '16 at 10:31
  • Why EJB? If you using `@javax.inject.Singleton` its from JSR-330, not EJB. – John Ament Apr 22 '16 at 20:39