【问题标题】:Spring - configure Jboss Intros for xml with java config?Spring - 使用 java config 为 xml 配置 Jboss Intros?
【发布时间】:2015-08-23 07:12:58
【问题描述】:

我正在编写一个 Spring Web 应用程序,并希望将我的对象转换为没有注释的 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 配置的新手,找不到很多关于如何做到这一点的文档。

任何帮助将不胜感激!

【问题讨论】:

    标签: java xml spring spring-mvc jboss


    【解决方案1】:

    当 xml config 到 java config 中的给定项目缺少文档时,最好的朋友是查找相关类的 javadocs,在这种情况下:

    @Bean javadocJaxb2Marshaler javadocJAXBRIContext javadoc,并且找不到 IntroductionsConfigParser 和 IntroductionsAnnotationReader 的 javadoc,可能需要积极订阅红帽?不确定。

    我自己从未使用过这些类,所以可能会出现错误,但我的第一次尝试应该是这样的:

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

    现在对工厂方法不太确定,所以我将发布我的 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 */ );
    }
    

    【讨论】:

    • 我不得不修改 jaxbIntroductions bean,但它对我来说就像是一样的。感谢您的帮助
    • 正如我所指出的,找不到 jaxbIntroductions 的 javadoc API,所以我只能猜测,很高兴它有帮助
    • 对于其他有相同问题的用户:这里是 jaxbIntros bean:return IntroductionsConfigParser.parseConfig(getClass().getResourceAsStream("/spring/marshaller-mapping.xml"));
    猜你喜欢
    • 2015-12-04
    • 2013-02-07
    • 2012-07-01
    • 2014-04-06
    • 1970-01-01
    • 2012-04-18
    • 2013-10-24
    • 2015-05-30
    • 2011-01-15
    相关资源
    最近更新 更多