24

I'm unable to understand the meaning of the @Resource annotation. I've looked at online resources, but they seem to explain the same in a way which is difficult to comprehend. Can someone please explain the meaning of @Resource in a simplistic manner, if possible?

Thanks !

J-Alex
  • 6,881
  • 10
  • 46
  • 64
LM10
  • 1,089
  • 4
  • 10
  • 16
  • @Resource means get me a known resource by name. The name is extracted from the name of the annotated field or it's taken from the name parameter. Refer this link : https://stackoverflow.com/questions/4093504/resource-vs-autowired – Ashraf.Shk786 May 18 '18 at 21:25
  • Possible duplicate of [@Resource vs @Autowired](https://stackoverflow.com/questions/4093504/resource-vs-autowired) – Ashraf.Shk786 May 18 '18 at 21:25

2 Answers2

52

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.

enter image description here

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.

J-Alex
  • 6,881
  • 10
  • 46
  • 64
  • Awesome explanation, I really like it. – Stefan Sonnenberg-Carstens May 06 '20 at 06:53
  • 5
    According to the Spring docs (https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-resource-annotation) @Resource without any parameters will trigger Match by Name: "If no name is explicitly specified, the default name is derived from the field name" – xtian Oct 09 '20 at 09:00
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