簡體   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