yService中的字段xDao需要一个类型为' javax.persistence.EntityManagerFactory'的bean。无法找到

时间:2018-01-28 05:34:52

标签: java spring jpa spring-boot spring-rest

回答herehere

的问题与此非常相似

他们建议在注释scanBasePackages中使用@SpringBootApplication,它可以找到自动连线所需的所有bean。

问题说明

但是,在其中一项服务CategoryServiceImpl中,有一个autowired dao categoryDao,其中包含@PersistenceContext实体管理员。

我的春季启动应用程序无法找到EntityManagerFactory  这是我的申请破产的重点。

这是一个非常直接的应用程序(我正在努力学习)。 我有两个简单的项目。

第一个是其余控制器,第二个是持久化实体的JPA项目。

第一个项目:REST控制器项目设置

Application.java(切入点)

 @SpringBootApplication(scanBasePackages = { 
              "com.example.resourcecontroller",
              "com.example.service",
              "com.example.dao",
              "javax" })
        public class Application {
    }

的pom.xml

<groupId>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>

第二个项目:JPA SPRING HIBERNATE

的pom.xml

   <groupId>com.example</groupId>
    <artifactId>spring-hibernate-jpa-example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
<dependency>
    <groupId>com.example</groupId>
    <artifactId>spring-hibernate-jpa-example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <scope>compile</scope>
</dependency>

服务类

 @Configuration
    @EnableTransactionManagement
    @ComponentScans(value = { @ComponentScan("com.example.dao"),
          @ComponentScan("com.example.service") })

    @Service
    public class CategoryServiceImpl implements CategoryService {

        @Autowired
        private CategoryDao categoryDao;
    }

DAO

  @Repository
    public class CategoryDaoImpl implements CategoryDao {

        @PersistenceContext
        private EntityManager em;
}

**申请配置**

@Configuration
@EnableTransactionManagement
@ComponentScans(value = { @ComponentScan("com.example.dao"),
      @ComponentScan("com.example.service") })
public class AppConfig {
   @Bean
   public LocalEntityManagerFactoryBean geEntityManagerFactoryBean() {
      LocalEntityManagerFactoryBean factoryBean = new LocalEntityManagerFactoryBean();
      factoryBean.setPersistenceUnitName("LOCAL_PERSISTENCE");
      return factoryBean;
   }

}

0 个答案:

没有答案
相关问题