gpt4 book ai didi

java - 从Migrate迁移到Spring MVC 4 + Hibernate5

转载 作者:行者123 更新时间:2023-12-03 05:59:18 27 4
gpt4 key购买 nike

我正在将应用程序从Jersey 1.18,Spring 3,Hibernate 3.6,Maven迁移到全新的架构: Spring 4.3 (通过Spring Boot), Hibernate 5 Gradle

在将Spring application.xml 移植到“代码库”配置期间,我面临以下异常:

创建名称为'jpaContext'的bean时出错:不满意的依赖关系 ... 未找到依赖项的合格bean [java.util.Set]

这是长堆栈跟踪

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.

事实是我没有在代码中定义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;
}
}

更新

这是一个构建过程:
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'
// ...

我想念的是什么?

谢谢戴维。

最佳答案

根据spring boot错误消息,它无法构建其构造函数需要DefaultJpaContextSet<javax.persistence.EntityManager> entityManagers bean。

看起来您的Spring-Data(或pom.xml)中具有build.gradle依赖项,而在您拥有Hibernate(EntityManagerFactory)内容时,这需要jpa LocalSessionFactoryBean。您是否需要DefaultJpaContext bean?

关于java - 从Migrate迁移到Spring MVC 4 + Hibernate5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42771683/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com