简体   繁体   English

从Migrate迁移到Spring MVC 4 + Hibernate5

[英]Migrating from Migrate to Spring MVC 4 + Hibernate5

I'm migrating an application from Jersey 1.18, Spring 3, Hibernate 3.6, Maven to a completely new architecture: Spring 4.3 (via Spring Boot), Hibernate 5 and Gradle . 我正在将应用程序从Jersey 1.18,Spring 3,Hibernate 3.6,Maven迁移到全新的体系结构: Spring 4.3 (通过Spring Boot), Hibernate 5Gradle

During the "porting" of the Spring application.xml to a Code-Base configuration, I'm facing the following exception: 在将Spring application.xml “移植”到代码库配置期间,我面临以下异常:

Error creating bean with name 'jpaContext': Unsatisfied dependency ... No qualifying bean found for dependency [java.util.Set] 创建名称为'jpaContext'的bean时出错:依赖关系未满足 ...未找到依赖项的合格Bean [java.util.Set]

Here the long stacktrace 这是长堆栈跟踪

2017-03-13 19:44:42.814  WARN 40010 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jpaContext': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [java.util.Set<javax.persistence.EntityManager>]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2017-03-13 19:44:42.816  INFO 40010 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
2017-03-13 19:44:42.825  INFO 40010 --- [           main] o.apache.catalina.core.StandardService   : Stopping service Tomcat
2017-03-13 19:44:42.860  INFO 40010 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-03-13 19:44:42.995 ERROR 40010 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in org.springframework.data.jpa.repository.support.DefaultJpaContext required a bean of type 'java.util.Set' that could not be found.


Action:

Consider defining a bean of type 'java.util.Set' in your configuration.

The fact is that I didn't define the DefaultJpaContext in my code: 事实是我没有在代码中定义DefaultJpaContext:

@Configuration
@EnableTransactionManagement
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class DatabaseConfig {

    @Autowired private Environment env;
    @Autowired private DataSource dataSource;
    @Autowired private SessionFactory sessionFactory;

    private static final String DB_DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
    private static final String HIBERNATE_DIALECT    = "org.hibernate.dialect.MySQL5Dialect";
    private static final String HIBERNATE_VALIDATION = "validate";
    private static final String SHOW_SQL             = "false";
    private static final String ENTITY_PACKAGE       = MainEntity.class.getPackage().getName();

    final Logger log = LoggerFactory.getLogger(DatabaseConfig.class);


    @Bean
    public DataSource dataSource() {
        final String driverName = env.getProperty("db.driver", DB_DRIVER_CLASS_NAME);
        final String url        = env.getProperty("db.url");
        final String username   = env.getProperty("db.username");
        final String password   = env.getProperty("db.password");
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();

        if (log.isInfoEnabled()) {
            log.info("Database Configuration:");
            log.info("Setting driverName to {}", driverName);
            log.info("Setting url to {}", url);
            log.info("Setting username to {}", username);
            if (StringUtils.isEmpty(password)) log.warn("Password not specified");
            else log.info("Setting password");
        }

        dataSource.setDriverClassName(driverName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean
    public LocalSessionFactoryBean sessionFactory(){
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setPackagesToScan(new String[] {ENTITY_PACKAGE});

        // Hibernate properties
        Properties additionalProperties = new Properties();
        additionalProperties.put("hibernate.dialect"     , env.getProperty("db.dialect"   , HIBERNATE_DIALECT));
        additionalProperties.put("hibernate.hbm2ddl.auto", env.getProperty("db.validation", HIBERNATE_VALIDATION));
        additionalProperties.put("hibernate.show_sql"    , env.getProperty("db.sql.show"  , SHOW_SQL));

        sessionFactory.setHibernateProperties(additionalProperties);
        return sessionFactory;
    }

    @Bean
    public HibernateTransactionManager transactionManager(){
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(sessionFactory);
        return transactionManager;
    }
}

Update 更新资料

Here it is a piece of gradle build: 这是一个构建过程:

compile('org.springframework.boot:spring-boot-starter-data-jpa')
// ...
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-web')
compile group: 'org.hibernate'        , name: 'hibernate-core'     , version: '5.2.8.Final'
// ...

What I'm missing? 我想念的是什么?

Thanks, Davide. 谢谢戴维。

Based on the spring boot error message, it fails to build the DefaultJpaContext bean whose constructor requires Set<javax.persistence.EntityManager> entityManagers . 基于spring boot错误消息,它无法构建其构造函数需要Set<javax.persistence.EntityManager> entityManagersDefaultJpaContext bean。

Looks like you have Spring-Data dependency in your pom.xml (or build.gradle ) which requires the jpa EntityManagerFactory while you have Hibernate( LocalSessionFactoryBean ) stuff. 看起来您的pom.xml (或build.gradle )中具有Spring-Data依赖项,而在拥有Hibernate( LocalSessionFactoryBean )时,这需要jpa EntityManagerFactory Do you have to have DefaultJpaContext bean? 您是否必须具有DefaultJpaContext bean?

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

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