Spring OXM不适用于Struts 1

时间:2011-05-27 08:40:48

标签: java spring struts-1 spring-oxm

我使用Spring OXM以及Struts 1但不使用与Spring IOC集成Struts。这是因为应用程序是旧的,我只是添加了一个涉及XML绑定的模块,我无意改变应用程序的体系结构。

我有一个动作类调用ClasspathXmlApplicationContext来为OXM注入bean。

这是我的春天语境XML:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:oxm="http://www.springframework.org/schema/oxm"
  xsi:schemaLocation="
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/oxm 
     http://www.springframework.org/schema/oxm/spring-oxm-1.5.xsd">

    <bean id="xmlMapper" class="com.st.mas.wmr.utils.xml.stifbinconv.XmlMapper">
        <property name="marshaller" ref="jaxbMarshaller" />
        <property name="unmarshaller" ref="jaxbMarshaller" />
    </bean>
    <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="contextPath" value="com.st.mas.wmr.utils.xml.jaxb.stifbinconv"/>
        <property name="validating" value="true"/>
    </bean>
</beans>

动作类:

public class StifBinConversionAction extends AnyDispatchAction {
    private IProcessStifOliBinConversion svc;

    public StifBinConversionAction() {
        super();
        svc = new ProcessStifOliBinConversion();
    }

服务类:

public class ProcessStifOliBinConversion
    implements
        IProcessStifOliBinConversion {
    private BasicDataSource ds;

    private IAtomStifOliBinConversion dao;
    private ApplicationContext ctx;
    private XmlMapper xmlMapper;

    public ProcessStifOliBinConversion() {
        super();
        ds = new BasicDataSource();
        //TODO consts
        ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        ds.setUrl("jdbc:oracle:thin:@sglx482:1521:wmr");
        ds.setUsername("wmr_online");
        ds.setPassword("wmr_online");

        dao = new AtomStifOliBinConversion(ds);
        ctx = new ClassPathXmlApplicationContext("com/st/mas/wmr/utils/xml/stifbinconv/oxm-context.xml");
        xmlMapper = ctx.getBean(XmlMapper.class);
    }

Web应用程序为HTTP 500 WITHOUT 提供任何错误消息或堆栈跟踪。但是,如果我将ClasspathXmlApplicationContext的配置位置更改为无效的配置位置,则Spring会抛出异常。

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [classes/com/st/mas/wmr/utils/xml/stifbinconv/oxm-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [classes/com/st/mas/wmr/utils/xml/stifbinconv/oxm-context.xml] cannot be opened because it does not exist

似乎问题出在Spring注入中。

当出现错误但是没有错误消息时,这很烦人。这会让你陷入困境。

由于

威尔

1 个答案:

答案 0 :(得分:1)

  

出现错误时很烦人   但是没有错误信息。它使   你坚持了几天。

??? 出现错误消息:在此位置找不到您的XML:

classes/com/st/mas/wmr/utils/xml/stifbinconv/oxm-context.xml

我说你将错误的参数传递给ApplicationContext。请查看4.7.1.1 Constructing ClassPathXmlApplicationContext instances - shortcuts

中的示例
  

考虑一个目录布局   看起来像这样:

com/
  foo/
    services.xml
    daos.xml
    MessengerService.class
  

ClassPathXmlApplicationContext   由定义的bean组成的实例   在'services.xml'和'daos.xml'中   可以这样实例化......

ApplicationContext ctx = new ClassPathXmlApplicationContext(
    new String[] {"services.xml", "daos.xml"}, MessengerService.class

也许您也应该将该模式与this Constructor

一起使用
ctx = new ClassPathXmlApplicationContext("oxm-context.xml", XmlMapper.class);