繁体   English   中英

直接调用@Bean注解的方法

[英]Method annotated with @Bean is called directly

我有一个配置文件,其中包含下面提供的 2 个 Bean:

    @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;
    }

我收到一个 hibenrateInterpretor 错误,直接调用了用hibenrateInterpretor注释的方法。 改用依赖注入。

这不会阻止我编译,但我如何摆脱这个错误?

您可以将 bean 作为参数传递给第二个 bean,如下所示:

@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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM