首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring boot应用启动失败

Spring boot应用启动失败
EN

Stack Overflow用户
提问于 2018-03-27 16:19:49
回答 4查看 788关注 0票数 1

我的Spring启动应用程序无法启动,控制台中显示以下消息。

代码语言:javascript
复制
/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.0.RELEASE)

2018-03-27 13:42:13.816  INFO 2516 --- [           main] com.bijit.BootTestApplication            : Starting BootTestApplication on INENBIRINBL2C with PID 2516 (started by birinb in C:\Users\birinb\workspace\Spring Projects1\BootTest)
2018-03-27 13:42:13.818  INFO 2516 --- [           main] com.bijit.BootTestApplication            : No active profile set, falling back to default profiles: default
2018-03-27 13:42:13.851  INFO 2516 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@184cf7cf: startup date [Tue Mar 27 13:42:13 IST 2018]; root of context hierarchy
2018-03-27 13:42:14.508  INFO 2516 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-03-27 13:42:14.524  INFO 2516 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-03-27 13:42:14.525  INFO 2516 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.28
2018-03-27 13:42:14.529  INFO 2516 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_131\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C:/Program Files/Java/jre1.8.0_131/bin/server;C:/Program Files/Java/jre1.8.0_131/bin;C:/Program Files/Java/jre1.8.0_131/lib/amd64;C:\Program Files\avs\bin;C:\Program Files (x86)\RSA SecurID Token Common;C:\ProgramData\Oracle\Java\javapath;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Common Files\EMC\;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files\nodejs\;C:\Program Files\Git\cmd;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Java\jdk1.8.0_121\bin;C:\Users\birinb\AppData\Roaming\npm;C:\Program Files\Microsoft VS Code\bin;C:\Users\birinb\AppData\Local\atom\bin;C:\Users\birinb\Software\eclipse;;.]
2018-03-27 13:42:14.617  INFO 2516 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-03-27 13:42:14.617  INFO 2516 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 768 ms
2018-03-27 13:42:14.656  INFO 2516 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-03-27 13:42:14.690  WARN 2516 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customerController': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customerServiceImpl': Unsatisfied dependency expressed through field 'dao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerDaoImpl': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManagerFactory' available
2018-03-27 13:42:14.692  INFO 2516 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-03-27 13:42:14.699  WARN 2516 --- [ost-startStop-1] o.a.c.loader.WebappClassLoaderBase       : The web application [ROOT] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.lang.Object.wait(Native Method)
 java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
 com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)
2018-03-27 13:42:14.705  INFO 2516 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-03-27 13:42:14.797 ERROR 2516 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Field dao in com.bijit.service.CustomerServiceImpl required a bean of type 'javax.persistence.EntityManagerFactory' that could not be found.


Action:

Consider defining a bean of type 'javax.persistence.EntityManagerFactory' in your configuration.

在DaoImpl类中找不到-Saying实体管理器bean,但我已经在那里定义了它。

代码语言:javascript
复制
package com.bijit.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.transaction.Transactional;

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

import com.bijit.entity.Customer;

@Repository
@Transactional 
public class CustomerDaoImpl implements CustomerDao {

    @PersistenceContext
     EntityManager em;

    public List<Customer> findAll(){
        TypedQuery<Customer> query  =em.createQuery("Select c from customer c", Customer.class);
        return query.getResultList();

    }

    public Customer findById(Long id){
        return em.find(Customer.class, id);
    }

    public void deleteCustomer(Customer customer){
        em.remove(customer);
        }

    public void deleteById(Long id){
        Customer customer = findById(id);
        em.remove(customer);
    }

    public void saveCustomer(Customer customer){

        if(customer.getId()==null){
            em.persist(customer);
        }
        else{
            em.merge(customer);
        }
    }


}

请帮帮我。我不明白问题出在哪里。我也试过用@Autowired来代替@PersistanceContext,但仍然不起作用。

EN

回答 4

Stack Overflow用户

发布于 2018-03-27 17:31:41

它说找不到类型为'javax.persistence.EntityManagerFactory‘的bean

在你的类中你使用了- javax.persistence.EntityManager;

你的构建干净吗?

更新

尝试添加:-

代码语言:javascript
复制
@Bean
public LocalEntityManagerFactoryBean entityManagerFactory() {
    LocalEntityManagerFactoryBean em =
    new LocalEntityManagerFactoryBean();
    return em;
}

您的应用程序是spring-boot应用程序吗?如果是,那么你应该跳过上面的bean。

您的项目中是否存在以下依赖项:

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

在你的application.properties中,你可能需要添加如下的db细节:-

代码语言:javascript
复制
spring.datasource.url=jdbc:h2:mem:amqp-demo;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.platform=h2
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

此处的大多数属性会因数据库而异

我使用的是H2 db,因此可以更具体地使用这些数据库。

票数 2
EN

Stack Overflow用户

发布于 2018-03-27 17:53:29

它说找不到javax.persistence.EntityManagerFactory。这个类来自JPA。您的项目中包含JPA提供程序了吗?

票数 0
EN

Stack Overflow用户

发布于 2018-03-27 19:07:33

代码语言:javascript
复制
<?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>com.bijit</groupId>
    <artifactId>RANDOM</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>BootTest</name>
    <description>boot test</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</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>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency> -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
        </dependency>

    </dependencies>

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


</project>

服务类别-

代码语言:javascript
复制
package com.bijit.service;

import java.util.List;

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

import com.bijit.dao.CustomerDao;
import com.bijit.entity.Customer;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    public CustomerDao dao;

    public List<Customer> getCustomers(){
        return dao.findAll();
    }

    public Customer getCustomer(Long id){
        return dao.findById(id);
    }

    public void deleteCustomer(Long id){
        dao.deleteById(id);
    }

    public void saveCustomer(Customer customer){
        dao.saveCustomer(customer);
    }
}

package com.bijit;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BootTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootTestApplication.class, args);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49507771

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档