启动Spring Boot示例应用程序时出现问题

时间:2018-11-27 14:29:29

标签: java spring-boot

我正在尝试启动示例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,该bean   找不到。行动:考虑定义一个名为   配置中的“ entityManagerFactory”。

添加此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]

我的application.properties:

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);
    }
}

0 个答案:

没有答案