简体   繁体   English

设置构造函数参数时无法解析对bean“entityManagerFactory”的引用;

[英]Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument;

I am getting this error in my code. 我在我的代码中收到此错误。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleRepository': Cannot create inner bean '(inner bean)#7540dc57' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; org.springframework.beans.factory.BeanCreationException:创建名为'roleRepository'的bean时出错:在设置bean属性'entityManager时,无法创建[org.springframework.orm.jpa.SharedEntityManagerCreator]类型的内部bean'(内部bean)#7540dc57' “; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#7540dc57': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; 嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'(内部bean)#7540dc57'的bean时出错:在设置构造函数参数时无法解析对bean'entalManagerFactory'的引用; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为'entityManagerFactory'的bean可用

I saw these: 我看到了这些:

Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument 在设置构造函数参数时无法解析对bean'entalManagerFactory'的引用

NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available NoSuchBeanDefinitionException:没有名为“entityManagerFactory”的bean可用

NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined NoSuchBeanDefinitionException:没有定义名为“entityManagerFactory”的bean

None of them answers my question. 他们都没有回答我的问题。 The thing is I was able the solve the problem but I have a question about it. 问题是我能够解决问题,但我有一个问题。

Let me share my related code and then ask the question I have. 让我分享我的相关代码,然后问我的问题。

@Configuration
@EnableTransactionManagement 
public class HibernateConfig {

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerF() {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());
    em.setPackagesToScan(new String[] {"com.gitreporter"});
    JpaVendorAdapter jpaAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(jpaAdapter);
    em.setJpaProperties(jpaProperties());

    return em;
}

@Bean
public PlatformTransactionManager jpaTransactionManager(EntityManagerFactory emf) {
    JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
    jpaTransactionManager.setEntityManagerFactory(emf);

    return jpaTransactionManager;
}

private final Properties jpaProperties() {
    Properties properties = new Properties();
    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");

    return properties;
}


@Bean
public DataSource dataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/MyDBNAME?useSSL=false");
    dataSource.setUsername("username");
    dataSource.setPassword("password");

    return dataSource;
}

The problem was on this line: 问题出在这条线上:

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerF() {

I changed the medhod name to entityManagerFactory like so: 我将medhod名称更改为entityManagerFactory,如下所示:

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

Making the name of the factory bean in the context equal to "entityManagerFactory" since by default the name of the bean will be equal to the method name unless explicitly specified. 使上下文中的工厂bean的名称等于“entityManagerFactory”,因为默认情况下,除非明确指定,否则bean的名称将等于方法名称。

My question: Is there a place in JPA API that "by convention" it is looking for an EntityManagerFactory bean named "entityManagerFactory" inside Spring container? 我的问题:JPA API中是否存在“按惯例”它在Spring容器中寻找名为“entityManagerFactory”的EntityManagerFactory bean? Why is it not working when the name of the method is "entityManagerF"? 当方法的名称是“entityManagerF”时,为什么它不起作用?

Here is the rest of the code: 这是代码的其余部分:

@NoRepositoryBean
public interface GenericRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {

public List<T> findByAttributeContainsText(String attributeName, String text);

}

public class GenericRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID>
    implements GenericRepository<T, ID> {

private EntityManager entityManager;

public GenericRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
    super(entityInformation, entityManager);
    this.entityManager = entityManager;
 }
}


public interface RoleRepository extends GenericRepository<Role, Long> {

}

I found the answer. 我找到了答案。

Checkout the documentation for @EnableJpaRepositories annotation. 查看@EnableJpaRepositories注释的文档

In the optional elements you will see this: 在可选元素中,您将看到:

entityManagerFactoryRef Configures the name of the EntityManagerFactory bean definition to be used to create repositories discovered through this annotation. entityManagerFactoryRef配置要用于创建通过此批注发现的存储库的EntityManagerFactory bean定义的名称。

Go down the page to the details and you will see this: 在页面下方查看详细信息,您将看到:

entityManagerFactoryRef entityManagerFactoryRef

public abstract String entityManagerFactoryRef public abstract String entityManagerFactoryRef

Configures the name of the EntityManagerFactory bean definition to be used to create repositories discovered through this annotation . 配置要用于创建通过此批注发现的存储库的EntityManagerFactory bean定义的名称。 Defaults to entityManagerFactory . 默认为entityManagerFactory

