Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LocalContainerEntityManagerFactoryBean.afterPropertiesSet() not called automatically post bean construction. [SPR-17612] #22144

Closed
spring-issuemaster opened this issue Dec 19, 2018 · 1 comment
Assignees

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Dec 19, 2018

Kshitij Pandey opened SPR-17612 and commented

I have following Java configuration Bean and the instance returned by it is null, which should not be the case if the "afterPropertiesSet()" method of the bean would have been called automatically, given that the bean class being created is implementing "InitializingBean" interface.

@Configuration
@ComponentScan(basePackages = {"com.project.src"})
@EnableJpaRepositories(
basePackages="com.*.repository",
transactionManagerRef="scribetransactionManager",
entityManagerFactoryRef="entityManagerFactory" )

public class JpaConfig@Bean("entityManagerFactory")
public EntityManagerFactory getEntityManagerFactory()

{ LocalContainerEntityManagerFactoryBean lcemfb = new LocalContainerEntityManagerFactoryBean(); Properties properties = new Properties(); properties.put("hibernate.dialect", "org.hibernate.dialect.DB2Dialect"); properties.put("hibernate.show_sql", "false"); properties.put("hibernate.jdbc.batch_size", "5000"); lcemfb.setJpaProperties(properties); lcemfb.setDataSource(getDataSource()); lcemfb.setPackagesToScan(new String[] {"com.**"}

);

lcemfb.setPersistenceUnitName("persistence-unit");
lcemfb.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
//lcemfb.afterPropertiesSet();
return lcemfb.getObject();
}

 
@Bean("dataSource")
public DB2DataSource getScribeMainDataSource() {
DB2DataSource db2ds = new DB2DataSource();
Map<String, String> cpm = new HashMap<>();
pm.put("retrieveMessagesFromServerOnGetMessage", true);
b2ds.setConnectionProperties(cpm);
db2ds.setDatabaseServer(mainDatabaseServer);
db2ds.setSchema(mainDatabaseSchema);
db2ds.setKerberized(true);
db2ds.setMinIdle(0);
db2ds.setMaxIdle(2);
db2ds.setMaxActive(2);
db2ds.setPerformValidation(true);
db2ds.setValidationTimeout(60000);
db2ds.setBackgroundCleanupPeriod(30000);
return db2ds;
}

}

Affects: 4.3.16

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Dec 20, 2018

Juergen Hoeller commented

You're manually handling the FactoryBean.getObject() contract on LocalContainerEntityManagerFactoryBean there, so the container never really sees the FactoryBean instance itself and you have to manually call afterPropertiesSet indeed.

You'd have to return the LocalContainerEntityManagerFactoryBean instance from your @Bean method in order for the container to call afterPropertiesSet etc on it, with the container subsequently calling getObject() whenever the actual EntityManagerFactory needs to be injected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.