Announcement Announcement Module
No announcement yet.
@Autowired does not work with my SOAP handler class. Throws a nullPointerException? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

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

    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:

    package com.myPortlet.crmService;
    import java.util.Properties;
    import javax.xml.namespace.QName;
    import javax.xml.soap.SOAPElement;
    import javax.xml.soap.SOAPEnvelope;
    import javax.xml.soap.SOAPFactory;
    import javax.xml.soap.SOAPHeader;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    public class ECMClientHeaderHandler implements SOAPHandler<SOAPMessageContext> {
    final static Logger logger = LoggerFactory
    private static final String AUTH_NS = "";
    private static final String AUTH_PREFIX = "wss";
    public ECMClientHeaderHandler() {
    public boolean handleFault(SOAPMessageContext smc) {
        return true;
    public void close(MessageContext mc) {
    private Properties applicationProperties;
    public boolean handleMessage(SOAPMessageContext smc) {
        boolean direction = ((Boolean) smc
        String userName = applicationProperties.getProperty("myCRM.userName");  /*previously hard-coded*/
        String password = applicationProperties.getProperty("myCRM.password"); /*previously hard-coded*/"This is USERNAME:"+ userName);"This is PASSWORD:"+ password);
        if (direction) {
            try {
                SOAPEnvelope envelope = smc.getMessage().getSOAPPart()
                SOAPFactory soapFactory = SOAPFactory.newInstance();
                // WSSecurity <Security> header
                SOAPElement wsSecHeaderElm = soapFactory.createElement(
                        "Security", AUTH_PREFIX, AUTH_NS);
                SOAPElement userNameTokenElm = soapFactory.createElement(
                        "UsernameToken", AUTH_PREFIX, AUTH_NS);
                SOAPElement userNameElm = soapFactory.createElement("Username",
                        AUTH_PREFIX, AUTH_NS);
                SOAPElement passwdElm = soapFactory.createElement("Password",
                        AUTH_PREFIX, AUTH_NS);
                // add child elements to the root element
                // create SOAPHeader instance for SOAP envelope
                SOAPHeader sh;
          "SOAPHeader null.Add header");
                    sh = envelope.addHeader();
          "SOAPHeader already present");
                     sh = envelope.getHeader();
                // add SOAP element for header to SOAP header object
            } catch (Exception ex) {
                throw new RuntimeException(ex);
        return true;
    public java.util.Set<QName> getHeaders() {
        return null;
    The "myCRM.userName" & "myCRM.password" is defined in my file. And the classPath of is defined in applicationContext.xml:

    <util:properties id="applicationProperties" location="classpath:/i18n/"/>
    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?

  • #2
    What is going wrong?

    The SoapHandler isn't managed by spring at least not the instance used that is constructed by Jax-WS and as such spring has no control. If you want spring to manage your handlers you also need spring to manage your jax-ws endpoint or make the SoapHandler @Configurable and use loadtime weaving to get the depenencies injected.