如何为此XML配置编写Java配置。

时间:2015-06-04 04:35:44

标签: java xml spring spring-java-config

我正在转换我的appconfig.xml tp JavaConfigurations(AnnotationConfigs)。我确实将大部分bean转换为JavaConfig。但是我被困在下面列出的几个豆子里。有人可以帮我这个.. ??

1. Bean有一个list元素,它从属性文件中获取值。 XML配置是:

<bean id="propertyCommons"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:dbUser.properties</value>
            </list>
        </property>
        <property name="ignoreUnresolvablePlaceholders" value="true" />
    </bean>

我们如何将其转换为java Config ..(尤其是从 dbUser.properties 中获取值?

2.我们如何将以下工厂配置转换为JavaConfigs?

    .
    .
    .
    <bean id="oozieJobFactoryBean" class="com.mycompany.product.dfe.main.OozieJobFactoryBean" />

    <bean id="cmdArgs" class="com.mycompany.product.dfe.main.CmdArgs"
            scope="prototype" />

    <bean id="oozieJob" factory-bean="oozieJobFactoryBean"
            factory-method="createJob" scope="prototype">
            <constructor-arg ref="cmdArgs" />
    </bean>
    .
    .
    .

3。还有以下配置..

.
.
.
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="productPU" />
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="jpaPropertyMap">
        <map>
            <entry key="hibernate.cache.use_second_level_cache" value="true" />
            <entry key="hibernate.cache.use_query_cache" value="true" />
            <entry key="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider" />
            <entry key="hibernate.show_sql" value="false" />
            <entry key="hibernate.format_sql" value="false" />
            <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <entry key="javax.persistence.validation.mode" value="NONE" />
            <entry key="hibernate.connection.characterEncoding" value="utf8" />
        </map>
    </property>
</bean>
.
.
.

请帮我解决这个春天新来的问题.. :) 在此先感谢...

Noushad Ali。

1 个答案:

答案 0 :(得分:1)

1.PropertyPlaceholderConfigurer

@Configuration
@PropertySource(value = "spring/test5.properties")
class Config {
    @Bean
    PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer c = new PropertySourcesPlaceholderConfigurer();
        c.setIgnoreUnresolvablePlaceholders(true);
        return c;
    }

...
  1. OOzieJobFactory

    class Config {

    @Bean
    @Scope("prototype")
    CmdArgs cmdArgs() {
        return new CmdArgs();
    }
    
    @Bean
    @Scope("prototype")
    OozieJobFactory oozieJobFactory() {
        return new OozieJobFactory();
    }
    
    @Bean
    OozieJob oozieJob(OozieJobFactory factory, CmdArgs cmdArgs) {
        return factory.createJob(cmdArgs);
    }
    

    }

  2. 的EntityManager

    class Config {

    @Bean
    LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean() {
        LocalContainerEntityManagerFactoryBean b = new LocalContainerEntityManagerFactoryBean();
        b.setPersistenceUnitName("productPU");
        ...
        return b;
    }
    
    @Bean
    JpaTransactionManager jpaTransactionManager(LocalContainerEntityManagerFactoryBean emf) {
        JpaTransactionManager tm = new JpaTransactionManager();
        tm.setEntityManagerFactory(emf);
        return tm;
    }