Returns: 返回:

Default: "entityManagerFactory" 默认值:“entityManagerFactory”

So this "conventional" default configuration comes from @EnableJpaRepositories annotation itself. 因此,这种“常规”默认配置来自@EnableJpaRepositories注释本身。

Yes I believe so. 是的我相信。 In your case, Spring did have pre-configured a bean entityManagerFactory . 在您的情况下,Spring确实预先配置了一个bean entityManagerFactory

An excerpt from the javadoc for @EnableAutoConfiguration says 来自@EnableAutoConfiguration的javadoc的摘录说

        Enable auto-configuration of the Spring Application Context, attempting to guess and configure beans that you are likely to need. Auto-configuration classes are usually applied based on your classpath and what beans you have defined. For example, if you have tomcat-embedded.jar on your classpath you are likely to want a TomcatServletWebServerFactory (unless you have defined your own ServletWebServerFactory bean).

And, seeing your hibernate configuration, 并且,看到你的休眠配置,

private final Properties jpaProperties() {
    Properties properties = new Properties();
    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");

    return properties;

... I reckon, this must get invoked, which is derived from JpaBaseConfiguration ...我想,这必须被调用,这是从JpaBaseConfiguration派生的

@Configuration
@ConditionalOnSingleCandidate(DataSource.class)
class HibernateJpaConfiguration extends JpaBaseConfiguration {

And JpaBaseConfiguration did have a bean definition for entityManagerFactory , which is what you are trying to override. 并且JpaBaseConfiguration确实有entityManagerFactory的bean定义,这是你试图覆盖的。

@Bean
@Primary
@ConditionalOnMissingBean({ LocalContainerEntityManagerFactoryBean.class,
        EntityManagerFactory.class })
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
        EntityManagerFactoryBuilder factoryBuilder) {
    Map<String, Object> vendorProperties = getVendorProperties();
    customizeVendorProperties(vendorProperties);
    return factoryBuilder.dataSource(this.dataSource).packages(getPackagesToScan())
            .properties(vendorProperties).mappingResources(getMappingResources())
            .jta(isJta()).build();
}

Edit:- Thanks to OP's answer. 编辑: -感谢OP的回答。 So, it can even be used to provide a custom bean name through a declaration like 因此,它甚至可以用于通过类似的声明提供自定义bean名称

@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerF",
)         

Another stackoverflow thread at https://stackoverflow.com/a/45665826/5107365 provides a deeper insight into this issue. https://stackoverflow.com/a/45665826/5107365上的另一个stackoverflow线程提供了对此问题的更深入的了解。

暂无
暂无

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

相关问题 Spring 运行时错误 - 设置构造函数参数时无法解析对 bean &#39;entityManagerFactory&#39; 的引用 - Spring Runtime Error - Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument Spring Security:设置构造函数参数时无法解析对bean的引用 - Spring security : Cannot resolve reference to bean while setting constructor argument 无法解析对 bean“jpaSharedEM_entityManagerFactory”的引用 - Cannot resolve reference to bean 'jpaSharedEM_entityManagerFactory' 设置bean属性'userDetailsS​​ervice'时无法解析对bean的引用 - Cannot resolve reference to bean while setting bean property 'userDetailsService' 设置 bean 属性“mongoOperations”时无法解析对 bean“mongoTemplate”的引用 - Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations 设置 bean 属性“mongoOperations”时无法解析对 bean“mongoTemplate”的引用 - Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations' 创建名称为“ mongoTransactionManager”的bean时出错:设置bean属性“ datastore”时无法解析对bean“ mongoDatastore”的引用 - Error creating bean with name 'mongoTransactionManager': Cannot resolve reference to bean 'mongoDatastore' while setting bean property 'datastore' 在部署 AWS Lambda 时设置 bean 属性“mongoOperations”时无法解析对 bean“mongoTemplate”的引用 - Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations' while deploying AWS Lambda 使用键[0]设置bean属性“ sourceList”时,无法解析对bean&#39;org.springframework.security.web.DefaultSecurityFilterChain#0&#39;的引用 - cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#0' while setting bean property 'sourceList' with key [0] 设置bean属性&#39;dataSource&#39;时,无法解析对bean&#39;DataSource&#39;的引用.factory.NoSuchBeanDefinitionException: - Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource' .factory.NoSuchBeanDefinitionException:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM