java – 设置构造函数参数时无法解析对bean’entalManagerFactory’的引用;
我在我的代码中收到此错误.
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’; 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; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
bean named ‘entityManagerFactory’ available
我看到了这些:
Cannot resolve reference to bean ‘entityManagerFactory’ while setting constructor argument
NoSuchBeanDefinitionException: No bean named ‘entityManagerFactory’ available
NoSuchBeanDefinitionException: No bean named ‘entityManagerFactory’ is defined
他们都没有回答我的问题.问题是我能够解决问题,但我有一个问题.
让我分享我的相关代码,然后问我的问题.
@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;
}
问题出在这条线上:
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerF() {
我将medhod名称更改为entityManagerFactory,如下所示:
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
使上下文中的工厂bean的名称等于“entityManagerFactory”,因为默认情况下,除非明确指定,否则bean的名称将等于方法名称.
我的问题:JPA API中是否存在“按惯例”它在Spring容器中寻找名为“entityManagerFactory”的EntityManagerFactory bean?当方法的名称是“entityManagerF”时,为什么它不起作用?
这是代码的其余部分:
@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> {
}
最佳答案 我找到了答案.
查看documentation以获取@EnableJpaRepositories注释.
在可选元素中,您将看到:
entityManagerFactoryRef
Configures the name of the EntityManagerFactory bean definition to be used to create repositories discovered through this annotation.
在页面下方查看详细信息,您将看到:
entityManagerFactoryRef
public abstract String entityManagerFactoryRef
Configures the name of
the EntityManagerFactory bean definition to be used to create
repositories discovered through this annotation. Defaults to
entityManagerFactory.Returns:
Default: “entityManagerFactory”
因此,这种“常规”默认配置来自@EnableJpaRepositories注释本身.