NoSuchBeanDefinitionException:没有名为“entityManagerFactory”的bean可用
Posted
技术标签:
【中文标题】NoSuchBeanDefinitionException:没有名为“entityManagerFactory”的bean可用【英文标题】:NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available 【发布时间】:2018-11-07 17:55:09 【问题描述】:我在构建时遇到此错误。我在这里提到了其他答案,但没有一个对我有用。
application.properties
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/externalPoc
spring.datasource.username=postgres
spring.datasource.password=
spring.database.driverClassName=org.postgresql.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=false
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
存储库:
@Repository
public interface CardRepository extends JpaRepository<CardRegistration, Serializable>
Optional<Card> findByUuid(String uuid);
实体:
@Entity
public class Card extends AbstractBaseEntity implements Serializable
String uuid;
String validFrom;
String validTo;
public String getValidFrom()
return validFrom;
public void setValidFrom(String validFrom)
this.validFrom = validFrom;
public String getValidTo()
return validTo;
public void setValidTo(String validTo)
this.validTo = validTo;
public String getUuid()
return uuid;
public void setUuid(String uuid)
this.uuid = uuid;
和 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>com.start.new.project</groupId>
<artifactId>externalAdapterPoc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>externalPoc</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.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>
<springfox-swagger.version>2.7.0</springfox-swagger.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-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1206-jdbc42</version><!--$NO-MVN-MAN-VER$-->
</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>
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>$springfox-swagger.version</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>$springfox-swagger.version</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency>
<dependency>
<groupId>ma.glasnost.orika</groupId>
<artifactId>orika-core</artifactId>
<version>1.5.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>hamcrest-core</artifactId>
<groupId>org.hamcrest</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>0.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path-assert</artifactId>
<version>0.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20171018</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
和主类(spring boot 应用)
@SpringBootApplication
@EnableJpaRepositories(basePackages = "com.start.new.project.externalPoc.model.repository")
@EntityScan(basePackages = "com.start.new.project.externalPoc.model")
public class ExternalPocApplication
public static void main(String[] args)
SpringApplication.run(ExternalPocApplication.class, args);
@Bean
public MapperFacade mapper()
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
return mapperFactory.getMapperFacade();
我从 .m2 目录中删除了所有文件夹。再次运行 mvn clean install。得到同样的错误。 服务类自动装配存储库。 控制器使用 @RestController 注释。
这就是确切的原因:
引起:org.springframework.beans.factory.BeanCreationException: 创建名为“(内部 bean)#4d3ca6c7”的 bean 时出错:无法解析 在设置构造函数时引用 bean 'entityManagerFactory' 争论;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 名为“entityManagerFactory”的 bean 可用
【问题讨论】:
我发现类EntityManagerFactory
应该在 javax.persistance package 中,所以也许你只需要 this dependecy 到 pom 文件?只是猜测
没用 :( ..
这就是你的全部代码吗?似乎您尝试在使用 entityManagerFactory 的地方创建 bean,但之前忘记创建它
是的,这就是所有的代码。我没有添加控制器和服务。
【参考方案1】:
从https://start.spring.io(选择Switch to the full version and tick PostGres, for example
)创建一个虚拟项目,然后将您的类添加到该项目中。
这样您就不必担心依赖关系,并且默认情况下您会获得最新的。我昨天用过它,它对我来说就像一个魅力。
【讨论】:
【参考方案2】:在自动装配 bean 的服务类顶部添加 @ComponentScan(basePackageClasses = ("Name of your entityManagerFactory bean class").class)
。还要在entityManagerFactory
bean 类之上添加@Component
或其派生注解
【讨论】:
什么是 entityManagerFactory bean 类?是实体吗?还是存储库? bean 类意味着 entityManagerFactory 看起来像一个变量(例如 String 字符串;这里的 string 是 String 类的变量)。现在无论变量属于哪个类,您都可以将其名称或其实现之一(如果它是接口)代替括号如果您可以提供指向声明变量的实际代码的链接,我可能会提供帮助更好:)以上是关于NoSuchBeanDefinitionException:没有名为“entityManagerFactory”的bean可用的主要内容,如果未能解决你的问题,请参考以下文章