0

这是我的 SOAP 处理程序类,用于为 CRM 生成安全服务处理程序。当我硬编码我的凭据 - 用户名和密码时,一切正常。现在,我尝试通过在属性文件中定义凭据并在此类中自动装配来删除硬编码。这种方法不起作用,每次我尝试访问我的 CRM 时,Spring 都会抛出 NullPointerExc(我猜不会发生自动装配!)。为什么@Autowired 在我的@Service 和@Controller 类中运行良好却不能在这里工作?这是我的代码:

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;
}

}

“myCRM.userName”和“myCRM.password”在我的 application.properties 文件中定义。而application.properties的classPath在applicationContext.xml中定义:

<util:properties id="applicationProperties" location="classpath:/i18n/application.properties"/>

出了什么问题?

4

2 回答 2

0

必须让 Spring Context 意识到它需要在特定类上加载一些自动装配的组件。@Controller 注释和 spring-servlet.xml 中的引用确保了这一点。您可以尝试将其添加到您的 spring-servlet.xml

<context:component-scan base-package="com.myPortlet.crmService" />

还在你的类中添加一个 @Controller 注释以在服务器启动时启动自动连接。否则,您的 Properties 实例将在您每次尝试访问它时为空。

于 2012-11-19T10:29:51.547 回答
0

我在尝试在 @webservice 类中注入依赖项时遇到了类似的问题。我解决了它在类中添加下面的方法(org.springframework.web.context.support.SpringBeanAutowiringSupport;)

@PostConstruct
    @WebMethod(exclude = true)
    public void init() {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }
于 2014-06-17T01:04:53.860 回答