First of all, to understand the point of @Resource you need to understand the Inversion of Control (IoC).

Inversion of Control is a principle in software development which goes that the control of objects should be transferred to a container or a framework.

Dependency Injection (DI) is a pattern of IoC implementation, where the control being inverted is the setting of object’s dependencies. The act of composing objects with other objects (injecting) is done by a container rather than by the objects themselves.

Using a DI framework (like Spring IoC or EJB) you're creating your POJOs and configuring the framework (a POJO configured such way called a Bean). A Bean may have different scopes, like singleton (1 object instance per container), prototype (creates a new instance of an object per injection or explicit call) and etc.

So far, so good. What's next? It's time to use our beans.

@Resource is the annotation that will help to extract beans from the container.

There are several lookup options to extract beans:

  • Match by Name
  • Match by Type
  • Match by Qualifier

Using @Resource without any parameters will trigger Match by Type lookup type.

There is an example of usage or @Resource with field injection and Spring framework with Java-based configuration and Match by Name:

@Configuration
public class ApplicationContext {
 
    // Put the bean into the spring container
    @Bean(name = "userFile")
    public File userFile() {
        File file = new File("user.txt");
        return file;
    }
}

@Service
class UserService {

    // Ask the container to get the bean and 'put' it here (inject)
    @Resource(name = "userFile")
    private File userFile;

}

@Resource is usually used to inject data sources, singleton services, context configurations and etc.

Answer from J-Alex on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › javax › annotation › Resource.html
Resource (Java Platform SE 8 )
October 20, 2025 - The Resource annotation marks a resource that is needed by the application. This annotation may be applied to an application component class, or to fields or methods of the component class. When the annotation is applied to a field or method, the container will inject an instance of the requested ...
Top answer
1 of 3
58

First of all, to understand the point of @Resource you need to understand the Inversion of Control (IoC).

Inversion of Control is a principle in software development which goes that the control of objects should be transferred to a container or a framework.

Dependency Injection (DI) is a pattern of IoC implementation, where the control being inverted is the setting of object’s dependencies. The act of composing objects with other objects (injecting) is done by a container rather than by the objects themselves.

Using a DI framework (like Spring IoC or EJB) you're creating your POJOs and configuring the framework (a POJO configured such way called a Bean). A Bean may have different scopes, like singleton (1 object instance per container), prototype (creates a new instance of an object per injection or explicit call) and etc.

So far, so good. What's next? It's time to use our beans.

@Resource is the annotation that will help to extract beans from the container.

There are several lookup options to extract beans:

  • Match by Name
  • Match by Type
  • Match by Qualifier

Using @Resource without any parameters will trigger Match by Type lookup type.

There is an example of usage or @Resource with field injection and Spring framework with Java-based configuration and Match by Name:

@Configuration
public class ApplicationContext {
 
    // Put the bean into the spring container
    @Bean(name = "userFile")
    public File userFile() {
        File file = new File("user.txt");
        return file;
    }
}

@Service
class UserService {

    // Ask the container to get the bean and 'put' it here (inject)
    @Resource(name = "userFile")
    private File userFile;

}

@Resource is usually used to inject data sources, singleton services, context configurations and etc.

2 of 3
1

The @Resource annotation is used to identify a class, field, or method that upon initialization, the resource will be injected. For a class-based @Resource, the "resource is looked up by the application at runtime".

Further information can be found here: https://docs.oracle.com/javaee/6/tutorial/doc/bncjk.html

