Spring @Autowired不适用于我的SOAP处理程序类。抛出nullPointerException?
这是我的SOAP处理程序类,用于为CRM生成安全服务处理程序。一切都很好,因为我硬编码我的凭据-用户名和密码。现在,我试图通过在属性文件中定义凭据并在此类中自动连接来删除硬编码。这种方法不起作用,每次我尝试访问CRM时,Spring都会抛出一个NullPointerExc(我想自动连接不会发生!)。为什么@Autowired在我的@Service、@Controller类中工作得很好的时候却不能在这里工作?这是我的密码:Spring @Autowired不适用于我的SOAP处理程序类。抛出nullPointerException?,spring,annotations,autowired,Spring,Annotations,Autowired,这是我的SOAP处理程序类,用于为CRM生成安全服务处理程序。一切都很好,因为我硬编码我的凭据-用户名和密码。现在,我试图通过在属性文件中定义凭据并在此类中自动连接来删除硬编码。这种方法不起作用,每次我尝试访问CRM时,Spring都会抛出一个NullPointerExc(我想自动连接不会发生!)。为什么@Autowired在我的@Service、@Controller类中工作得很好的时候却不能在这里工作?这是我的密码: package com.myPortlet.crmService; im
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;
}
package com.myPortlet.crmService;
导入java.util.Properties;
导入javax.xml.namespace.QName;
导入javax.xml.soap.SOAPElement;
导入javax.xml.soap.SOAPEnvelope;
导入javax.xml.soap.SOAPFactory;
导入javax.xml.soap.SOAPHeader;
导入javax.xml.ws.handler.MessageContext;
导入javax.xml.ws.handler.soap.SOAPHandler;
导入javax.xml.ws.handler.soap.SOAPMessageContext;
导入org.slf4j.Logger;
导入org.slf4j.LoggerFactory;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Component;
@组成部分
公共类ECMClientHeaderHandler实现SOAPHandler{
最终静态记录器=记录器工厂
.getLogger(ECMClientHeaderHandler.class);
私有静态最终字符串身份验证=”http://schemas.xmlsoap.org/ws/2002/12/secext";
私有静态最终字符串AUTH_PREFIX=“wss”;
公共ECMClientHeaderHandler(){
}
公共布尔handleFault(SOAPMessageContext smc){
返回true;
}
公共作废关闭(MessageContext mc){
}
@自动连线
私有财产申请财产;
公共布尔handleMessage(SOAPMessageContext smc){
布尔方向=((布尔)smc
.get(SOAPMessageContext.MESSAGE_出站_属性))
.booleanValue();
字符串userName=applicationProperties.getProperty(“myCRM.userName”);/*以前硬编码*/
字符串密码=applicationProperties.getProperty(“myCRM.password”);/*以前硬编码*/
logger.info(“这是用户名:”+USERNAME);
logger.info(“这是密码:”+密码);
如果(指示){
试一试{
SOAPEnvelope信封=smc.getMessage().getSOAPPart()
.getEnvelope();
SOAPFactory SOAPFactory=SOAPFactory.newInstance();
//WSSecurity报头
SOAPElement wssecHeaderem=soapFactory.createElement(
“安全性”、认证前缀、认证编号);
SOAPElement userNameTokenElm=soapFactory.createElement(
“UsernameToken”、认证前缀、认证号);
SOAPElement userNameElm=soapFactory.createElement(“用户名”,
认证前缀,认证号);
userNameElm.addTextNode(用户名);
SOAPElement passwdElm=soapFactory.createElement(“密码”,
认证前缀,认证号);
passwdElm.addTextNode(密码);
userNameTokenElm.addChildElement(userNameElm);
userNameTokenElm.addChildElement(passwdElm);
//将子元素添加到根元素
wssecheaderem.addChildElement(userNameTokenElm);
//为SOAP信封创建SOAPHeader实例
皂甙;
if(envelope.getHeader()==null){
logger.info(“SOAPHeader null.Add header”);
sh=envelope.addHeader();
}否则{
logger.info(“SOAPHeader已存在”);
sh=envelope.getHeader();
}
//将标头的SOAP元素添加到SOAP标头对象
sh.addChildElement(wssecheaderem);
}捕获(例外情况除外){
例如printStackTrace();
抛出新的运行时异常(ex);
}
}
返回true;
}
public java.util.Set getHeaders(){
返回null;
}
}
“myCRM.userName”和“myCRM.password”在我的application.properties文件中定义。application.properties的类路径在applicationContext.xml中定义:
<util:properties id="applicationProperties" location="classpath:/i18n/application.properties"/>
出了什么问题?必须让Spring上下文知道它需要在特定类上加载一些自动连接的组件。spring-servlet.xml中的@Controller注释和引用确保了这一点。 您可以尝试将其添加到spring-servlet.xml中
<context:component-scan base-package="com.myPortlet.crmService" />
还可以在类中添加@Controller注释,以在服务器启动时启动自动连接。否则,每次您尝试访问属性实例时,它都将为null。我在@webservice类中注入依赖项时遇到了类似的问题。我通过在类(org.springframework.web.context.support.SpringBeanAutowiringSupport;)中添加下面的方法解决了这个问题
我在applicationcontext.xml文件中添加了这一点。即使那样,它似乎也不起作用。我已经有了我的视图控制器。多添加一个@Controller不会导致冲突吗?请尝试使用如下内容:spring-servlet.xml:并在类中选择如下值:@Value(“#{jiraProps.JIRAURL}”)私有字符串JIRAURL;可能重复的
@PostConstruct
@WebMethod(exclude = true)
public void init() {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}