6

Can anyone explain why @Inject object are null when its class is initialized with new operator?

public class A{
    @Inject
    B b;
    ...
    ...
}

When the above class is created as A a = new A(); I get b as null. Can anyone explain why? I know it works when I Inject class A. But I want to know why it doesn't work with new operator. What does spring do?

vicky
  • 1,046
  • 2
  • 12
  • 25

6 Answers6

8

The dependancy injection is handled by spring container, so only objects which are created by the container will be subjected to it

In this case you are creating an object manually using new operator, the spring container will not know about the object creation.

A possible solution is to use @Configurable Annotation (and AspectJ) to solve this as given in the documentation

Also have a look at this answer

Community
  • 1
  • 1
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • This is not 100% correct. Spring provides with the @Configurable Annotation (and AspectJ) injection functionality even for instances created by 'new'. – Ralph Jun 18 '13 at 09:21
  • @Ralph i think you are talking about http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-atconfigurable I've seen that but thought this was more easy – Arun P Johny Jun 18 '13 at 09:25
  • @A run P Johnny: yes. The point is that wong statements that are phrased like the absolute truth, without a hint that there is an other ways, will harm in the long run. – Ralph Jun 18 '13 at 10:25
4

Spring has no chance to autowire dependencies in beans that it does not create itself. The dependency injection should be handled by the Spring container. If you use new to create objects then you are not using the Spring container at all . Instead of creating the instance yourself , you should request the container for objects. This way the Container will have a hook on the life-cycle of that object.

A a = new A();

This way your object referenced by a is not managed by Spring . Hence it will not be able to inject any dependent objects into a.

You should get the instance of A from the container, somewhat like this :

ClassPathXmlApplicationContext context =
             new ClassPathXmlApplicationContext("applicationContext.xml");
A a = context.getBean("myBean");

P.S: - Though out of context , but this blog to-new-or-not-to-new is a nice read.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
0

How's the Injector supposed to notice and act on a new? It's a language operation that can't be intercepted.

Nicktar
  • 5,548
  • 1
  • 28
  • 43
0

B will be injected in the Bean that spring creates.

A a = new A();

this is not created by spring, this is not a spring bean.

In your context.xml, you need to create a Bean of type A and use that.

Pheonix
  • 6,049
  • 6
  • 30
  • 48
0

Even though it seems that way, injection isn't all that magical. If you create an instance with "new", all that happens is that the constructor is called and the code in the constructor is processed.

How to get an instance of a class with injected values depends on the technology you are using.

fancy
  • 2,077
  • 2
  • 23
  • 45
0

static access to entity manager in spring and unusual architecture (Could someone move this link to the last paragraph please. With my phone it is impossible to past it in the right location)

Add @Configurable to the class. Then Spring will pay inject in to this class even when it is created with an normal new.

But this requires real AspectJ.

See this question and the answer (link at the beginning) for more details.

Community
  • 1
  • 1
Ralph
  • 118,862
  • 56
  • 287
  • 383