找不到EntityManger bean?

时间:2017-11-21 10:57:47

标签: spring-boot spring-data

ConfigServletWebServerApplicationContext:刷新org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@769f71a9:启动日期[Tue Nov 21 15:46:02 IST 2017];上下文层次结构的根 2017-11-21 15:46:24.774 INFO 6272 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer:使用端口初始化的Tomcat:8080(http) 2017-11-21 15:46:24.922 INFO 6272 --- [main] o.apache.catalina.core.StandardService:启动服务[Tomcat] 2017-11-21 15:46:24.924 INFO 6272 --- [main] org.apache.catalina.core.StandardEngine:启动Servlet引擎:Apache Tomcat / 8.5.23 2017-11-21 15:46:25.250 INFO 6272 --- [ost-startStop-1] oacatalina.core.AprLifecycleListener:在Java上找不到基于APR的Apache Tomcat Native库,它可以在生产环境中实现最佳性能。 library.path:[C:\ Program Files \ Java \ jdk1.8.0_141 \ bin; C:\ Windows \ Sun \ Java \ bin; C:\ Windows \ system32; C:\ Windows; C:\ ProgramData \ Oracle \ Java \ javapath; C:\ Windows \ system32; C:\ Windows; C:\ Windows \ System32 \ Wbem; C:\ Windows \ System32 \ WindowsPowerShell \ v1.0 \; C:\ Program Files \ Microsoft SQL Server \ Client SDK \ ODBC \ 110 \ Tools \ Binn \; C:\ Program Files(x86)\ Microsoft SQL Server \ 120 \ Tools \ Binn \; C:\ Program Files \ Microsoft SQL Server \ 120 \ Tools \ Binn \; C: \ Program Files \ Microsoft SQL Server \ 120 \ DTS \ Binn \; C:\ Program Files(x86)\ Microsoft SQL Server \ 120 \ DTS \ Binn \; C:\ Program Files(x86)\ Microsoft SQL Server \ 120 \ Tools \ Binn \ ManagementStudio \; C:\ Windows \ system32 \ config \ systemprofile.dnx \ bin; C:\ Program Files \ Microsoft DNX \ Dnvm \; C:\ Program Files \ Microsoft SQL Server \ 130 \ Tools \ Binn \ ; C:\ Program File s(x86)\ Windows Kits \ 8.1 \ Windows性能工具包\; C:\ Program Files \ Microsoft SQL Server \ 110 \ Tools \ Binn \; C:\ Program Files(x86)\ Microsoft SDKs \ TypeScript \ 1.0 \; C :\ Program Files(x86)\ Microsoft Visual Studio 12.0 \ Common7 \ IDE \ PrivateAssemblies \; C:\ Program Files \ nodejs \; C:\ Program Files \ Java \ jdk1.8.0_141 \ bin; C:\ Users \ kaleti \应用程序数据\本地\微软\ WindowsApps; C:\用户\ kaleti \应用程序数据\漫游\ NPM; C:\用户\ kaleti \应用程序数据\本地\ GitHubDesktop \ BIN ;.] 2017-11-21 15:46:25.628 INFO 6272 --- [ost-startStop-1] o.a.c.c.C. [Tomcat]。[localhost]。[/]:初始化Spring嵌入式WebApplicationContext 2017-11-21 15:46:25.629 INFO 6272 --- [ost-startStop-1] o.s.web.context.ContextLoader:Root WebApplicationContext:初始化完成时间为23087 ms 2017-11-21 15:46:25.986 INFO 6272 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean:映射servlet:' dispatcherServlet'至 [/] 2017-11-21 15:46:25.986 INFO 6272 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean:映射过滤器:' characterEncodingFilter'至:[/ ] 2017-11-21 15:46:25.986 INFO 6272 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean:映射过滤器:' hiddenHttpMethodFilter'致:[/ ] 2017-11-21 15:46:25.986 INFO 6272 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean:映射过滤器:' httpPutFormContentFilter'至:[/ ] 2017-11-21 15:46:25.986 INFO 6272 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean:映射过滤器:' requestContextFilter'致:[/ ] 2017-11-21 15:46:26.625 WARN 6272 --- [main] ConfigServletWebServerApplicationContext:在上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为' statusController&的bean时出错#39;:通过字段' statusService'表达的不满意的依赖性;嵌套异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为' statusService的bean时出错:通过字段' repository'表达的不满意的依赖关系;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名称为' repositoryImpl'的错误:无法创建内部bean'(内部bean)#340b7ef6'类型为[org.springframework.orm.jpa.SharedEntityManagerCreator]时设置bean属性' entityManager&#39 ;;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'(内部bean)的bean时出错#340b7ef6':无法解析对bean的引用' entityManagerFactory'设置构造函数参数时;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有bean命名为' entityManagerFactory'可得到 2017-11-21 15:46:26.656 INFO 6272 --- [main] o.apache.catalina.core.StandardService:停止服务[Tomcat] 2017-11-21 15:46:26.788 INFO 6272 --- [main] ConditionEvaluationReportLoggingListener:

