CDI annotations @Named and @Inject Dependency Injection in Spring 3.0 Tutorial

In Spring 3.0 the support for the CDI annotations @Named and @Inject are provided.

1) @Inject instead of Spring’s @Autowired to inject a bean.
2) @Named instead of Spring’s @Component to declare a bean.

Step 1: Create a simple Maven project. Just press enter for all the prompts.

Step 2: Import the above “simple-spring” in the file system into eclipse. File –> Import –> “Existing Maven Projects” and then select the “simple-spring” folder from the file system.

Step 3: Add the required libraries for the DI, CDI, Spring, etc to the pom.xml.

Step 4: Define the interfaces and the implementations with the annotations.

Dao Layer

The implementation is annotated with “@Named”.

Service Layer

Step 5: Wiring the DI via XML or Java config.

Wiring with XML – simpleApplicationConext.xml

Wiring with Java Config – AppConfig

Step 6: The main class that can be run as a stand-alone Java application

When wiredup via XML – simpleApplicationConext.xml

When wiredup via Java Config – AppConfig


Java Interview FAQs

800+ Java Interview Q&As

Top