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>
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 { //... }
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 |
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…