Spring - 使用 java 配置为 xml 配置 Jboss 简介?

Spring - configure Jboss Intros for xml with java config?

我正在编写一个 spring 网络应用程序,想将我的对象转换为 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 配置的新手,找不到很多关于如何执行此操作的文档。

如有任何帮助,我们将不胜感激!

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

@Bean javadoc, Jaxb2Marshaler javadoc, JAXBRIContext 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());
}

现在对工厂方法不太确定,所以我会 post 我的 2 个可能的选择:

@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 */ );
}