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-projects-issues opened this issue Dec 19, 2018 · 1 comment
Assignees
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) status: invalid An issue that we don't feel is valid

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues 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-projects-issues
Copy link
Collaborator Author

spring-projects-issues 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.

@spring-projects-issues spring-projects-issues added type: bug A general bug status: invalid An issue that we don't feel is valid in: data Issues in data modules (jdbc, orm, oxm, tx) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues removed the type: bug A general bug label Jan 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

2 participants