Search code examples
jakarta-eecdijava-ee-7

How to ignore/veto CDI producer method?


I know that I could us ProcessAnnotatedType.veto() to ignore the bean.

However, I want to ignore the bean provided by a producer. e.g: I want to ignore this in Production enviroment

@Produces
@ApplicationScoped
@Development
EnvironmentDao developmentDao() {
    return new DevelopmentDao()
}

Anyone has clue? Thank you.

@Update Actually I have another solution like

@Development
@ApplicationScoped
class DevelopmentProducer {
  @Produces
  EnvironmentBean developmentBean = new EnvironmentBean() {
    @Override
    String getText() {
        return 'I am DevelopmentBean'
    }
  }

  @Produces
  @ApplicationScoped
  EnvironmentDao developmentDao() {
    return new DevelopmentDao()
  }
}

So that I could use ProcessAnnotatedType to ignore this Producer. However, I want know if there any method to just ignore the producer method instead of the entire producer?

Thank you.


Solution

  • CDI offers no straightforward way to disable a producer.

    In fact, event @Alternative won't work. However, @Specializes would work as the bean class would be truly dropped and the producer therefore not found. But that's not what you are after anyway.

    You can also make use of Extension and observe the ProcessProducer event. It does not offer a veto method but you can replace such producer with a new one fitting your needs in the setProducer method.