Is it possible to inject beans into inner class?
For example:
@Named
public class outer {
@Inject
private SomeClass inst; // Injected correctly
private static class inner {
@Inject
private AnotherClass instance; // Not being injected
...
Edit: The “AnotherClass” is used only by inner class, so I do not want to pollute outer class with it. Additional reason to keep the declaration in the inner class is that I’ll have to remove the static modifier from the inner class or add it to the outer class member if I move the AnotherClass member to the outer class.
Annotations like
@Injectare used only if spring instantiates your objects. Since you annotated outer with@Named, spring will make a bean out of it and will injectSomeClassinstance correctly. On the other handinneris probably instantiated by you manually so there is no way spring will notice this annotation and do something about it.It’s not about being inner or top level class, it’s about who creates objects.