我的Spring启动应用程序无法在控制台中显示以下消息。
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: 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类中找不到实体管理器bean,但我在那里定义了它。
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仍然无法正常工作。
答案 0 :(得分:2)
它是说没有类型的bean&#39; javax.persistence.EntityManagerFactory&#39;无法找到
在你的班级中你正在使用--javax.persistence.EntityManager;
你的构建是否干净?
<强>更新强>
尝试添加: -
@Bean
public LocalEntityManagerFactoryBean entityManagerFactory() {
LocalEntityManagerFactoryBean em =
new LocalEntityManagerFactoryBean();
return em;
}
是你的应用程序spring-boot应用程序?如果是,那么你应该跳过上面的bean。
您的项目中是否具有以下依赖项: -
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
在您的application.properties中,您可能需要添加以下内容的db详细信息: -
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,因此事情更具针对性
答案 1 :(得分:0)
它说无法找到javax.persistence.EntityManagerFactory
。本课程来自JPA。您是否在项目中包含了JPA提供程序?
答案 2 :(得分:0)
<?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>
服务类 -
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);
}
}
答案 3 :(得分:0)
在Application类中添加以下bean
@Bean
public SessionFactory sessionFactory(@Qualifier("entityManagerFactory") EntityManagerFactory emf) {
return emf.unwrap(SessionFactory.class);
}
然后在你的Dao中自动装配sessionFactory,如下所示:
@Autowired
SessionFactory sessionFactory;
现在您可以使用此sessionFactory中的会话来访问您的数据库