JSR 330 Annotations in Spring

JSR 330 Annotations in Spring

Spring provides support for JSR 330 annotations since Spring 3.0. As spring annotations JSR 330 annotations are also working in the spring bean container. You just need to have the relevant jars in your classpath.

In pom.xml file add

<dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency>

JSR-330 standard annotations (Dependency Injection):

  • Also known as @Inject
  • Joint JCP effort bu Google and SpringSource
  • Standardizes internal DI annotations
  • Published late 2009

Dependency Injection with @Inject and @Named
Instead of @Autowired, @javax.inject.Inject may be used as follows:

import javax.inject.Inject;
import javax.inject.Named;
@Named
public class TransferServiceImpl implements TransferService{

    @Inject
    public void TransferServiceImpl(@Named("accountRepository") AccountRepository accountRepository ) {
        this.accountRepository = accountRepository ;
    }

 }


import javax.inject.Named;

@Named("accountRepository")
public class JdbcAccountRepository implements AccountRepository {
//...
}
  • As with @Autowired, it is possible to use @Inject at the field level, method level and constructor-argument level.
  • As with @Component, it is possible to use @Named at the class level for component scanning by @ComponentScan
  • If you would like to use a qualified name for the dependency that should be injected, you should use the @Named annotation as with @Qualifier.

Note: In contrast to @Component, the JSR-330 @Named annotation are not composable. Please use Spring’s stereotype model for building custom component annotations.

From @Autowired to @Inject

Spring JSR 330 Comments
@Autowired
@Inject
@Inject always mandatory, has no ‘required’ attribute.
@Component
@Named
Spring also scan for @Named .
@Scope @Scope JSR 330 Scope for ,eta annotation and injection point only
@Scope(“singleton”)
@Singleton
JSR-330 default scope is like Spring’s prototype.
@Qualifier
@Named
@Value
No equivalent SpEL specific
@Required
Redundant @Inject always required
@Lazy
No equivalent Useful when needed, often abused

Spring Related Topics you may like

  1. Spring Interview Questions and Answers
  2. Spring AOP Interview Questions and Answers
  3. Spring MVC Interview Questions
  4. Spring Security Interview Questions and Answers
  5. Spring REST Interview Questions and Answers
  6. Spring Boot Interview Questions and Answers
  7. Spring Boot Microservices Interview Questions and Answers
  8. Dependency Injection (DI) in Spring
  9. Spring IoC Container
  10. What is Bean Factory in Spring
  11. ApplicationContext in Spring
  12. Bean Autowiring in Spring
  13. Spring Bean Scopes
  14. Create Custom Bean Scope in Spring Example
  15. Using ApplicationContextAware in Spring
  16. Spring Bean Life Cycle and Callbacks
  17. BeanPostProcessor in Spring
  18. BeanFactoryPostProcessor in Spring
  19. Annotations in Spring and Based Configuration
  20. Spring JSR-250 Annotations
  21. JSR 330 Annotations in Spring
  22. Spring @Component, @Repository, @Service and @Controller Stereotype Annotations
  23. Method injection with Spring using Lookup method property
  24. Spring AOP-Introduction to Aspect Oriented Programming
  25. @Aspect Annotation in Spring
  26. Spring AOP AspectJ @Before Annotation Advice Example
  27. Spring AOP Before Advice Example using XML Config
  28. Spring AOP AspectJ @After Annotation Advice Example
  29. Spring AOP After Advice Example using XML Config
  30. Spring AOP AspectJ @AfterReturning Annotation Advice Example
  31. Spring AOP After-Returning Advice Example using XML Config
  32. Spring AOP AspectJ @AfterThrowing Annotation Advice Example
  33. Spring AOP After Throwing Advice Example using XML Config
  34. Spring AOP AspectJ @Around Annotation Advice Example
  35. Spring AOP Around Advice Example using XML Config
  36. Spring AOP Proxies in Spring
  37. Spring AOP Transaction Management in Hibernate
  38. Spring Transaction Management
  39. Spring Declarative Transaction Management Example
  40. Spring AOP-Ordering of Aspects with Example
  41. Spring Security Java Based Configuration with Example
  42. Spring Security XML Namespace Configuration Example