java - Spring OXM 不适用于 Struts 1

标签 java spring struts-1 spring-oxm

我使用 Spring OXM 以及 Struts 1,但没有将 Struts 与 Spring IOC 集成。这是因为该应用程序是旧应用程序,我只是添加一个涉及 XML 绑定(bind)的模块,我无意更改应用程序的体系结构。

我有一个操作类调用 ClasspathXmlApplicationContext 进行 OXM 的 bean 注入(inject)。

这是我的 Spring 上下文 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>

Action 类:

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 没有任何错误消息或堆栈跟踪。但是,如果我将 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 注入(inject)中。

当出现错误但没有错误消息时,这很烦人。它会让你被困好几天。

谢谢

最佳答案

It's irritating when there's an error but there's no error message. It makes you stuck for days.

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

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

我想说您正在向 ApplicationContext 传递错误的参数。看一下 4.7.1.1 Constructing ClassPathXmlApplicationContext instances - shortcuts 中的示例

Consider a directory layout that looks like this:

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

A ClassPathXmlApplicationContext instance composed of the beans defined in the 'services.xml' and 'daos.xml' could be instantiated like so...

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

也许您还应该将该模式与 this Constructor 一起使用:

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

关于java - Spring OXM 不适用于 Struts 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6149778/

相关文章:

java - 将自定义依赖项添加到 spring 项目后缺少类

javascript - 使用 STRUTS 禁用自动完成(自动完成 ="off")

java - 无法从 IntelliJ 连接到 AWS rekognition API

java - 0E30 不为零

java.lang.IllegalArgumentException : 'dataSource' or 'jdbcTemplate' is required when starting tomcat 异常

java - 使用 Spring Java Config 引用具有依赖关系的 bean

java - 使用 Tomcat 进行基于表单的身份验证

java - 无法检索表单 bean 的定义

java - 在java中访问有点奇怪的JSONObject

java - 进入新 Activity 时在后台运行应用程序