Reputation: 335
I'm seeing an issue with the JSR-330 @Inject annotation not populating the ApplicationContext into my bean. When using the JSR-250 @Resource annotation it gets injected properly. Yes, I know I could have MyClass implement ApplicationContextAware, but wondering why @Resource works, but @Inject does not. I'm using spring-context version 4.1.6.RELEASE and java 8
This works:
@Named
public class MyClass {
@Resource
public ApplicationContext applicationContext;
...
}
This has ApplicationContext as null
@Named
public class MyClass {
@Inject
public ApplicationContext applicationContext;
...
}
Upvotes: 2
Views: 1976
Reputation: 402
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]
source http://www.adam-bien.com/roller/abien/entry/what_is_the_relation_between
Upvotes: 3