I'm looking to convert this XML to Java Config for Spring:
<bean id="hibernateConfiguration" factory-bean="&sessionFactory"
factory-method="getConfiguration" />
And I'm thinking...
@Bean
public org.hibernate.cfg.Configuration configuration() {
org.hibernate.cfg.Configuration config = new org.hibernate.cfg.Configuration();
//uhhh...
return config;
}
How would I specify the factory-bean
and factory-method
params? Tried looking around on SO, but no luck.
Aside: the &
tells Spring to fetch the actual bean
UPDATE:
Here is how I am using the bean. It's a listener that prints out the schema.
@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);
}
As for more XML, it's not all relevant, but since it was requested:
<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>
Don't think that you'll need to mention the factory bean or methods in the java config.In your class wire the sessionFactory in class and see if it works
In Java configuration class
If the autowire of session object fails then try to create on Object in the config like below
For the rest of the configuration refer to some guide or example Here are few example1 example2
Hope this will solve your issue