启动ApplicationContext时出错。要显示条件报告,请使用' debug'重新运行您的应用程序。启用。 2017-11-21 15:46:26.983 ERROR 6272 --- [主要] o.s.b.d.LoggingFailureAnalysisReporter
申请未能启动***************************

描述: com.learing.demo.StatusService中的字段存储库需要一个名为' entityManagerFactory'的bean。无法找到。 动作:

考虑定义名为' entityManagerFactory'的bean。在你的配置中。

使用退出代码1完成处理

我的代码如下

package com.learing.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

    @SpringBootApplication
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }

StatusController类

package com.learing.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class StatusController {

@Autowired
private StatusService statusService;

@RequestMapping("/getEmpStatus")
public List<Employee> GetAllEmployeeStatus(){
    return statusService.GetAllEmpStatus();
}

@RequestMapping("/getEmpStatus/{id}")
public Employee GetAllEmployeeStatus(@PathVariable int id){
    return statusService.GetEmpStatus(id);
}

@RequestMapping(method= RequestMethod.POST, value="/getEmpStatus")
 public void  AddEmployeeStatus(@RequestBody Employee employee){
    statusService.AddEmployeeStatus(employee);

}
@RequestMapping(method = RequestMethod.PUT, value="/getEmpStatus/{id}")
public void  UpdateStatus(@RequestBody Employee employee, @PathVariable int id){
     statusService.UpdateStatus(employee,id);
}
@RequestMapping(method= RequestMethod.DELETE, value="/getEmpStatus/{id}")
public void  UpdateStatus(@PathVariable int id){
    statusService.DeleteStatus(id);
}

}

StatusService类

    package com.learing.demo;

    import org.springframework.beans.factory.annotation.Autowired;

    import org.springframework.context.annotation.Bean;
    import org.springframework.stereotype.Service;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;

    @Service
    public class StatusService {

        @Autowired
        public RepositoryImpl repository;

        private List<Employee> employees = new ArrayList<>(Arrays.asList(new Employee(1, "NSIN253", "Kalpa", "BHS-Mapping", "Mapping doc Completed"), new Employee(2, "NSIN254", "Fareeda", "BHS-Mapping", "Mapping doc Completed"), new Employee(3, "NSIN252", "KrishnaVeni", "BHS-Mapping", "Mapping doc Completed")));


        public List<Employee> GetAllEmpStatus() {
            return employees;
        }

        public Employee GetEmpStatus(int id) {
            return employees.stream().filter(employee -> employee.getiD() == id).findFirst().get();
        }

        public void AddEmployeeStatus(Employee employee) {
            repository.save(employee);
        }

        public void UpdateStatus(Employee employee, int id) {
            for (int i = 0; i < employees.size(); i++) {
                Employee emp = employees.get(i);
                if (emp.getiD() == id) {
                    employees.set(i, employee);
                }
            }
        }

        public void DeleteStatus(int id) {
            employees.removeIf(emp -> emp.getiD() == id);
        }
    }

RepositoryImpl类

    package com.learing.demo;

    import org.springframework.data.repository.CrudRepository;
    import org.springframework.stereotype.Repository;

    @Repository
    public interface RepositoryImpl extends CrudRepository<Employee,Integer> {

    }

申请道具:

spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=EmployeeDb
    spring.datasource.username=xxxx
    spring.datasource.password=xxxxx
    spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
    spring.jpa.show-sql=true
    spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
    spring.jpa.hibernate.ddl-auto =update

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd">     4.0.0

<groupId>com.learing</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.BUILD-SNAPSHOT</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <scope>runtime</scope>

    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.1.4.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.2.3.Final</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
    <pluginRepository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

3 个答案:

答案 0 :(得分:0)

您需要从POM中删除这些内容:

 @Override
      public int getItemCount() {
         return planList.size;
      }

答案 1 :(得分:0)

Spring Data使用名为entityManagerFactory的默认bean查找EntityManagerFactory。

尝试在配置中使用以下注释,例如:

@EnableJpaRepositories(basePackages =“com.learing.demo”,entityManagerFactoryRef =“entityManagerFactory”) class Config {}

答案 2 :(得分:0)

我在您的application.properties配置中发现了这个问题。您使用了错误的属性

变化。

spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect

spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect