【问题标题】:Convert Java-Based Configuration to Spring XML-based将基于 Java 的配置转换为基于 Spring XML 的配置
【发布时间】:2014-09-25 07:20:35
【问题描述】:

我正在尝试将在 java 注释中完成的 Birt 应用程序转换为基于 XML 的但在将这部分更改为 xml 时遇到困难

@Bean   
public BirtViewResolver birtViewResolver() throws Exception {
    BirtViewResolver bvr = new BirtViewResolver();
    bvr.setBirtEngine(this.engine().getObject());
    bvr.setViewClass(HtmlSingleFormatBirtView.class);
    bvr.setDataSource(this.birtDataServiceConfiguration.dataSource());
    bvr.setReportsDirectory("Reports");
    bvr.setOrder(2);
    return bvr;
}

我试过了,但不知道如何设置 birtEngine、viewClass 和 dataSource 部分

<beans:bean id="birtViewResolver" class="org.eclipse.birt.spring.core.BirtViewResolver">
        <beans:property name="birtEngine" value="?" />
        <beans:property name="viewClass" value="?" />
        <beans:property name="dataSource" value="?" />
        <beans:property name="reportsDirectory" value="Reports" />
        <beans:property name="order" value="2" />
    </beans:bean>

提前谢谢你

【问题讨论】:

  • 一个友好的建议,通过JavaConfig比XML更好地定义基础设施bean
  • @ManuelJordan 我在使用 xml 并不是因为它更好,我正在使用的应用程序已经在使用 xml,而且我更容易将上面的 javaConfig 更改为 xml

标签: java xml spring spring-mvc birt


【解决方案1】:

鉴于此

bvr.setBirtEngine(this.engine().getObject());

我将假设engine() 是另一个@Bean 方法,它返回一个FactoryBean 对象。在 XML 中,你可以把它当作

<bean name="engine" class="com.example.EngineFactoryBean" />

对于

bvr.setViewClass(HtmlSingleFormatBirtView.class);

Spring 可以在运行时将完全限定的类名作为 XML 中的字符串值转换为 Class 实例。

对于

bvr.setDataSource(this.birtDataServiceConfiguration.dataSource());

我将假设 birtDataServiceConfiguration 是对另一个 @Configuration 类的引用,而 @AutowireddataSource() 是在该类中声明的 @Bean 方法。

生成的 XML 声明类似于

<!-- Assuming you converted that config to XML as well -->
<import resource="classpath:birtDataServiceConfiguration.xml" /> 
<beans:bean id="birtViewResolver" class="org.eclipse.birt.spring.core.BirtViewResolver">
    <beans:property name="birtEngine" ref="engine" />
    <!-- You would have to give the class' fully qualified name -->
    <beans:property name="viewClass" value="com.example.fully.qualified.HtmlSingleFormatBirtView" />
    <!-- Assuming that imported config had a bean declared with the name 'dataSource' -->
    <beans:property name="dataSource" ref="dataSource" />
    <beans:property name="reportsDirectory" value="Reports" />
    <beans:property name="order" value="2" />
</beans:bean>

【讨论】:

    猜你喜欢
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    相关资源
    最近更新 更多