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: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 javax.xml.ws.handler.MessageContext; import javax.xml.ws.handler.soap.SOAPHandler; import javax.xml.ws.handler.soap.SOAPMessageContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class ECMClientHeaderHandler implements SOAPHandler<SOAPMessageContext> { final static Logger logger = LoggerFactory .getLogger(ECMClientHeaderHandler.class); private static final String AUTH_NS = "http://schemas.xmlsoap.org/ws/2002/12/secext"; private static final String AUTH_PREFIX = "wss"; public ECMClientHeaderHandler() { } public boolean handleFault(SOAPMessageContext smc) { return true; } public void close(MessageContext mc) { } @Autowired private Properties applicationProperties; public boolean handleMessage(SOAPMessageContext smc) { boolean direction = ((Boolean) smc .get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY)) .booleanValue(); String userName = applicationProperties.getProperty("myCRM.userName"); /*previously hard-coded*/ String password = applicationProperties.getProperty("myCRM.password"); /*previously hard-coded*/ logger.info("This is USERNAME:"+ userName); logger.info("This is PASSWORD:"+ password); if (direction) { try { SOAPEnvelope envelope = smc.getMessage().getSOAPPart() .getEnvelope(); 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); userNameElm.addTextNode(userName); SOAPElement passwdElm = soapFactory.createElement("Password", AUTH_PREFIX, AUTH_NS); passwdElm.addTextNode(password); userNameTokenElm.addChildElement(userNameElm); userNameTokenElm.addChildElement(passwdElm); // add child elements to the root element wsSecHeaderElm.addChildElement(userNameTokenElm); // create SOAPHeader instance for SOAP envelope SOAPHeader sh; if(envelope.getHeader()==null){ logger.info("SOAPHeader null.Add header"); sh = envelope.addHeader(); }else{ logger.info("SOAPHeader already present"); sh = envelope.getHeader(); } // add SOAP element for header to SOAP header object sh.addChildElement(wsSecHeaderElm); } catch (Exception ex) { ex.printStackTrace(); throw new RuntimeException(ex); } } return true; } public java.util.Set<QName> getHeaders() { return null; }}
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!Code:<util:properties id="applicationProperties" location="classpath:/i18n/application.properties"/>
What is going wrong?![]()


Reply With Quote
