aspose file tools*
The moose likes Spring and the fly likes Dependency injection does not seem to work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Dependency injection does not seem to work" Watch "Dependency injection does not seem to work" New topic
Author

Dependency injection does not seem to work

Saurabh Pillai
Ranch Hand

Joined: Sep 12, 2008
Posts: 509



When I access springWSEndpointService to call one of the methods, it throws Null Pointer Exception. On line 7, 2nd code snippet, throws NPE. and in debug mode I can see that springWSEndpointService is null.

"anotherService" bean is in another xml file. springWSEndpoint is a Controller class (SimpleURLHandler)

Setter methods of both class (SpringWSEndPointService and AnotherServiceImpl) is getting called. I have checked it by having logger statement. I do not need constructor for SpringWSEndPointService class still I wrote it explicitly in the class and that is also getting called during server start up.

Cross Post : http://forum.springsource.org/showthread.php?126746-Dependency-injection-does-not-seem-to-work&p=413669#post413669
Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
Hi Saurabh,

I hope you are not Initializing the Object using new operator, rather you are getting the object from Spring Container.

Regards,
Hemant.
Saurabh Pillai
Ranch Hand

Joined: Sep 12, 2008
Posts: 509
This is what I have,



And this method is getting called as mentioned earlier. SpringWSEndPoint class extends Spring's ServletEndpointSupport if this provides anymore information.
Manish Sridharan
Ranch Hand

Joined: Jul 19, 2005
Posts: 64
Hi Saurabh,

I suspect one of your config file is getting loaded. Have you mentioned both of the config files under "contextConfigLocation" parameter in web.xml. Alternatively, you can use spring import tag in your main spring config file to include other config files. Hope this helps.

Thanks,

Manish S.
Saurabh Pillai
Ranch Hand

Joined: Sep 12, 2008
Posts: 509
I have all necessary xml files under contextConfigLocation, I think this should not be an issue as I don't get bean not found exception or something when server starts.
Saurabh Pillai
Ranch Hand

Joined: Sep 12, 2008
Posts: 509
Now this is height of surprise,


At the time of server startup it calls this setter and it prints, this.springWSEndpointService is NOT NULL.
Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
Hi Saurabh,

I can see the only 2 way for this to happen is

1. you are using new operator to initialize springWSEndpoint;
2. you are using Spring container to get instance of springWSEndpoint, but prior to this code snippet of yours, you are calling springWSEndpoint bean and setting springWSEndpointService to null (this happened to me once).

Can you test your application by making your springWSEndpoint bean scope as prototype and post your result.

Regards,
Hemant

Saurabh Pillai
Ranch Hand

Joined: Sep 12, 2008
Posts: 509
1. you are using new operator to initialize springWSEndpoint;

I am not creating the object of springWSEndpoint anywhere.

2. you are using Spring container to get instance of springWSEndpoint, but prior to this code snippet of yours, you are calling springWSEndpoint bean and setting springWSEndpointService to null (this happened to me once).

Sorry I don't understand what do you mean?

Can you test your application by making your springWSEndpoint bean scope as prototype and post your result.


I did it and now the setter does not get called at server startup time. and still I get NullPointerException.
Muhammad Abdul Arif
Greenhorn

Joined: Feb 04, 2012
Posts: 25
Do you have setter method for springWSEndpointService
 
 
subject: Dependency injection does not seem to work