Spring - 使用java配置为xml配置Jboss Intros?

时间:2015-06-08 17:35:15

标签: java xml spring spring-mvc jboss

我正在编写一个弹簧网络应用程序,并希望将我的对象转换为没有注释的xml。我知道使用xml配置你可以这样做:

<bean id="jaxb2Marshaller" class="software.bytepushers.userprofile.models.JaxbIntroductionsMarshaller">
        <property name="classesToBeBound">
            <list>
                <value>software.bytepushers.userprofile.models.Person</value>
             <value>software.bytepushers.userprofile.models.WebServiceResponse</value>
            </list>
        </property>
        <property name="jaxbContextProperties">
            <map>
                <entry>
                    <key>
                        <util:constant static-field="com.sun.xml.bind.api.JAXBRIContext.ANNOTATION_READER"/>
                    </key>

                    <bean class="org.jboss.jaxb.intros.IntroductionsAnnotationReader">
                        <constructor-arg ref="jaxbIntroductions"/>
                    </bean>
                </entry>
            </map>
        </property>
        <property name="schema" value="classpath:schemas/avs-pdf-query-request.xsd"/>
    </bean>

    <bean id="jaxbIntroductions" class="org.jboss.jaxb.intros.IntroductionsConfigParser"
          factory-method="parseConfig">
        <constructor-arg><value>classpath:spring/jaxb-intros-marshaller-mapping.xml</value></constructor-arg>
    </bean>

我的webconfig.java现在有

@Bean
    public Jaxb2Marshaller jaxb2Marshaller() throws SAXException {
        org.springframework.core.io.Resource schema = new ClassPathResource("schemas/person-schema.xsd");

        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setClassesToBeBound(new Class[]{Person.class});
        marshaller.setSchema(schema);

        return marshaller;
    }

我知道我在java配置中遗漏了很多,我是java配置的新手,并且无法找到很多关于如何执行此操作的文档。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

当xml配置中缺少给定项目的文档到java配置时,你最好的朋友是找到所涉及的类的javadoc,在这种情况下:

@Bean javadocJaxb2Marshaler javadocJAXBRIContext javadoc,无法找到IntroductionsConfigParser和IntroductionsAnnotationReader的javadoc,可能需要主动订阅red hat吗?不确定。

我自己从未使用过这些类,所以也许可能会出现错误,但我的第一次尝试看起来像这样:

@Bean(name = "jaxb2Marshaller")
public JaxbIntroductionsMarshaller jaxb2Marshaller() throws SAXException {
    JaxbIntroductionsMarshaller marshaller = new JaxbIntroductionsMarshaller ();

    marshaller.setClassesToBeBound(new Class[] {
            Person.class,
            WebServiceResponse.class
        });

    Map<String,Object> jaxbContextProperties = new HashMap<String,Object>();
    jaxbContextProperties.put(JAXBRIContext.ANNOTATION_READER, introductionsAnnotationReader());
    marshaller.setJaxbContextProperties(jaxbContextProperties);

    Resource schema = new ClassPathResource("schemas/avs-pdf-query-request.xsd");
    marshaller.setSchema(schema);

    return marshaller;
}

@Bean
public IntroductionsAnnotationReader introductionsAnnotationReader() {
    return new IntroductionsAnnotationReader(parseConfig());
}

目前还不完全确定工厂方法,所以我发布了两个可能的选项:

@Bean(name = "jaxbIntroductions") // bean id
public IntroductionsConfigParser parseConfig() { // factory-method
   return new IntroductionsConfigParser( /* classpath:spring/jaxb-intros-marshaller-mapping.xml, Resource or String?? lack of javadoc */ );
}

@Bean(name = "jaxbIntroductions") // bean id
public IntroductionsConfigParser parseConfig() {
   return IntroductionsConfigParser.parseConfig( /* Resource or String?? lack of javadoc */ );
}