相关疑难解决方法(0)

Spring数据jpa-没有定义名为'entityManagerFactory'的bean; 注入自动连接的依赖项失败

我正在使用spring数据jpa,hibernate,mysql,tomcat7,maven开发应用程序,这就是创建错误.我想弄明白但是我失败了.

错误是在设置构造函数参数时无法解析对bean'entalManagerFactory'的引用; 没有定义名为'entityManagerFactory'的bean; 注入自动连接的依赖项失败

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'initDbService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.wahid.cse.repository.RoleRepository org.wahid.cse.service.InitDbService.roleRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleRepository': Cannot create inner bean '(inner bean)#c08f81' 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)#c08f81': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' …
Run Code Online (Sandbox Code Playgroud)

java jpa spring-mvc spring-data spring-data-jpa

62
推荐指数
5
解决办法
10万
查看次数

spring boot 连接多个数据库

我需要连接到我的项目中的两个数据库。所以我创建了两个配置文件。

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories( basePackages = {"com.virat.webservices.datastore.v1.Repo" } )
@EntityScan("com.virat.webservices.datastore.v1.Model")
public class V1DBConfig {

@Primary
@Bean( name = "dataSource" )
@ConfigurationProperties( prefix = "v1.datasource" )
public DataSource dataSource()
{
    return DataSourceBuilder.create().build();
}

@Primary
@Bean( name = "entityManagerFactory" )
public LocalContainerEntityManagerFactoryBean entityManagerFactory( EntityManagerFactoryBuilder builder,
        @Qualifier( "dataSource" ) DataSource dataSource )
{
    return builder.dataSource(dataSource).packages("com.virat.webservices.datastore.v1.Model")
            .persistenceUnit("db1").build();
}

@Primary
@Bean( name = "transactionManager" )
public PlatformTransactionManager transactionManager(
        @Qualifier( "entityManagerFactory" ) EntityManagerFactory entityManagerFactory )
{
    return new JpaTransactionManager(entityManagerFactory);
}
}
Run Code Online (Sandbox Code Playgroud)

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories( entityManagerFactoryRef …
Run Code Online (Sandbox Code Playgroud)

java spring-data spring-boot

6
推荐指数
2
解决办法
6692
查看次数

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

我的代码中出现此错误。

org.springframework.beans.factory.BeanCreationException:创建名称为'roleRepository'的bean时出错:设置bean属性'entityManager时,无法创建类型为[org.springframework.orm.jpa.SharedEntityManagerCreator]的内部bean'(inner bean)#7540dc57' '; 嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名称为“((内部bean)#7540dc57”)的bean时出错:在设置构造函数参数时无法解析对bean'entityManagerFactory'的引用;嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的名为'entityManagerFactory'的bean

我看到了这些:

设置构造函数参数时,无法解析对bean'entityManagerFactory'的引用

NoSuchBeanDefinitionException:没有可用的名为“ entityManagerFactory”的bean

NoSuchBeanDefinitionException:未定义名为“ entityManagerFactory”的Bean

他们都没有回答我的问题。问题是我能够解决问题,但对此有疑问。

让我分享我的相关代码,然后提出我的问题。

@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 …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate spring-mvc spring-data-jpa

6
推荐指数
1
解决办法
1756
查看次数

Spring Data JPA:将 @EnableJpaRepositories basePackages 配置外部化到属性文件

我想外部化 @EnableJPARepositories basePackages 的配置。

我下面有两个不同的示例包

  • com.project.ph.dao
  • 示例.project.jpa.repositories

我尝试了下面的属性外部化(不适用于多个包)

项目配置类

@EnableJpaRepositories(basePackages = {"${basePackages}"})
Run Code Online (Sandbox Code Playgroud)

配置属性

basePackages=com.project.ph.dao,sample.project.jpa.repositories
Run Code Online (Sandbox Code Playgroud)

有没有其他方法可以为多个包外部化此配置?

谢谢!

java spring spring-data spring-data-jpa spring-repositories

2
推荐指数
1
解决办法
2471
查看次数

没有可用的“javax.sql.DataSource”类型的合格 bean:在候选对象中找到了多个“主要”bean:

我想配置 Spring Boot 以使用 2 个 JNDI 数据源。我试过这个配置:

应用程序属性

spring.production-datasource.jndi-name=java:/global/production_gateway
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update

spring.warehouse-datasource.jndi-name=java:/global/production_warehouse
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
Run Code Online (Sandbox Code Playgroud)

主数据库

@Configuration
@EnableJpaRepositories(
        basePackages = "org.datalis.plugin.production.entity", 
        entityManagerFactoryRef = "productionEntityManager", 
        transactionManagerRef = "productionTransactionManager"
    )
@EnableTransactionManagement
public class ContextProductionDatasource {

    @Autowired
    private Environment env;

    @Primary
    @Bean
    @ConfigurationProperties(prefix="spring.production-datasource")
    public DataSource productionDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean   
    public EntityManager productionEntityManager(EntityManagerFactory emf) {
        return emf.createEntityManager();
    }

    @Primary
    @Bean
    public PlatformTransactionManager productionTransactionManager(final EntityManagerFactory emf) {
        final JpaTransactionManager transactionManager …
Run Code Online (Sandbox Code Playgroud)

java spring spring-data-jpa spring-boot

0
推荐指数
1
解决办法
2533
查看次数