🌐
Spring
docs.spring.io › spring-framework › reference › core › beans › annotation-config › resource.html
Injection with @Resource :: Spring Framework
Spring also supports injection by using the JSR-250 @Resource annotation (jakarta.annotation.Resource) on fields or bean property setter methods. This is a common pattern in Jakarta EE: for example, in JSF-managed beans and JAX-WS endpoints.
🌐
Oracle
docs.oracle.com › javaee › 6 › api › javax › annotation › Resource.html
javax.annotation Annotation Type Resource
The Resource annotation marks a resource that is needed by the application. This annotation may be applied to an application component class, or to fields or methods of the component class. When the annotation is applied to a field or method, the container will inject an instance of the requested ...
🌐
Spring
docs.spring.io › pitchfork › files › m2 › docs › api › javax › annotation › class-use › Resource.html
Uses of Class javax.annotation.Resource (Pitchfork 1.0-m2 API)
Uses of Resource in org.springframework.jee.inject · Constructors in org.springframework.jee.inject with parameters of type Resource FieldInjection(java.lang.reflect.Field f, Resource annotation) MethodInjection(java.lang.reflect.Method m, Resource annotation) ResourceInfo(Resource annotation)
🌐
ConcretePage
concretepage.com › spring › resource-spring
@Resource Annotation in Spring
1. The @Resource annotation is JSR-250 jakarta.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.
🌐
Baeldung
baeldung.com › home › spring › wiring in spring: @autowired, @resource and @inject
Wiring in Spring: @Autowired, @Resource and @Inject | Baeldung
May 11, 2024 - If the design is such that the application has complex behavior, each behavior is based on different interfaces/abstract classes, and the usage of each of these implementations varies across the application, then we can use the @Resource annotation. In this scenario, the primary default execution path is match-by-name. If there is a design mandate for all dependencies to be injected by the Jakarta EE Platform as opposed to Spring, then the choice is between the @Resource annotation and the @Inject annotation.
Find elsewhere
🌐
Kotlin Discussions
discuss.kotlinlang.org › support
Resource annotation - Support
July 31, 2023 - I have TaskService interface and TaskServiceImpl class: @Service class TaskServiceImpl(val taskRepository: TaskRepository, val userRepository: UserRepository, val taskEntityConverter: TaskEntityConverter, val taskCreateResponseConverter: TaskCreateResponseConverter, val taskResponseConverter: TaskResponseConverter, @Resource(name = “requestScopedUser”) val currentUserInfo: UserInfo) : TaskService { // … other methods override fun getTaskPage(pageable: Pageable): Page { ...
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › enterprise java › spring
Spring @Resource Annotation Example - Java Code Geeks
February 14, 2019 - JDK JSR-250 provides a property or method-level annotation that supports the Autowiring functionality in the spring framework. Spring supports this injection by using the @Resource annotation, applied to either the property or the setter method of a bean.
🌐
Springcloud
springcloud.io › post › 2023-06 › spring-autowired-and-resource
The difference between @Resource and @Autowired in Spring - Spring Cloud
June 5, 2023 - Attributes are different: The @Resource annotation does not have a required attribute, but a name attribute that indicates the name of the bean to be injected, while the @Autowired annotation has a required and a name attribute, where required indicates whether the object must be injected and defaults to true, and name indicates the name of the bean to be injected.
🌐
Android Developers
developer.android.com › android studio › improve code inspection with annotations
Improve code inspection with annotations | Android Studio | Android Developers
Annotations are added as metadata tags that you attach to variables, parameters, and return values to inspect method return values, passed parameters, local variables, and fields.
🌐
Apache
tomcat.apache.org › tomcat-7.0-doc › annotationapi › javax › annotation › Resource.html
Resource (Common Annotations 1.1 API Documentation - Apache Tomcat 7.0.109)
@Target(value={TYPE,METHOD,FIELD}) @Retention(value=RUNTIME) public @interface Resource · Since: Common Annotations 1.0 · public abstract java.lang.String name · Default: "" public abstract java.lang.Class type · Default: java.lang.Object.class · public abstract Resource.AuthenticationType authenticationType ·
🌐
api-linter
linter.aip.dev › 123 › resource-annotation
Resource annotation presence - api-linter
This rule scans all top-level messages, and assumes that messages with a string name field are resources unless the message name ends with Request. For messages that are resources, it complains if the google.api.resource annotation is missing.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › aspire › fundamentals › annotations-overview
Resource annotations - Aspire | Microsoft Learn
Annotations are a key extensibility mechanism in Aspire that allow you to attach metadata and behavior to resources. They provide a way to customize how resources are configured, deployed, and managed throughout the application lifecycle.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › javax › annotation › Resources.html
Resources (Java Platform SE 8 )
July 15, 2025 - javax.annotation · @Documented @Retention(value=RUNTIME) @Target(value=TYPE) public @interface Resources · This class is used to allow multiple resources declarations. Since: Common Annotations 1.0 · See Also: Resource · public abstract Resource[] value ·
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › annotation › Annotation.html
Annotation (Java Platform SE 8 )
2 weeks ago - java.lang.annotation · All Known Implementing Classes: Action, Addressing, BindingType, ConstructorProperties, Deprecated, DescriptorKey, Documented, FaultAction, FunctionalInterface, Generated, HandlerChain, Inherited, InitParam, MTOM, MXBean, Native, Oneway, Override, PostConstruct, PreDestroy, Repeatable, RequestWrapper, Resource, Resources, RespectBinding, ResponseWrapper, Retention, SafeVarargs, ServiceMode, SOAPBinding, SOAPMessageHandler, SOAPMessageHandlers, SupportedAnnotationTypes, SupportedOptions, SupportedSourceVersion, SuppressWarnings, Target, Transient, WebEndpoint, WebFault,