将 XML hibernateConfiguration 转换为 Java 配置?
Convert XML hibernateConfiguration to Java Config?
我希望将此 XML 转换为 Java 配置 Spring:
<bean id="hibernateConfiguration" factory-bean="&sessionFactory"
factory-method="getConfiguration" />
我在想...
@Bean
public org.hibernate.cfg.Configuration configuration() {
org.hibernate.cfg.Configuration config = new org.hibernate.cfg.Configuration();
//uhhh...
return config;
}
如何指定 factory-bean
和 factory-method
参数?试着环顾四周,但没有运气。
旁白:&
告诉 Spring 获取实际的 bean
更新:
下面是我使用 bean 的方式。它是一个打印模式的侦听器。
@Component
public class SchemaExportListener extends AbstractEnvironment implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
log.debug("onApplicationEvent");
if (isPrintSchemaEnabled()) {
log.info("printing schema");
SchemaExport schema = new SchemaExport(hibernateConfiguration);
schema.setDelimiter(BaseConstants.SEMI_COLON);
if(isCreateOutputFile()) {
schema.setOutputFile(getSchemaOutputPath());
}
schema.create(true, false);
}
}
public static boolean isPrintSchemaEnabled() {
return Boolean.valueOf(getResourceBundle().getString(PRINT_SCHEMA_ENABLED));
}
public static boolean isCreateOutputFile() {
return Boolean.valueOf(getResourceBundle().getString(OUTPUT_FILE_ENABLED));
}
public static String getSchemaOutputPath() {
return getResourceBundle().getString(SCHEMA_OUTPUT_PATH);
}
@Autowired
private Configuration hibernateConfiguration;
public static final String PRINT_SCHEMA_ENABLED = "enablePrintSchema";
public static final String SCHEMA_OUTPUT_PATH = "schemaOutputPath";
public static final String OUTPUT_FILE_ENABLED = "enableSchemaOutputFile";
private static final Logger log = LoggerFactory.getLogger(SchemaExportListener.class);
}
至于更多XML,并非都相关,但既然被要求:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--Annotated classes redacted -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="hikariConfig" />
</bean>
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="springHikariConnectionPool" />
<property name="connectionTestQuery" value="SELECT 1" />
<property name="dataSourceClassName" value="${dataSource.dataSourceClassName}" />
<property name="maximumPoolSize" value="10" />
<property name="idleTimeout" value="30000" />
<property name="dataSourceProperties">
<props>
<prop key="url">${dataSource.url}</prop>
<prop key="user">${dataSource.username}</prop>
<prop key="password">${dataSource.password}</prop>
</props>
</property>
</bean>
您可以直接自动装配 LocalSessionFactoryBean
并获取配置
@Autowired
private LocalSessionFactoryBean localSessionFactoryBean;
@Bean(name = "hibernateConfig")
public org.hibernate.cfg.Configuration getConfig() {
return localSessionFactoryBean.getConfiguration();
}
不要认为您需要在 java config.In 中提及工厂 bean 或方法,您的 class 连接 class 中的 sessionFactory 并查看如果有效
@Component
public class SchemaExportListener extends AbstractEnvironment implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
private LocalSessionFactoryBean localSessionFactoryBean;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
log.debug("onApplicationEvent");
if (isPrintSchemaEnabled()) {
log.info("printing schema");
SchemaExport schema = new SchemaExport(localSessionFactoryBean.getConfiguration());
schema.setDelimiter(BaseConstants.SEMI_COLON);
在Java配置中class
@Autowired
private LocalSessionFactoryBean localSessionFactoryBean;
@Bean
public DataSource getDataSource() {
//return the data source here
}
@Bean(name = "hibernateConfig")
public org.hibernate.cfg.Configuration getConfig() {
return localSessionFactoryBean.getConfiguration();
}
如果会话对象的自动装配失败,请尝试在配置中创建对象,如下所示
@Autowired
@Bean
public LocalSessionFactoryBean getSessionFactoryBean(DataSource dataSource) {
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
sessionFactory.setDataSource(this.getDataSource());
//sessionFactory.setHibernateProperties(this.hibernateProperties());
return localSessionFactoryBean;
}
其余配置请参考一些指南或示例
这里有几个 example1 example2
希望这能解决您的问题
我希望将此 XML 转换为 Java 配置 Spring:
<bean id="hibernateConfiguration" factory-bean="&sessionFactory"
factory-method="getConfiguration" />
我在想...
@Bean
public org.hibernate.cfg.Configuration configuration() {
org.hibernate.cfg.Configuration config = new org.hibernate.cfg.Configuration();
//uhhh...
return config;
}
如何指定 factory-bean
和 factory-method
参数?试着环顾四周,但没有运气。
旁白:&
告诉 Spring 获取实际的 bean
更新:
下面是我使用 bean 的方式。它是一个打印模式的侦听器。
@Component
public class SchemaExportListener extends AbstractEnvironment implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
log.debug("onApplicationEvent");
if (isPrintSchemaEnabled()) {
log.info("printing schema");
SchemaExport schema = new SchemaExport(hibernateConfiguration);
schema.setDelimiter(BaseConstants.SEMI_COLON);
if(isCreateOutputFile()) {
schema.setOutputFile(getSchemaOutputPath());
}
schema.create(true, false);
}
}
public static boolean isPrintSchemaEnabled() {
return Boolean.valueOf(getResourceBundle().getString(PRINT_SCHEMA_ENABLED));
}
public static boolean isCreateOutputFile() {
return Boolean.valueOf(getResourceBundle().getString(OUTPUT_FILE_ENABLED));
}
public static String getSchemaOutputPath() {
return getResourceBundle().getString(SCHEMA_OUTPUT_PATH);
}
@Autowired
private Configuration hibernateConfiguration;
public static final String PRINT_SCHEMA_ENABLED = "enablePrintSchema";
public static final String SCHEMA_OUTPUT_PATH = "schemaOutputPath";
public static final String OUTPUT_FILE_ENABLED = "enableSchemaOutputFile";
private static final Logger log = LoggerFactory.getLogger(SchemaExportListener.class);
}
至于更多XML,并非都相关,但既然被要求:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--Annotated classes redacted -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="hikariConfig" />
</bean>
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="springHikariConnectionPool" />
<property name="connectionTestQuery" value="SELECT 1" />
<property name="dataSourceClassName" value="${dataSource.dataSourceClassName}" />
<property name="maximumPoolSize" value="10" />
<property name="idleTimeout" value="30000" />
<property name="dataSourceProperties">
<props>
<prop key="url">${dataSource.url}</prop>
<prop key="user">${dataSource.username}</prop>
<prop key="password">${dataSource.password}</prop>
</props>
</property>
</bean>
您可以直接自动装配 LocalSessionFactoryBean
并获取配置
@Autowired
private LocalSessionFactoryBean localSessionFactoryBean;
@Bean(name = "hibernateConfig")
public org.hibernate.cfg.Configuration getConfig() {
return localSessionFactoryBean.getConfiguration();
}
不要认为您需要在 java config.In 中提及工厂 bean 或方法,您的 class 连接 class 中的 sessionFactory 并查看如果有效
@Component
public class SchemaExportListener extends AbstractEnvironment implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
private LocalSessionFactoryBean localSessionFactoryBean;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
log.debug("onApplicationEvent");
if (isPrintSchemaEnabled()) {
log.info("printing schema");
SchemaExport schema = new SchemaExport(localSessionFactoryBean.getConfiguration());
schema.setDelimiter(BaseConstants.SEMI_COLON);
在Java配置中class
@Autowired
private LocalSessionFactoryBean localSessionFactoryBean;
@Bean
public DataSource getDataSource() {
//return the data source here
}
@Bean(name = "hibernateConfig")
public org.hibernate.cfg.Configuration getConfig() {
return localSessionFactoryBean.getConfiguration();
}
如果会话对象的自动装配失败,请尝试在配置中创建对象,如下所示
@Autowired
@Bean
public LocalSessionFactoryBean getSessionFactoryBean(DataSource dataSource) {
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
sessionFactory.setDataSource(this.getDataSource());
//sessionFactory.setHibernateProperties(this.hibernateProperties());
return localSessionFactoryBean;
}
其余配置请参考一些指南或示例 这里有几个 example1 example2
希望这能解决您的问题