我希望将此XML转换为Java Config for 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>
答案 0 :(得分:2)
不要认为你需要在java配置中提及工厂bean或方法。在你的类中连接类中的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配置类
中@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
希望这能解决您的问题
答案 1 :(得分:0)
您只需自动装配LocalSessionFactoryBean
并获取配置
@Autowired
private LocalSessionFactoryBean localSessionFactoryBean;
@Bean(name = "hibernateConfig")
public org.hibernate.cfg.Configuration getConfig() {
return localSessionFactoryBean.getConfiguration();
}