This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Spring and the fly likes @Autowired does not work with my SOAP handler class. Throws a nullPointerException? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "@Autowired does not work with my SOAP handler class. Throws a nullPointerException? " Watch "@Autowired does not work with my SOAP handler class. Throws a nullPointerException? " New topic
Author

@Autowired does not work with my SOAP handler class. Throws a nullPointerException?

lesac Nayak
Greenhorn

Joined: Nov 21, 2012
Posts: 1
This is my SOAP Handler class to generate security service handlers for a CRM. Everything was working fine as I hard coded my credentials - Username & Password. Now I tried to remove the hard-coding by defining the credentials in a properties file and autowiring it in this class. This method is not working and Spring throws a NullPointerExc (autowiring not happening I guess!) everytime I try to access my CRM. Why does @Autowired not work here while it works perfectly well my @Service, @Controller classes? Here is my code:



The "myCRM.userName" & "myCRM.password" is defined in my application.properties file. And the classPath of application.properties is defined in applicationContext.xml:



I've tried declaring the class as a bean in my applicationContext.xml; Declaring my class to spring as @Component + <component-scan...> in my xml;passing values explicitly using @Value; Still it doesn't work. Throws null pointer Exception everytime!

What is going wrong?



Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

Try to use @Resource instead. You have to use this for Collections or Maps since Autowired has a different behavior for these.



Since this is a Properties and not a Collection or Map you can still use @Autowired but you will need to add a qualifier




More on the Spring docs

If you intend to express annotation-driven injection by name, do not primarily use @Autowired, even if is technically capable of referring to a bean name through @Qualifier values. Instead, use the JSR-250 @Resource annotation, which is semantically defined to identify a specific target component by its unique name, with the declared type being irrelevant for the matching process.

As a specific consequence of this semantic difference, beans that are themselves defined as a collection or map type cannot be injected through @Autowired, because type matching is not properly applicable to them. Use @Resource for such beans, referring to the specific collection or map bean by unique name.

@Autowired applies to fields, constructors, and multi-argument methods, allowing for narrowing through qualifier annotations at the parameter level. By contrast, @Resource is supported only for fields and bean property setter methods with a single argument. As a consequence, stick with qualifiers if your injection target is a constructor or a multi-argument method.


[How To Ask Questions][Read before you PM me]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: @Autowired does not work with my SOAP handler class. Throws a nullPointerException?