gpt4 book ai didi

java - 如何为此 XML 配置编写 Java 配置?

转载 作者:行者123 更新时间:2023-12-01 11:28:15 25 4
gpt4 key购买 nike

我正在将 appconfig.xml 转换为 JavaConfigurations(AnnotationConfigs)。我确实将大部分 bean 转换为 JavaConfig。但我被下面列出的几种 bean 困住了。有人可以帮我解决这个问题吗??

1.Bean 有一个列表元素,它从属性文件中获取值。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>
.
.
.

请帮我解决这个问题,我是 Spring 的新手..:)提前致谢...

努沙德·阿里。

最佳答案

1.PropertyPlaceholderConfigurer

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

...
  • OOzieJobFactory

    类配置{

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

    }

  • 实体管理器

    类配置{

    @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;
    }
  • 关于java - 如何为此 XML 配置编写 Java 配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30635008/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com