Using JSR 330 Standard Annotations
这句话是什么意识呢?我们可以不对spring进行依赖,换成别的东西也是可以注入依赖的?是不是可以这么理解,这些事JavaEE自己处理的支持。
Starting with Spring 3.0, Spring offers support for JSR-330 standard annotations (Dependency Injection). Those annotations are scanned in the same way as the Spring annotations. You just need to have the relevant jars in your classpath.
我们来看一下他们的区别
Dependency Injection with @Inject and @Named
Instead of @Autowired, @javax.inject.Inject may be used as follows:
感觉差不多哦!
import javax.inject.Inject;
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Inject
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
和@Autowired 一样, @Inject 可用于类注解、域注解、方法注解、构造参数注解。如果需要注入指定 qualifier 标识符的 bean,应该使用@Named 注解,像这样:
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;
}
// ...
}
@Named: a standard equivalent to the @Component annotation 和组件的注解一样的
Instead of @Component, @javax.inject.Named may be used as follows:
import javax.inject.Inject;
import javax.inject.Named;
@Named("movieListener")
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Inject
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
When using @Named, it is possible to use component-scanning in the exact same way as when using Spring annotations:我们也是需要扫描包的!找到相关的后处理去处理
<beans>
<context:component-scan base-package="org.example"/>
</beans>