Reputation: 12427
I have a configuration file with 2 Beans provided below:
@Bean
public EmptyInterceptor hibernateInterceptor() {
return new EmptyInterceptor() {
@Override
public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
if (entity instanceof TenantSupport) {
((TenantSupport) entity).setTenantId(TenantContext.getCurrentTenant());
}
}
@Override
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
if (entity instanceof TenantSupport) {
((TenantSupport) entity).setTenantId(TenantContext.getCurrentTenant());
}
return false;
}
@Override
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
if (entity instanceof TenantSupport) {
((TenantSupport) entity).setTenantId(TenantContext.getCurrentTenant());
}
return false;
}
};
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder factory, DataSource dataSource, JpaProperties properties) {
Map<String, Object> jpaPropertiesMap = new HashMap<>(jpaProperties.getProperties());
jpaPropertiesMap.put("hibernate.ejb.interceptor", hibernateInterceptor());
LocalContainerEntityManagerFactoryBean factoryBean = factory.dataSource(dataSource).packages("com.monytyz.billing").properties(jpaPropertiesMap).build();
return factoryBean;
}
I get an error for the hibenrateInterpretor
that Method annotated with @Bean is called directly. Use dependency injection instead.
This doesn't prevent me from compiling but how do I get rid of this error?
Upvotes: 2
Views: 4004
Reputation: 235
You can pass the bean as a parameter to the second bean, like this:
@Bean
public EmptyInterceptor hibernateInterceptor() {
return new EmptyInterceptor() {
@Override
public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
if (entity instanceof TenantSupport) {
((TenantSupport) entity).setTenantId(TenantContext.getCurrentTenant());
}
}
@Override
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
if (entity instanceof TenantSupport) {
((TenantSupport) entity).setTenantId(TenantContext.getCurrentTenant());
}
return false;
}
@Override
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
if (entity instanceof TenantSupport) {
((TenantSupport) entity).setTenantId(TenantContext.getCurrentTenant());
}
return false;
}
};
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder factory, DataSource dataSource, JpaProperties properties, EmptyInterceptor hibernateInterceptor) {
Map<String, Object> jpaPropertiesMap = new HashMap<>(jpaProperties.getProperties());
jpaPropertiesMap.put("hibernate.ejb.interceptor", hibernateInterceptor);
LocalContainerEntityManagerFactoryBean factoryBean = factory.dataSource(dataSource).packages("com.dddytyz.billing").properties(jpaPropertiesMap).build();
return factoryBean;
}
Upvotes: 2