yair
yair

Reputation: 9245

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.

Upvotes: 3

Views: 2001

Answers (3)

user2542461
user2542461

Reputation: 59

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

Upvotes: 1

NicuMarasoiu
NicuMarasoiu

Reputation: 806

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

Upvotes: 1

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280000

The javax.servlet.annotation package defines a number of annotations to be used to register Servlet, Filter, and Listener classes as well as do some other configuration, for example, security.

You can also use the ServletContainerInitializer class to configure your ServletContext through Java instead of through the XML deployment descriptor. Spring provides its own implementation of ServletContainerInitializer in which case all you have to do is create a class that implements WebApplicationInitializer and does servlet, filter, and listener registration and leave that class on the classpath.

Examples abound in the javadoc.

Upvotes: -1

Related Questions