我正在尝试启动示例 Spring Boot 2 应用程序https://dzone.com/articles/spring-boot-jpa-mysql-sample-app-code-example,我的问题很简单,如何纠正它以便可以部署,我只是想看看它运行?并理解这个问题,这不是我造成的,因为我需要知道什么是正确的生产配置。
Method requestMappingHandlerMapping in org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration required a single bean, but 2 were found:
- sessionFactory: defined by method 'sessionFactory' in class path resource [hello/MySQLAppConfig.class]
- entityManagerFactory: defined by method 'entityManagerFactory' in class path resource [hello/MySQLAppConfig.class]
该示例未定义 entityManagerFactory bean
****************************** 应用程序无法启动 ******************* ******** 描述:hello.MainController 中的字段 userRepository 需要一个名为“entityManagerFactory”的 bean,但无法找到。行动:考虑在您的配置中定义一个名为“entityManagerFactory”的bean。
添加这个bean
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
EntityManagerFactoryBuilder builder) {
LocalContainerEntityManagerFactoryBean em = builder
.dataSource(getDataSource())
.packages("hello")
.build();
return em;
}
导致有两个这样的 bean 的错误 ??: 它就像 sessionFactoryBean 和 entityManagerBeans 正在实现相同的接口
Method requestMappingHandlerMapping in org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration required a single bean, but 2 were found:
- sessionFactory: defined by method 'sessionFactory' in class path resource [hello/MySQLAppConfig.class]
- entityManagerFactory: defined by method 'entityManagerFactory' in class path resource [hello/MySQLAppConfig.class]
我的应用程序属性:
app.datasource.driverClassName=com.mysql.jdbc.Driver
app.datasource.url=jdbc:mysql://localhost:3306/test
app.datasource.username=test
app.datasource.password=test
我有这个配置 Java 类:
package hello;
import javax.sql.DataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EntityScan("hello")
@EnableTransactionManagement
@PropertySource("classpath:application.properties")
public class MySQLAppConfig {
@Value("${app.datasource.driverClassName}") String driverClassName;
@Value("${app.datasource.url}") String url;
@Value("${app.datasource.username}") String username;
@Value("${app.datasource.password}") String password;
@Bean(name = "dataSource")
public DataSource getDataSource() {
DataSource dataSource = DataSourceBuilder
.create()
.username(username)
.password(password)
.url(url)
.driverClassName(driverClassName)
.build();
return dataSource;
}
@Bean(name = "sessionFactory")
public SessionFactory getSessionFactory(DataSource dataSource) {
LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
sessionBuilder.scanPackages("hello");
return sessionBuilder.buildSessionFactory();
}
@Bean(name = "transactionManager")
public HibernateTransactionManager getTransactionManager(
SessionFactory sessionFactory) {
HibernateTransactionManager transactionManager = new HibernateTransactionManager(
sessionFactory);
return transactionManager;
}
@Bean
public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) {
final DataSourceInitializer initializer = new DataSourceInitializer();
initializer.setDataSource(dataSource);
return initializer;
}
}
我有这个 MainController 类:
@Controller // This means that this class is a Controller
@RequestMapping(path="/demo") // This means URL's start with /demo (after Application path)
public class MainController {
@Autowired // This means to get the bean called userRepository
// Which is auto-generated by Spring, we will use it to handle the data
private UserRepository userRepository;
最后我有了这个 pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-mysql-data</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
以及应用程序本身:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}