1.11 use jsr330 standard annotation
Starting with spring 3.0, spring provides support for jsr-330 standard annotations (dependency injection). These annotations are scanned in the same way as spring annotations. To use them, you need to rely on the relevant jar package in the classpath.
If you use maven, javax.inject The components are in the standard Maven repository( https://repo1.maven.org/maven… 。 You can add the following dependencies to your pom.xml :
<dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency>
1.11.1 @Inject
and@Named
Dependency injection
You can use it@javax.inject.Inject
replace@Autowired
, similar to the following example:
import javax.inject.Inject;
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Inject
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
public void listMovies() {
this.movieFinder.findMovies(...);
// ...
}
}
similar@Autowired
Annotations can be used at the field level, method level, and construction parameter level@Inject
。 In addition, you can declare the injection point asProvider
To allow on-demand access to shorter beans, or through providersget()
Call to delay access to other beans. The following example provides a variation of the front face example:
import javax.inject.Inject;
import javax.inject.Provider;
public class SimpleMovieLister {
private Provider<MovieFinder> movieFinder;
@Inject
public void setMovieFinder(Provider<MovieFinder> movieFinder) {
this.movieFinder = movieFinder;
}
public void listMovies() {
this.movieFinder.get().findMovies(...);
// ...
}
}
If you like to use qualified names for dependencies that should be injected, you should use@Named
Note, similar to the following example:
import javax.inject.Inject;
import javax.inject.Named;
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Inject
public void setMovieFinder(@Named("main") MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
similar@Autowired
,@Inject
It can also be usedjava.util.Optional
or@Nullable
。 This is more applicable here because@Inject
No,required
Property. The following two examples show how to use it@Inject
and@Nullable
:
public class SimpleMovieLister {
@Inject
public void setMovieFinder(Optional<MovieFinder> movieFinder) {
// ...
}
}
public class SimpleMovieLister {
@Inject
public void setMovieFinder(@Nullable MovieFinder movieFinder) {
// ...
}
}
Reference code:
com.liyong.ioccontainer.starter.XmlInjectAndNamedIocContainer
1.11.2 @Named
and@ManagedBean
: equivalent to@Component
replace@Component
You can use@javax.inject.Named
orjavax.annotation.ManagedBean
, similar to the following example:
import javax.inject.Inject;
import javax.inject.Named;
@Named("movieListener") // @ManagedBean("movieListener") could be used as well
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Inject
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
Use if no component name is specified@Component
It’s very common.@Named
It can be used in a similar way, such as the following example:
import javax.inject.Inject;
import javax.inject.Named;
@Named
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Inject
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
use@Named
or@ManagedBean
You can use component scanning in exactly the same way as using spring annotations. This is similar to the following example:
@Configuration
@ComponentScan(basePackages = "org.example")
public class AppConfig {
// ...
}
And
@Component
Compared with jsr330@Named
And jsr-250ManagedBean
Annotations cannot be combined. You should use spring’s stereotype model to build custom component annotations.Reference code:
com.liyong.ioccontainer.starter.XmlNamedAndManagerBeanIocContainer
1.11.3 limitations of jsr-330 standard notes
When you use standard annotations, you should know that some important features are not available, as shown in the following table:
Spring | javax.inject.* | javax.inject restrictions / comments |
---|---|---|
@Autowired |
@Inject |
@Inject No,required Property. Can be used in Java 8Optional 。 |
@Component |
@Named / @ManagedBean |
Jsr-330 does not provide a composable model, only a method to identify named components. |
@Scope("singleton") |
@Singleton |
The default scope of jsr-330 is spring’sprototype However, in order to maintain general default consistency with spring, the jsr-330 bean is declared as asingleton 。 In order to use other scopes, you should use spring’s@Scope Notes. javax.inject There is also a@Scope Notes. However, this is only used to create your own annotations. |
@Qualifier |
@Qualifier / @Named |
javax.inject.Qualifier Is to build a custom qualifier meta annotationString Restricted passagejavax.inject.Named relation. (similar to spring’s@Qualifier Value) |
@Value |
– | no equivalent |
@Required |
– | no equivalent |
@Lazy |
– | no equivalent |
ObjectFactory |
Provider |
javax.inject.Provider It is a direct replacement of spring’s objectfactory. There is only oneget() Method name, which can also be used with spring’s@Autowired It can also be used in combination with uncommented constructors and setter methods. |
author
Personally engaged in the financial industry, I have worked in Chongqing’s first-class technical team, such as yijifu, Sijian technology, a car Hailing platform, etc. at present, I am in charge of the construction of unified payment system in a bank. I have a strong interest in the financial industry. At the same time, it also practices big data, data storage, automatic integration and deployment, distributed microservices, responsive programming, artificial intelligence and other fields. At the same time, it is also keen to share technology and create official account and blog site.
Blog address: http://youngitman.tech
CSDN: https://blog.csdn.net/liyong1…
The official account of WeChat:
Technical exchange group: