Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java EE 6 / 7 equivalent for Spring @Configuration

Is there a Java EE 6 / 7 equivalent annotation for Spring's @Configuration?

If the answer is yes then are the equivalents for its surrounding annotations, such as @ComponentScan and @EnableWebMvc?

I did, of course, look for it in Java EE 6 / 7 (I admit I skipped a paragraph here and there), in javadocs (specifically among annotations), in Spring doc, tutorials, blogs, SO and Google.

like image 235
yair Avatar asked Sep 10 '26 07:09

yair


2 Answers

JEE CDI has also an annotation of creating bean programmatically and exposing them, so it offers bean factories, called producers: https://dzone.com/articles/cdi-and-the-produces-annotation-for-factory

like image 142
NicuMarasoiu Avatar answered Sep 12 '26 06:09

NicuMarasoiu


CDI offers Producer methods (annotated with @Produces) which is the equivalent of @Bean in spring. You can than implement Producers classes that are beans which contain a bunch of producer methods. However, this is by far not as powerful as a spring configuration since as far as I am aware of, there is no possibity to e.g. "import" other configurations (producer classes).

This makes it especially difficult to test CDI applications.

You can either

  1. heavily use mocks to test a single CDI bean to totally avoid injected objects
  2. blow up your test class just to create the instance under test with all dependencies
  3. use testing frameworks like CDI-Unit that creates the beans for you

With 1. Test Driven Development becomes almost impossible and tests have always to be adapted when implementation changes, even if the contract does not change.

With 2. you end up in a lot compiler errors in tests as soon as dependencies between your Beans change

With 3. you need to point your testing framework to the implementation of your beans. Since there exists no Configuration that knows about all beans, the test needs to know about it. Again, if things change your tests will break.

I admit... I don't like CDI ;-P

like image 36
user2542461 Avatar answered Sep 12 '26 05:09

user2542461



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!