NoSuchBeanDefinitionException:没有定义名为“entityManagerFactory”的bean

时间:2015-02-16 15:09:22

标签: java spring-mvc spring-data-jpa

我有简单的Spring 4 WebMVC应用程序(Java-config),我想添加JPA。 但是当我尝试运行app(在Tomcat上进行部署)时,我得到: 什么可能是错误的来源?

  

创建名为'indexController'的bean时出错:注入   自动连接依赖失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:不能   autowire字段:org.demo.webtemplate.db.repository.CustSysRepository   org.demo.webtemplate.controllers.IndexController.repository;嵌套   异常是org.springframework.beans.factory.BeanCreationException:   创建名为'custSysRepository'的bean时出错:无法创建内部   bean'(内部bean)#f4da8a0'的类型   [org.springframework.orm.jpa.SharedEntityManagerCreator]设置时   bean属性'entityManager';嵌套异常是   org.springframework.beans.factory.BeanCreationException:错误   创建名为'(内部bean)#f4da8a0'的bean:无法解析   设置构造函数时引用bean“entityManagerFactory”   参数;嵌套异常是   org.springframework.beans.factory。 NoSuchBeanDefinitionException:没有   名为'entityManagerFactory'的bean已定义

初始化器:

package org.demo.webtemplate;

...

public class SpringWebMvcInitializer implements WebApplicationInitializer  {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(Config.class);
        ctx.setServletContext(servletContext);
        Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);
    }

}

配置:

package org.demo.webtemplate;

...

@Configuration
@EnableWebMvc
@ComponentScan("pl.bzwbk.webtemplate")
@EnableJpaRepositories("pl.bzwbk.webtemplate.db.repository")
public class Config {

    @Bean
    public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }
}

控制器:

package org.demo.webtemplate.controllers;

...

@Controller
public class IndexController {

    @Autowired
    CustSysRepository repository;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        List<CustSys> clients = repository.findByFullName("SOME NAME");

        return "index"+clients .size();
    }

}

存储库:

package org.demo.webtemplate.db.repository;

...

public interface CustSysRepository extends JpaRepository<CustSys, Long> {

    List<CustSys> findByFullName(String fullName);
}

实体:

package org.demo.webtemplate.db.entity;

...

@Entity
@Table(name = "CUST_SYS")
public class CustSys implements Serializable {
    private static final long serialVersionUID = 1L;
    ...
    @Size(max = 255)
    @Column(name = "FULL_NAME")
    private String fullName;
    ...
    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
    ...
}

application.properties:

jdbc.driverClassName=org.hsqldb.jdbc.JDBCDriver
jdbc.url=jdbc:hsqldb:mem:testdb
jdbc.user=sa
jdbc.pass=

2 个答案:

答案 0 :(得分:11)

您在Config课程中缺少完整的数据库配置。

试试这个例子:

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("org.hsqldb.jdbc.JDBCDriver");
    dataSource.setUrl("jdbc:hsqldb:mem:testdb");
    dataSource.setUsername("sa");
    dataSource.setPassword("");
    return dataSource;
}

@Bean
public EntityManager entityManager() {
    return entityManagerFactory().getObject().createEntityManager();
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());
    em.setPackagesToScan("package.where.your.entites.like.CustSys.are.stored");
    return em;
}

答案 1 :(得分:0)

this question中我发布了一个完整的例子,说明如何对需要自动装配的JpaRepository的弹簧控制器进行单元测试。