Dependency Injection in Spring Through Annotations

@Autowired, @Resource, and @Inject are examples for annotations that are used for dependency injection in Spring.

While @Autowired is provided by the Spring framework, @Resource and @Inject are java standard annotations.

When using annotation-based dependency injection, if the dependency is not injected, we would get an exception at the startup of our application itself.

 

@Autowired annotation

@Autowired does dependency injection based on name, type etc. without explicitely defining dependencies. You can learn more about autowiring with example @ bean-autowiring-in-spring.

 

@Resource annotation

Similarly @Resource does automatic dependency injection based on the name of the property (if @Resource is used without any parameters) or based on the name you specify as @Resource (name= "dependent-bean-name").

 

@Required Annotation

We can define the @Required annotation over the setter which should be called without fail, or a special class called BeanPostProcessor for this annotation will throw an exception.

In addition to annotating the setter method with @Required, we also need to register the BeanPostProcessor for this annotation. Else this annotation will not have any effect. We can do this in two ways. You can directly register this BeanPostProcessor as:

<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" />

Instead you can also do the below to enable all bean post processors at once:

<context:annotation-config />

If you do this option, then you will have to make sure you change your spring.xml as:

<beans xmlns="http://www.springframework.org/schema/beans"

            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

            xmlns:context="http://www.springframework.org/schema/context"

            xsi:schemaLocation="http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

    http://www.springframework.org/schema/context

            http://www.springframework.org/schema/context/spring-context-4.0.xsd">

You will also need to add spring-aop-x.x.x.RELEASE.jar to the classpath if not already present.

 

@Component and @ComponentScan Annotations

@Component annotation can be used to tell Spring that a class is a bean without declaring it as a bean in the spring xml. 

 

When you declare a class as a bean using @Component annotation, you also need to tell Spring wihch all packages to look into for beans, and you can do it in the spring xml file as:

<context:component-scan base-package="com.javajee.spring">

</context:component-scan>

Spring will now scan your classpath for classes that are annotated with Component (or one of the specialized annotations like Service, Repository, Controller, or Configuration).

 

You can also enable component-scanning,from code using the ComponentScan annotation:

@ComponentScan(basePackages = {

"com.javajee.spring1",

 "com.javajee.spring2"

})

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!