@Resource Annotation in Spring
March 24, 2022
On this page we will learn using @Resource
annotation in Spring.
1. The
@Resource
annotation is JSR-250 javax.annotation.Resource
API. Some other JSR-250 annotations are @PostConstruct
, and @PreDestroy
.
2. The
@Resource
annotation is used to declare a reference to a resource. The @Resource
annotation resolves dependency injection. We can use it in place of @Autowired
annotation.
3. In case of field-based and method-based injection, the Spring container will inject the resource when the application is initialized. For class-based injection, resource is injected at runtime.
4.The elements of
@Resource
annotation are name, type, authenticationType, shareable, mappedName and description.
5. If
@Resource
annotation is not using name attribute, Spring accesses other beans for matching them by type.
6. The
@Resource
can be used on a
a. Field
b. Method
7. The
@Resource
can be used by
a.
name
attribute
b.
type
attribute.
Contents
@Resource vs @Autowired
1. In Spring, the JSR-250@Resource
and Spring @Autowired
both annotations are used to solve dependency injection. The @Resource
is supported only for fields and bean property setter methods with a single argument whereas @Autowired
applies to fields, constructors, and multi-argument methods.
2. In case when multiple beans are eligible to be injected, we should prefer using
@Resource
over combination of @Autowired
and @Qualifier
.
3. When using
@Resource
annotation at method level, the method can have only one argument. The @Autowired
annotation can be used at argument level, so it supports more than one arguments.
4. In case of annotation-driven injection by name, we should prefer JSR-250
@Resource
annotation over Spring @Autowired
.
@Resource at Field level
Here we are using@Resource
on fields. In the below class, the company property will be injected with instance of CompanyDAO
. The address property will be injected with instance of AddressDAO
.
We are using name attribute of
@Resource
to specify bean name.
public class EmployeeService { @Resource(name="company") private CompanyDAO company; @Resource(name="address") private AddressDAO address; public CompanyDAO getCompany() { return company; } public AddressDAO getAddress() { return address; } }
@Resource(name="company")
, the company is the bean name. The name attribute is used to specify bean name.
Find the XML configuration for bean definition.
app-conf.xml
<context:annotation-config/> <bean class="com.concretepage.EmployeeService"/> <bean id="address" class="com.concretepage.dao.AddressDAO"/> <bean id="company" class="com.concretepage.dao.CompanyDAO"/>
@Resource at Method level
The@Resource
annotation is used at setter methods with one argument.
Find the code to use @Resource
at method level. We are using name attribute to specify bean name.
public class EmployeeService { private CompanyDAO company; private AddressDAO address; @Resource(name="company") public void setCompany(CompanyDAO company) { this.company = company; } @Resource(name="address") public void setAddress(AddressDAO address) { this.address = address; } ------ }
CompanyDAO
.
@Resource(name="address") : Injects
AddressDAO
.
@Resource by Type
We can inject resource by name as well as by type. Here we will discuss injecting resource by type. The type is an attribute of@Resource
annotation.
1. By default,
@Resource
annotation injects instances by type.
@Resource private CompanyDAO company;
CompanyDAO
will be injected.
2. When more than one beans are eligible to be injected, use type attribute and specify the class type to be injected.
@Resource(type = CompanyDAO.class) private AppDAO company;
CompanyDAO
will be injected.
Find the complete code of our example.
AppDAO.java
package com.concretepage.dao; public interface AppDAO { public String get(); }
package com.concretepage.dao; import org.springframework.stereotype.Component; @Component public class AddressDAO implements AppDAO { public String get() { return "Varanasi"; } }
package com.concretepage.dao; import org.springframework.stereotype.Component; @Component public class CompanyDAO implements AppDAO { public String get() { return "MyCompany Ltd"; } }
package com.concretepage; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.concretepage.dao.AddressDAO; import com.concretepage.dao.AppDAO; import com.concretepage.dao.CompanyDAO; @Service public class EmployeeService { @Resource(type = CompanyDAO.class) private AppDAO company; @Resource(type = AddressDAO.class) private AppDAO address; public AppDAO getCompany() { return company; } public AppDAO getAddress() { return address; } }
package com.concretepage; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan("com.concretepage") public class AppConfig { }
package com.concretepage; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class SpringDemo { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AppConfig.class); ctx.refresh(); EmployeeService employee = ctx.getBean(EmployeeService.class); System.out.println(employee.getAddress().get()); System.out.println(employee.getCompany().get()); } }
Varanasi MyCompany Ltd
References
Resource InjectionSpring Reference