- 浏览: 145535 次
- 来自: ...
最新评论
-
安铁辉:
亲,问题解决了吗,我也遇到类似的问题,我是想cygwin+ma ...
nutch1.0 cygwin eclipse问题 -
bk41:
不明白···
Spring中的destroy-method="close"- - -
蓝月儿:
嗯 如果把得到的数据再放到一个列表显示在界面上呢,该怎么放置呢 ...
用SAX API解析XML文档为Java对象 -
zhaotao_king:
没看明白
Spring中的destroy-method="close"- - -
coffeesweet:
java.beans确实应该好好看看,了解一下,楼主的例子很好 ...
关于Spring属性编辑器详解
axis + spring 集成(2)
一.SpringAxisConstans
package com.skysuite.axis.spring;
import org.apache.axis.deployment.wsdd.WSDDConstants;
import javax.xml.namespace.QName;
public class SpringAxisConstans {
public static final String PROVIDER_SPRINGRPC = "SPRINGRPC";
public static final String PROVIDER_SPRINGMSG = "SPRINGMSG";
public static final QName QNAME_JAVARPC_SPRINGPROVIDER = new QName(WSDDConstants.URI_WSDD_JAVA, PROVIDER_SPRINGRPC);
public static final QName QNAME_JAVAMSG_SPRINGPROVIDER = new QName(WSDDConstants.URI_WSDD_JAVA, PROVIDER_SPRINGMSG);
}
2.AxisServlet
package com.skysuite.axis.spring;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.apache.axis.transport.http.AxisServlet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContextException;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.StringUtils;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;
public class SpringAxisServlet extends AxisServlet {
private static final Log LOG = LogFactory.getLog(SpringAxisServlet.class);
/**
* Suffix for WebApplicationContext namespaces. If a SpringAxisServlet is
* given the name "axis" in a context, the namespace used by this instance will
* resolve to "axis-servlet".
*/
public static final String DEFAULT_NAMESPACE_SUFFIX = "-servlet";
/**
* Default context class for SpringAxisServlet.
*
* @see org.springframework.web.context.support.XmlWebApplicationContext
*/
public static final Class DEFAULT_CONTEXT_CLASS = XmlWebApplicationContext.class;
/** Name of the ServletContext attribute for the WebApplicationContext */
public static final String SERVLET_CONTEXT_ATTRIBUTE = SpringAxisServlet.class.getName()
+ ".CONTEXT";
/** Custom WebApplicationContext class */
private Class contextClass = DEFAULT_CONTEXT_CLASS;
/** Namespace for this servlet */
private String namespace;
/** Explicit context config location */
private String contextConfigLocation;
/** WebApplicationContext for the SpringAxisServlet */
private WebApplicationContext webApplicationContext;
/**
* Initialise the WebApplicationContext of the SpringAxisServlet.
*
* @see org.apache.axis.transport.http.AxisServlet#init()
*/
public void init() throws ServletException {
super.init();
long startTime = System.currentTimeMillis();
if (LOG.isDebugEnabled()) {
LOG.debug("Servlet '" + getServletName() + "' init");
}
setContextClassName(getServletConfig().getInitParameter("contextClassName"));
setContextConfigLocation(getServletConfig().getInitParameter("contextConfigLocation"));
setNamespace(getServletConfig().getInitParameter("namespace"));
try {
this.webApplicationContext = initWebApplicationContext();
} catch (BeansException ex) {
LOG.error("Context initialization failed", ex);
throw ex;
}
if (LOG.isDebugEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
LOG.debug("Servlet '"
+ this.getServletName()
+ "' init completed in "
+ elapsedTime
+ " ms");
}
}
/**
* Close the WebApplicationContext of the SpringAxisServlet.
*
* @see org.apache.axis.transport.http.AxisServletBase#destroy()
*/
public void destroy() {
super.destroy();
log("Closing WebApplicationContext of servlet '" + getServletName() + "'");
if (this.webApplicationContext instanceof ConfigurableApplicationContext) {
((ConfigurableApplicationContext) this.webApplicationContext).close();
}
}
/**
* Set a custom context class by name. This class must be of type WebApplicationContext,
* when using the default SpringAxisServlet implementation, the context class
* must also implement ConfigurableWebApplicationContext.
*
* @see #createWebApplicationContext
*/
public void setContextClassName(String contextClassName) throws IllegalArgumentException {
if (contextClassName != null) {
try {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
this.contextClass = Class.forName(contextClassName, true, loader);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e.getMessage());
}
}
}
/**
* Set a custom context class. This class must be of type WebApplicationContext,
* when using the default SpringAxisServlet implementation, the context class
* must also implement ConfigurableWebApplicationContext.
*
* @see #createWebApplicationContext
*/
public void setContextClass(Class contextClass) {
this.contextClass = contextClass;
}
/**
* Return the custom context class.
*/
public Class getContextClass() {
return contextClass;
}
/**
* Set a custom namespace for the SpringAxisServlet,
* to be used for building a default context config location.
*/
public void setNamespace(String namespace) {
this.namespace = namespace;
}
/**
* Return the namespace for the SpringAxisServlet, falling back to default scheme if
* no custom namespace was set: e.g. "axis-servlet" for a servlet named "axis".
*/
public String getNamespace() {
if (namespace != null) {
return namespace;
}
return getServletName() + DEFAULT_NAMESPACE_SUFFIX;
}
/**
* Set the context config location explicitly, instead of relying on the default
* location built from the namespace. This location string can consist of
* multiple locations separated by any number of commas and spaces.
*/
public void setContextConfigLocation(String contextConfigLocation) {
this.contextConfigLocation = contextConfigLocation;
}
/**
* Return the explicit context config location, if any.
*/
public String getContextConfigLocation() {
return contextConfigLocation;
}
/**
* Initialize and publish the WebApplicationContext for the SpringAxisServlet.
* Delegates to createWebApplicationContext for actual creation.
* Can be overridden in subclasses.
*
* @throws org.springframework.beans.BeansException
* if the context couldn't be initialized
*
* @see #createWebApplicationContext
*/
protected WebApplicationContext initWebApplicationContext() throws BeansException {
log("Initializing WebApplicationContext for servlet '" + this.getServletName() + "'");
ServletContext ctx = getServletContext();
WebApplicationContext parent = WebApplicationContextUtils.getWebApplicationContext(ctx);
//WebApplicationContext wac = createWebApplicationContext(parent);
WebApplicationContext wac = parent;
if (LOG.isDebugEnabled()) {
LOG.debug("Using context class '"
+ wac.getClass().getName()
+ "' for servlet '"
+ getServletName()
+ "'");
}
// publish the context as a servlet context attribute
ctx.setAttribute(SERVLET_CONTEXT_ATTRIBUTE, wac);
if (LOG.isDebugEnabled()) {
LOG.debug("Published WebApplicationContext of servlet '"
+ getServletName()
+ "' as ServletContext attribute with name ["
+ SERVLET_CONTEXT_ATTRIBUTE
+ "]");
}
return wac;
}
/**
* Instantiate the WebApplicationContext for the SpringAxisServlet, either a default
* XmlWebApplicationContext or a custom context class if set. This implementation
* expects custom contexts to implement ConfigurableWebApplicationContext.
* Can be overridden in subclasses.
*
* @throws org.springframework.beans.BeansException
* if the context couldn't be initialized
*
* @see #setContextClass
*
* @see org.springframework.web.context.support.XmlWebApplicationContext
*/
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent)
throws BeansException {
if (LOG.isDebugEnabled()) {
LOG.debug("Servlet with name '"
+ getServletName()
+ "' will try to create custom WebApplicationContext context of class '"
+ getContextClass().getName()
+ "'"
+ " using parent context ["
+ parent
+ "]");
}
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(getContextClass())) {
throw new ApplicationContextException(
"Fatal initialization error in servlet with name '"
+ getServletName()
+ "': custom WebApplicationContext class ["
+ getContextClass().getName()
+ "] is not of type ConfigurableWebApplicationContext");
}
ConfigurableWebApplicationContext wac = createContextInstance();
wac.setParent(parent);
wac.setServletContext(getServletContext());
wac.setNamespace(getNamespace());
if (this.contextConfigLocation != null) {
wac.setConfigLocations(StringUtils.tokenizeToStringArray(
this.contextConfigLocation,
ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS,
true,
true));
}
wac.refresh();
return wac;
}
private ConfigurableWebApplicationContext createContextInstance() {
return (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(getContextClass());
}
/**
* Return the SpringAxisServlet's WebApplicationContext.
*/
public final WebApplicationContext getWebApplicationContext() {
return webApplicationContext;
}
}
三.SpringAxisSetup
package com.skysuite.axis.spring;
import org.apache.axis.deployment.wsdd.WSDDProvider;
public class SpringAxisSetup {
public SpringAxisSetup() {
init();
}
public void init() {
WSDDProvider.registerProvider(
SpringAxisConstans.QNAME_JAVARPC_SPRINGPROVIDER,
new WSDDSpringRPCProvider());
WSDDProvider.registerProvider(
SpringAxisConstans.QNAME_JAVAMSG_SPRINGPROVIDER,
new WSDDSpringMsgProvider());
}
}
四.SpringBeanMsgProvider
package com.skysuite.axis.spring;
import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.soap.SOAPService;
import org.apache.axis.providers.java.MsgProvider;
/**
* Axis provider for message-style services that uses Spring Framework
* to retrieve service classes and resolve their dependencies.
* Simply delegates to {@link com.workingmouse.webservice.axis.SpringBeanProvider}.
* <p>
* To use this class:<br>
* 1. Configure {@link com.workingmouse.webservice.axis.SpringAxisServlet} as your axis servlet in web.xml.
* <pre>
* <servlet>
* <servlet-name>axis</servlet-name>
* <display-name>Apache-Axis Servlet</display-name>
* <servlet-class>com.workingmouse.webservice.axis.SpringAxisServlet</servlet-class>
* </servlet>
* </pre>
* 2. Configure your server-config.wsdd service to use this class as the service handler.
* <pre>
* <service name="formRequest.jws" provider="Handler" style="message">
* <parameter name="handlerClass" value="com.workingmouse.webservice.axis.SpringBeanMsgProvider"/>
* <parameter name="wsdlTargetNamespace" value="http://www.ioof.com.au/schemas"/>
* <parameter name="springBean" value="formRequestWS"/>
* </service>
* </pre>
* 3. Configure a Spring-managed bean in axis-servlet.xml that will act as the web service end point.
* <pre>
* <bean id="formRequestWS" class="com.workingmouse.webservice.forms.FormRequestWebService">
* <property name="documentServices"><ref bean="documentServices"/></property>
* </bean>
* </pre>
*
*/
public class SpringBeanMsgProvider extends MsgProvider {
private final SpringBeanProvider provider = new SpringBeanProvider();
/**
* @see org.apache.axis.providers.java.JavaProvider#makeNewServiceObject(org.apache.axis.MessageContext, java.lang.String)
*/
protected Object makeNewServiceObject(MessageContext msgContext, String clsName)
throws Exception {
return provider.getBean(msgContext, clsName);
}
/**
* @see org.apache.axis.providers.java.JavaProvider#getServiceClass(java.lang.String, org.apache.axis.handlers.soap.SOAPService, org.apache.axis.MessageContext)
*/
protected Class getServiceClass(String clsName, SOAPService service, MessageContext msgContext)
throws AxisFault {
return provider.getBeanClass(clsName);
}
/**
* @see org.apache.axis.providers.java.JavaProvider#getServiceClassNameOptionName()
*/
protected String getServiceClassNameOptionName() {
return SpringBeanProvider.BEAN_OPTION_NAME;
}
}
五.SpringBeanProvider
package com.skysuite.axis.spring;
import javax.servlet.ServletContext;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.server.ServletEndpointContext;
import org.apache.axis.Constants;
import org.apache.axis.MessageContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
*
*/
public class SpringBeanProvider {
private Log log = LogFactory.getLog(getClass());
/**
* The server-config.wsdd service parameter used to provide the name of the
* Spring-managed bean to use as the web service end-point.
*/
public static final String BEAN_OPTION_NAME = "springBean";
public static final String BEAN_CLASS_OPTION_NAME = "springBeanClass";
private WebApplicationContext webAppCtx;
/**
* Return a bean bound with the given beanName from the WebApplicationContext.
*/
public Object getBean(MessageContext msgContext, String beanName) throws Exception {
initWebAppContext(msgContext);
if (webAppCtx != null) {
return webAppCtx.getBean(beanName);
}
else {
return null;
}
}
/**
* Return the class of a bean bound with the given beanName in the WebApplicationContext.
*/
public Class getBeanClass(String className) {
Class result = null;
try {
result = Class.forName(className);
}
catch (ClassNotFoundException e) {
log.debug("class "+className+" not found");
}
return result;
}
private void initWebAppContext(MessageContext msgContext) throws ServiceException {
log.info("initializing app context for spring-axis integration");
if (webAppCtx == null) {
if (msgContext != null) {
Object context = msgContext.getProperty(Constants.MC_SERVLET_ENDPOINT_CONTEXT);
if (context instanceof ServletEndpointContext) {
ServletEndpointContext servletEndpointContext = (ServletEndpointContext) context;
ServletContext servletCtx = servletEndpointContext.getServletContext();
webAppCtx = WebApplicationContextUtils.getWebApplicationContext(servletCtx);
if (webAppCtx == null) {
log.info("failed to retrieve webapp context for spring-axis integration");
throw new ServiceException(
"Cannot find WebApplicationContext from org.springframework.web.context.ContextLoaderListener");
}
} else {
log.info("failed to retrieve webapp context for spring-axis integration because this is an incorrect servlet context!");
throw new ServiceException("Invalid context - expected ["
+ ServletEndpointContext.class.getName()
+ "], actual ["
+ context
+ "]");
}
}
else {
log.info("null msg context!");
}
}
}
}
六.SpringBeanRPCProvider
package com.skysuite.axis.spring;
import org.apache.axis.AxisFault;
import org.apache.axis.Handler;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.soap.SOAPService;
import org.apache.axis.providers.java.RPCProvider;
/**
* <p>
* To use this class:<br>
* 1. Configure web.xml to use the axis servlet
* 2. Configure web.xml to use the spring @see org.springframework.web.context.ContextLoaderListener.
* 3. Configure your server-config.wsdd service to use this class as the service handler.
*
* Here is an example:
* <pre>
* <service name="hdWebService.jws" provider="Handler" style="rpc">
* <parameter name="handlerClass" value="com.workingmouse.webservice.axis.SpringBeanRPCProvider"/>
* <parameter name="wsdlTargetNamespace" value="http://healthdec.com"/>
* <parameter name="springBean" value="hdWebService"/>
* <parameter name="springBeanClass" value="com.healthdec.hdSOAPServer.WebService"/>
* <parameter name="allowedMethods" value="findAllInstances"/>
* <beanMapping qname="myNS:Instance" xmlns:myNS="urn:BeanService" languageSpecificType="java:com.healthdec.domain.dm.Instance"/>
* <beanMapping qname="myNS:CRFType" xmlns:myNS="urn:BeanService" languageSpecificType="java:com.healthdec.domain.dm.CRFType"/>
* <beanMapping qname="myNS:CRFQuestionType" xmlns:myNS="urn:BeanService" languageSpecificType="java:com.healthdec.domain.dm.CRFQuestionType"/>
* </service>
* </pre>
*
* 4. Configure a Spring-managed bean in your applicationContext xml file that will act as the web service end point.
* <pre>
* <bean id="productWS" class="com.skysuite.webservice.ProductWebServices">
* </bean>
* </pre>
*
*/
public class SpringBeanRPCProvider extends RPCProvider {
private final SpringBeanProvider provider = new SpringBeanProvider();
private String serviceClassName;
private String springBeanName;
/* (non-Javadoc)
* @see org.apache.axis.providers.BasicProvider#initServiceDesc(org.apache.axis.handlers.soap.SOAPService, org.apache.axis.MessageContext)
*
* Get the name and class of the Spring bean that will implement the Webservice methods.
*
*/
public void initServiceDesc(SOAPService service, MessageContext arg1)
throws AxisFault {
setServiceClassName((String)service.getOption(SpringBeanProvider.BEAN_CLASS_OPTION_NAME));
setSpringBeanName((String)service.getOption(SpringBeanProvider.BEAN_OPTION_NAME));
super.initServiceDesc(service, arg1);
}
/**
* @see org.apache.axis.providers.java.JavaProvider#makeNewServiceObject(org.apache.axis.MessageContext, java.lang.String)
*/
protected Object makeNewServiceObject(MessageContext msgContext, String clsName)
throws Exception {
if (msgContext != null) {
return provider.getBean(msgContext, getSpringBeanName());
}
else {
return null;
}
}
/**
* @see org.apache.axis.providers.java.JavaProvider#getServiceClass(java.lang.String, org.apache.axis.handlers.soap.SOAPService, org.apache.axis.MessageContext)
*/
protected Class getServiceClass(String clsName, SOAPService service, MessageContext msgContext) {
return provider.getBeanClass(clsName);
}
/**
* @see org.apache.axis.providers.java.JavaProvider#getServiceClassNameOptionName()
*/
protected String getServiceClassNameOptionName() {
return SpringBeanProvider.BEAN_OPTION_NAME;
}
public void setServiceClassName(String serviceClassName) {
this.serviceClassName = serviceClassName;
}
/**
* @see org.apache.axis.providers.java.JavaProvider#getServiceClassName(org.apache.axis.Handler)
*/
protected String getServiceClassName(Handler arg0) {
return getServiceClassName();
}
/**
* @return Returns the serviceClassName.
*/
public String getServiceClassName() {
return serviceClassName;
}
/**
* @param serviceClassName The serviceClassName to set.
*/
/**
* @return Returns the springBeanName.
*/
public String getSpringBeanName() {
return springBeanName;
}
/**
* @param springBeanName The springBeanName to set.
*/
public void setSpringBeanName(String springBeanName) {
this.springBeanName = springBeanName;
}
}
七.WSDDSpringMsgProvider
package com.skysuite.axis.spring;
import org.apache.axis.EngineConfiguration;
import org.apache.axis.Handler;
import org.apache.axis.deployment.wsdd.WSDDProvider;
import org.apache.axis.deployment.wsdd.WSDDService;
public class WSDDSpringMsgProvider extends WSDDProvider{
@Override
public Handler newProviderInstance(WSDDService arg0, EngineConfiguration arg1) throws Exception {
return new SpringBeanMsgProvider();
}
@Override
public String getName() {
return SpringAxisConstans.PROVIDER_SPRINGMSG;
}
}
八. WSDDSpringRPCProvider
package com.skysuite.axis.spring;
import org.apache.axis.EngineConfiguration;
import org.apache.axis.Handler;
import org.apache.axis.deployment.wsdd.WSDDProvider;
import org.apache.axis.deployment.wsdd.WSDDService;
public class WSDDSpringRPCProvider extends WSDDProvider{
@Override
public Handler newProviderInstance(WSDDService arg0, EngineConfiguration arg1) throws Exception {
return new SpringBeanRPCProvider();
}
@Override
public String getName() {
return SpringAxisConstans.PROVIDER_SPRINGRPC;
}
}
package com.skysuite.axis.spring;
import org.apache.axis.deployment.wsdd.WSDDConstants;
import javax.xml.namespace.QName;
public class SpringAxisConstans {
public static final String PROVIDER_SPRINGRPC = "SPRINGRPC";
public static final String PROVIDER_SPRINGMSG = "SPRINGMSG";
public static final QName QNAME_JAVARPC_SPRINGPROVIDER = new QName(WSDDConstants.URI_WSDD_JAVA, PROVIDER_SPRINGRPC);
public static final QName QNAME_JAVAMSG_SPRINGPROVIDER = new QName(WSDDConstants.URI_WSDD_JAVA, PROVIDER_SPRINGMSG);
}
2.AxisServlet
package com.skysuite.axis.spring;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.apache.axis.transport.http.AxisServlet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContextException;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.StringUtils;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;
public class SpringAxisServlet extends AxisServlet {
private static final Log LOG = LogFactory.getLog(SpringAxisServlet.class);
/**
* Suffix for WebApplicationContext namespaces. If a SpringAxisServlet is
* given the name "axis" in a context, the namespace used by this instance will
* resolve to "axis-servlet".
*/
public static final String DEFAULT_NAMESPACE_SUFFIX = "-servlet";
/**
* Default context class for SpringAxisServlet.
*
* @see org.springframework.web.context.support.XmlWebApplicationContext
*/
public static final Class DEFAULT_CONTEXT_CLASS = XmlWebApplicationContext.class;
/** Name of the ServletContext attribute for the WebApplicationContext */
public static final String SERVLET_CONTEXT_ATTRIBUTE = SpringAxisServlet.class.getName()
+ ".CONTEXT";
/** Custom WebApplicationContext class */
private Class contextClass = DEFAULT_CONTEXT_CLASS;
/** Namespace for this servlet */
private String namespace;
/** Explicit context config location */
private String contextConfigLocation;
/** WebApplicationContext for the SpringAxisServlet */
private WebApplicationContext webApplicationContext;
/**
* Initialise the WebApplicationContext of the SpringAxisServlet.
*
* @see org.apache.axis.transport.http.AxisServlet#init()
*/
public void init() throws ServletException {
super.init();
long startTime = System.currentTimeMillis();
if (LOG.isDebugEnabled()) {
LOG.debug("Servlet '" + getServletName() + "' init");
}
setContextClassName(getServletConfig().getInitParameter("contextClassName"));
setContextConfigLocation(getServletConfig().getInitParameter("contextConfigLocation"));
setNamespace(getServletConfig().getInitParameter("namespace"));
try {
this.webApplicationContext = initWebApplicationContext();
} catch (BeansException ex) {
LOG.error("Context initialization failed", ex);
throw ex;
}
if (LOG.isDebugEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
LOG.debug("Servlet '"
+ this.getServletName()
+ "' init completed in "
+ elapsedTime
+ " ms");
}
}
/**
* Close the WebApplicationContext of the SpringAxisServlet.
*
* @see org.apache.axis.transport.http.AxisServletBase#destroy()
*/
public void destroy() {
super.destroy();
log("Closing WebApplicationContext of servlet '" + getServletName() + "'");
if (this.webApplicationContext instanceof ConfigurableApplicationContext) {
((ConfigurableApplicationContext) this.webApplicationContext).close();
}
}
/**
* Set a custom context class by name. This class must be of type WebApplicationContext,
* when using the default SpringAxisServlet implementation, the context class
* must also implement ConfigurableWebApplicationContext.
*
* @see #createWebApplicationContext
*/
public void setContextClassName(String contextClassName) throws IllegalArgumentException {
if (contextClassName != null) {
try {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
this.contextClass = Class.forName(contextClassName, true, loader);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e.getMessage());
}
}
}
/**
* Set a custom context class. This class must be of type WebApplicationContext,
* when using the default SpringAxisServlet implementation, the context class
* must also implement ConfigurableWebApplicationContext.
*
* @see #createWebApplicationContext
*/
public void setContextClass(Class contextClass) {
this.contextClass = contextClass;
}
/**
* Return the custom context class.
*/
public Class getContextClass() {
return contextClass;
}
/**
* Set a custom namespace for the SpringAxisServlet,
* to be used for building a default context config location.
*/
public void setNamespace(String namespace) {
this.namespace = namespace;
}
/**
* Return the namespace for the SpringAxisServlet, falling back to default scheme if
* no custom namespace was set: e.g. "axis-servlet" for a servlet named "axis".
*/
public String getNamespace() {
if (namespace != null) {
return namespace;
}
return getServletName() + DEFAULT_NAMESPACE_SUFFIX;
}
/**
* Set the context config location explicitly, instead of relying on the default
* location built from the namespace. This location string can consist of
* multiple locations separated by any number of commas and spaces.
*/
public void setContextConfigLocation(String contextConfigLocation) {
this.contextConfigLocation = contextConfigLocation;
}
/**
* Return the explicit context config location, if any.
*/
public String getContextConfigLocation() {
return contextConfigLocation;
}
/**
* Initialize and publish the WebApplicationContext for the SpringAxisServlet.
* Delegates to createWebApplicationContext for actual creation.
* Can be overridden in subclasses.
*
* @throws org.springframework.beans.BeansException
* if the context couldn't be initialized
*
* @see #createWebApplicationContext
*/
protected WebApplicationContext initWebApplicationContext() throws BeansException {
log("Initializing WebApplicationContext for servlet '" + this.getServletName() + "'");
ServletContext ctx = getServletContext();
WebApplicationContext parent = WebApplicationContextUtils.getWebApplicationContext(ctx);
//WebApplicationContext wac = createWebApplicationContext(parent);
WebApplicationContext wac = parent;
if (LOG.isDebugEnabled()) {
LOG.debug("Using context class '"
+ wac.getClass().getName()
+ "' for servlet '"
+ getServletName()
+ "'");
}
// publish the context as a servlet context attribute
ctx.setAttribute(SERVLET_CONTEXT_ATTRIBUTE, wac);
if (LOG.isDebugEnabled()) {
LOG.debug("Published WebApplicationContext of servlet '"
+ getServletName()
+ "' as ServletContext attribute with name ["
+ SERVLET_CONTEXT_ATTRIBUTE
+ "]");
}
return wac;
}
/**
* Instantiate the WebApplicationContext for the SpringAxisServlet, either a default
* XmlWebApplicationContext or a custom context class if set. This implementation
* expects custom contexts to implement ConfigurableWebApplicationContext.
* Can be overridden in subclasses.
*
* @throws org.springframework.beans.BeansException
* if the context couldn't be initialized
*
* @see #setContextClass
*
* @see org.springframework.web.context.support.XmlWebApplicationContext
*/
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent)
throws BeansException {
if (LOG.isDebugEnabled()) {
LOG.debug("Servlet with name '"
+ getServletName()
+ "' will try to create custom WebApplicationContext context of class '"
+ getContextClass().getName()
+ "'"
+ " using parent context ["
+ parent
+ "]");
}
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(getContextClass())) {
throw new ApplicationContextException(
"Fatal initialization error in servlet with name '"
+ getServletName()
+ "': custom WebApplicationContext class ["
+ getContextClass().getName()
+ "] is not of type ConfigurableWebApplicationContext");
}
ConfigurableWebApplicationContext wac = createContextInstance();
wac.setParent(parent);
wac.setServletContext(getServletContext());
wac.setNamespace(getNamespace());
if (this.contextConfigLocation != null) {
wac.setConfigLocations(StringUtils.tokenizeToStringArray(
this.contextConfigLocation,
ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS,
true,
true));
}
wac.refresh();
return wac;
}
private ConfigurableWebApplicationContext createContextInstance() {
return (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(getContextClass());
}
/**
* Return the SpringAxisServlet's WebApplicationContext.
*/
public final WebApplicationContext getWebApplicationContext() {
return webApplicationContext;
}
}
三.SpringAxisSetup
package com.skysuite.axis.spring;
import org.apache.axis.deployment.wsdd.WSDDProvider;
public class SpringAxisSetup {
public SpringAxisSetup() {
init();
}
public void init() {
WSDDProvider.registerProvider(
SpringAxisConstans.QNAME_JAVARPC_SPRINGPROVIDER,
new WSDDSpringRPCProvider());
WSDDProvider.registerProvider(
SpringAxisConstans.QNAME_JAVAMSG_SPRINGPROVIDER,
new WSDDSpringMsgProvider());
}
}
四.SpringBeanMsgProvider
package com.skysuite.axis.spring;
import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.soap.SOAPService;
import org.apache.axis.providers.java.MsgProvider;
/**
* Axis provider for message-style services that uses Spring Framework
* to retrieve service classes and resolve their dependencies.
* Simply delegates to {@link com.workingmouse.webservice.axis.SpringBeanProvider}.
* <p>
* To use this class:<br>
* 1. Configure {@link com.workingmouse.webservice.axis.SpringAxisServlet} as your axis servlet in web.xml.
* <pre>
* <servlet>
* <servlet-name>axis</servlet-name>
* <display-name>Apache-Axis Servlet</display-name>
* <servlet-class>com.workingmouse.webservice.axis.SpringAxisServlet</servlet-class>
* </servlet>
* </pre>
* 2. Configure your server-config.wsdd service to use this class as the service handler.
* <pre>
* <service name="formRequest.jws" provider="Handler" style="message">
* <parameter name="handlerClass" value="com.workingmouse.webservice.axis.SpringBeanMsgProvider"/>
* <parameter name="wsdlTargetNamespace" value="http://www.ioof.com.au/schemas"/>
* <parameter name="springBean" value="formRequestWS"/>
* </service>
* </pre>
* 3. Configure a Spring-managed bean in axis-servlet.xml that will act as the web service end point.
* <pre>
* <bean id="formRequestWS" class="com.workingmouse.webservice.forms.FormRequestWebService">
* <property name="documentServices"><ref bean="documentServices"/></property>
* </bean>
* </pre>
*
*/
public class SpringBeanMsgProvider extends MsgProvider {
private final SpringBeanProvider provider = new SpringBeanProvider();
/**
* @see org.apache.axis.providers.java.JavaProvider#makeNewServiceObject(org.apache.axis.MessageContext, java.lang.String)
*/
protected Object makeNewServiceObject(MessageContext msgContext, String clsName)
throws Exception {
return provider.getBean(msgContext, clsName);
}
/**
* @see org.apache.axis.providers.java.JavaProvider#getServiceClass(java.lang.String, org.apache.axis.handlers.soap.SOAPService, org.apache.axis.MessageContext)
*/
protected Class getServiceClass(String clsName, SOAPService service, MessageContext msgContext)
throws AxisFault {
return provider.getBeanClass(clsName);
}
/**
* @see org.apache.axis.providers.java.JavaProvider#getServiceClassNameOptionName()
*/
protected String getServiceClassNameOptionName() {
return SpringBeanProvider.BEAN_OPTION_NAME;
}
}
五.SpringBeanProvider
package com.skysuite.axis.spring;
import javax.servlet.ServletContext;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.server.ServletEndpointContext;
import org.apache.axis.Constants;
import org.apache.axis.MessageContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
*
*/
public class SpringBeanProvider {
private Log log = LogFactory.getLog(getClass());
/**
* The server-config.wsdd service parameter used to provide the name of the
* Spring-managed bean to use as the web service end-point.
*/
public static final String BEAN_OPTION_NAME = "springBean";
public static final String BEAN_CLASS_OPTION_NAME = "springBeanClass";
private WebApplicationContext webAppCtx;
/**
* Return a bean bound with the given beanName from the WebApplicationContext.
*/
public Object getBean(MessageContext msgContext, String beanName) throws Exception {
initWebAppContext(msgContext);
if (webAppCtx != null) {
return webAppCtx.getBean(beanName);
}
else {
return null;
}
}
/**
* Return the class of a bean bound with the given beanName in the WebApplicationContext.
*/
public Class getBeanClass(String className) {
Class result = null;
try {
result = Class.forName(className);
}
catch (ClassNotFoundException e) {
log.debug("class "+className+" not found");
}
return result;
}
private void initWebAppContext(MessageContext msgContext) throws ServiceException {
log.info("initializing app context for spring-axis integration");
if (webAppCtx == null) {
if (msgContext != null) {
Object context = msgContext.getProperty(Constants.MC_SERVLET_ENDPOINT_CONTEXT);
if (context instanceof ServletEndpointContext) {
ServletEndpointContext servletEndpointContext = (ServletEndpointContext) context;
ServletContext servletCtx = servletEndpointContext.getServletContext();
webAppCtx = WebApplicationContextUtils.getWebApplicationContext(servletCtx);
if (webAppCtx == null) {
log.info("failed to retrieve webapp context for spring-axis integration");
throw new ServiceException(
"Cannot find WebApplicationContext from org.springframework.web.context.ContextLoaderListener");
}
} else {
log.info("failed to retrieve webapp context for spring-axis integration because this is an incorrect servlet context!");
throw new ServiceException("Invalid context - expected ["
+ ServletEndpointContext.class.getName()
+ "], actual ["
+ context
+ "]");
}
}
else {
log.info("null msg context!");
}
}
}
}
六.SpringBeanRPCProvider
package com.skysuite.axis.spring;
import org.apache.axis.AxisFault;
import org.apache.axis.Handler;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.soap.SOAPService;
import org.apache.axis.providers.java.RPCProvider;
/**
* <p>
* To use this class:<br>
* 1. Configure web.xml to use the axis servlet
* 2. Configure web.xml to use the spring @see org.springframework.web.context.ContextLoaderListener.
* 3. Configure your server-config.wsdd service to use this class as the service handler.
*
* Here is an example:
* <pre>
* <service name="hdWebService.jws" provider="Handler" style="rpc">
* <parameter name="handlerClass" value="com.workingmouse.webservice.axis.SpringBeanRPCProvider"/>
* <parameter name="wsdlTargetNamespace" value="http://healthdec.com"/>
* <parameter name="springBean" value="hdWebService"/>
* <parameter name="springBeanClass" value="com.healthdec.hdSOAPServer.WebService"/>
* <parameter name="allowedMethods" value="findAllInstances"/>
* <beanMapping qname="myNS:Instance" xmlns:myNS="urn:BeanService" languageSpecificType="java:com.healthdec.domain.dm.Instance"/>
* <beanMapping qname="myNS:CRFType" xmlns:myNS="urn:BeanService" languageSpecificType="java:com.healthdec.domain.dm.CRFType"/>
* <beanMapping qname="myNS:CRFQuestionType" xmlns:myNS="urn:BeanService" languageSpecificType="java:com.healthdec.domain.dm.CRFQuestionType"/>
* </service>
* </pre>
*
* 4. Configure a Spring-managed bean in your applicationContext xml file that will act as the web service end point.
* <pre>
* <bean id="productWS" class="com.skysuite.webservice.ProductWebServices">
* </bean>
* </pre>
*
*/
public class SpringBeanRPCProvider extends RPCProvider {
private final SpringBeanProvider provider = new SpringBeanProvider();
private String serviceClassName;
private String springBeanName;
/* (non-Javadoc)
* @see org.apache.axis.providers.BasicProvider#initServiceDesc(org.apache.axis.handlers.soap.SOAPService, org.apache.axis.MessageContext)
*
* Get the name and class of the Spring bean that will implement the Webservice methods.
*
*/
public void initServiceDesc(SOAPService service, MessageContext arg1)
throws AxisFault {
setServiceClassName((String)service.getOption(SpringBeanProvider.BEAN_CLASS_OPTION_NAME));
setSpringBeanName((String)service.getOption(SpringBeanProvider.BEAN_OPTION_NAME));
super.initServiceDesc(service, arg1);
}
/**
* @see org.apache.axis.providers.java.JavaProvider#makeNewServiceObject(org.apache.axis.MessageContext, java.lang.String)
*/
protected Object makeNewServiceObject(MessageContext msgContext, String clsName)
throws Exception {
if (msgContext != null) {
return provider.getBean(msgContext, getSpringBeanName());
}
else {
return null;
}
}
/**
* @see org.apache.axis.providers.java.JavaProvider#getServiceClass(java.lang.String, org.apache.axis.handlers.soap.SOAPService, org.apache.axis.MessageContext)
*/
protected Class getServiceClass(String clsName, SOAPService service, MessageContext msgContext) {
return provider.getBeanClass(clsName);
}
/**
* @see org.apache.axis.providers.java.JavaProvider#getServiceClassNameOptionName()
*/
protected String getServiceClassNameOptionName() {
return SpringBeanProvider.BEAN_OPTION_NAME;
}
public void setServiceClassName(String serviceClassName) {
this.serviceClassName = serviceClassName;
}
/**
* @see org.apache.axis.providers.java.JavaProvider#getServiceClassName(org.apache.axis.Handler)
*/
protected String getServiceClassName(Handler arg0) {
return getServiceClassName();
}
/**
* @return Returns the serviceClassName.
*/
public String getServiceClassName() {
return serviceClassName;
}
/**
* @param serviceClassName The serviceClassName to set.
*/
/**
* @return Returns the springBeanName.
*/
public String getSpringBeanName() {
return springBeanName;
}
/**
* @param springBeanName The springBeanName to set.
*/
public void setSpringBeanName(String springBeanName) {
this.springBeanName = springBeanName;
}
}
七.WSDDSpringMsgProvider
package com.skysuite.axis.spring;
import org.apache.axis.EngineConfiguration;
import org.apache.axis.Handler;
import org.apache.axis.deployment.wsdd.WSDDProvider;
import org.apache.axis.deployment.wsdd.WSDDService;
public class WSDDSpringMsgProvider extends WSDDProvider{
@Override
public Handler newProviderInstance(WSDDService arg0, EngineConfiguration arg1) throws Exception {
return new SpringBeanMsgProvider();
}
@Override
public String getName() {
return SpringAxisConstans.PROVIDER_SPRINGMSG;
}
}
八. WSDDSpringRPCProvider
package com.skysuite.axis.spring;
import org.apache.axis.EngineConfiguration;
import org.apache.axis.Handler;
import org.apache.axis.deployment.wsdd.WSDDProvider;
import org.apache.axis.deployment.wsdd.WSDDService;
public class WSDDSpringRPCProvider extends WSDDProvider{
@Override
public Handler newProviderInstance(WSDDService arg0, EngineConfiguration arg1) throws Exception {
return new SpringBeanRPCProvider();
}
@Override
public String getName() {
return SpringAxisConstans.PROVIDER_SPRINGRPC;
}
}
相关推荐
标题中的“axis2+spring webservice”指的是使用Apache Axis2框架与Spring框架集成来开发Web服务。Apache Axis2是Java环境中广泛使用的Web服务引擎,它提供了高性能、灵活且可扩展的架构。Spring框架则是一个全面的...
标题"axis2+spring整合实例"表明了这个压缩包内容是关于如何将Apache Axis2服务框架与Spring框架进行集成的实践案例。Apache Axis2是用于构建Web服务和SOA(Service-Oriented Architecture)的高性能、灵活的开源...
【WebService(Axis+Spring+jpa)】是一种将Web服务与企业级Java技术相结合的应用实例,...这种集成方式的优势在于,Axis提供了Web服务的生成和调用,Spring简化了服务层的管理,而JPA则让数据存储变得更加透明和高效。
Spring还支持与其他框架的集成,如Axis2,使得Web服务的调用更加方便。 3. iBATIS:iBATIS(现在已经演变为MyBatis)是一个持久层框架,它简化了Java应用与数据库之间的交互。开发者可以通过XML或注解定义SQL查询,...
3. **Axis2与Spring集成**:说明如何配置Spring应用上下文,将Spring Bean作为Axis2服务,以及如何利用Spring的IoC和AOP特性优化服务行为。 4. **创建服务**:详细步骤指导如何创建一个基于Axis2的服务,包括编写...
在提供的链接(http://blog.csdn.net/linlinv3/article/details/9017767)中,你可以找到更具体的关于如何在实际项目中集成和使用Axis2与Spring3.2.0的详细步骤和示例。对于Java Web开发者来说,理解并掌握这种整合...
标题中的“Axis和Spring集成”指的是在Java Web开发中,将Apache Axis,一个用于构建Web服务的开源工具,与Spring框架结合使用的过程。这个主题涵盖了如何利用Spring的强大依赖注入(DI)和管理bean的能力,来增强...
Axis2和Spring框架的结合提供了一种高效且灵活的方式来创建和管理WebService。让我们深入了解一下这两个技术以及它们如何协同工作。 首先,Apache Axis2是Java平台上一个成熟的Web服务引擎,专门用于处理SOAP消息。...
在“axis2+spring+hibernate Webservice”这个组合中,Spring通常作为胶水层,将Axis2和Hibernate集成在一起。Spring可以帮助管理Web服务的生命周期,同时协调数据库操作。例如,可以使用Spring的AOP功能来实现事务...
7. **安全集成**:Spring的安全框架如Spring Security可以与Axis2集成,提供认证、授权和加密等功能,增强Web服务的安全性。 整合Axis2和Spring2.5不仅提高了开发效率,还增强了Web服务的灵活性和可维护性。这种...
在集成Axis2和Spring的过程中,开发者通常会将Spring的业务逻辑和服务层组件暴露为Web服务,这样可以使得服务具有更好的解耦性和可重用性。具体步骤可能包括以下几个方面: 1. **配置Spring容器**:首先,你需要...
SPRING框架则为AXIS2提供了良好的集成,使得我们可以利用SPRING的依赖注入特性来管理AXIS2的服务实例,以及处理文件上传后的业务逻辑。SPRING的AOP(面向切面编程)功能也可以用来实现文件上传的事务管理和权限控制...
axis2与spring的集成,在application中配置要发布的Java类,然后配置aar文件,在aar打包文件中的services.xml要嵌入 <parameter name="ServiceObjectSupplier">org.apache.axis2.extensions.spring.receivers....
在构建基于Axis2和Spring的Web服务时,我们需要一系列的依赖库来支持整个框架的运行。这些库提供了从XML解析到服务部署的各种功能。以下是标题和描述中提及的关键知识点及其详细解释: 1. **Axis2**:Apache Axis2...
Spring 集成 AXIS2 的 Web service 配置方法 Spring 是一个流行的 Java 应用程序框架,它提供了一个灵活的方式来构建企业级应用程序。AXIS2 是一个流行的 Web Service 引擎,它提供了一个强大的方式来构建 Web ...
通过axis2与spring进行集成,发布webservice服务,
Spring框架和Apache Axis是实现Web服务集成的两个关键工具。本文将深入探讨如何使用Spring与Axis进行集成,以便开发和消费Web服务。 首先,让我们了解Spring和Axis的基本概念。Spring是一个开源Java框架,它为构建...
在本文中,我们将深入探讨如何将Apache Axis2与Spring Boot集成,以构建一个高效、可扩展的Web服务应用。Apache Axis2是Java平台上的一个Web服务框架,它提供了高性能、灵活的服务开发和部署机制。而Spring Boot是...
总结来说,Spring集成Axis2实现Web服务涉及到Spring的IoC容器、服务的创建和发布、以及客户端的调用等多个环节。了解并掌握这些知识点,对于开发高质量的Web服务应用至关重要。在实际项目中,务必确保所有必要的库...
标题中的“spring-axis2-test.rar_Axis2 Spring3_axis2_axis2 spring3_axis2 s”指的是一个关于Spring和Axis2集成的示例项目,它包含了一组用于演示如何在Spring框架中使用Apache Axis2来开发和部署Web服务的源代码...