java - 用于注入(inject)应用程序上下文的 JSR-250 与 JSR-330

标签 java spring jsr330 jsr250

我发现 JSR-330 @Inject 注释存在问题,未将 ApplicationContext 填充到我的 bean 中。当使用 JSR-250 @Resource 注释时,它会被正确注入(inject)。是的,我知道我可以让 MyClass 实现 ApplicationContextAware,但想知道为什么 @Resource 有效,但 @Inject 无效。我正在使用 spring-context 版本 4.1.6.RELEASE 和 java 8

这有效:

@Named
public class MyClass {

  @Resource
  public ApplicationContext applicationContext;

...
}

ApplicationContext 为 null

@Named
public class MyClass {

  @Inject
  public ApplicationContext applicationContext;

  ...
}

最佳答案

There was/is a lot of confusion, as JSR-330 (Dependency Injection for Java) led by Rod Johnson (SpringSource) and Bob Lee (Google Inc.) became a part of Java EE 6. JSR-330 is very simplistic. It comes with own few annotations from the package: javax.inject. The package contains the following elements: Inject, Qualifier, Scope, Singleton, Named and Provider. Its the definition of the basic dependency injection semantics.

JSR-299 (Java Contexts and Dependency Injection), with Gavin King as lead, uses JSR-330 as base and enhances it significantly with modularization, cross cutting aspects (decorators, interceptors), custom scopes, or type safe injection capabilities. JSR-299 is layered on top of JSR-330.

It is amusing to see that the built-in qualifier @Named is not recommended and should be used only for integration with legacy code:

"The use of @Named as an injection point qualifier is not recommended, except in the case of integration with legacy code that uses string-based names to identify beans." [3.11 The qualifier @Named at injection points, JSR-299 Spec, Page 32]

来源http://www.adam-bien.com/roller/abien/entry/what_is_the_relation_between

关于java - 用于注入(inject)应用程序上下文的 JSR-250 与 JSR-330,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35278746/

相关文章:

java - 我怎样才能理顺我的 Tomcat 日志记录?

java - Eclipse 不以 Debug模式启动 tomcat-7 服务器

java - 如何将两个数组合并成各自的二维数组?

java - Spring Data 的 Pageable 是否采用页码或查询偏移量?

java - 如何使用 Weld 轻松注入(inject)字符串常量?

java - JSR 330 和 Guice 互操作性

java - 如何剪切和替换字符串

java - Jackson 如何容忍 json 中错误的参数类型?

java - 如何在 Java Spring 应用程序中接受 GET/POST 请求之前预先评估条件?

spring - WebSphere 7 中的 Spring 依赖注入(inject)(JSR 330 注释)不起作用