NoSuchBeanDefinitionException:没有定义名为"entityManagerFactory"的bean

Son*_*Son 4 java spring-mvc spring-data-jpa

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

创建名为'indexController'的bean时出错:注入自动连接的依赖项失败; 嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:org.demo.webtemplate.db.repository.CustSysRepository org.demo.webtemplate.controllers.IndexController.repository; 嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'custSysRepository'的bean时出错:设置bean时无法创建[org.springframework.orm.jpa.SharedEntityManagerCreator]类型的内部bean'(内部bean)#f4da8a0' property'entalManager'; 嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'(内部bean)#f4da8a0'的bean时出错:在设置构造函数参数时无法解析对bean'entalManagerFactory'的引用; 嵌套异常是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);
    }

}
Run Code Online (Sandbox Code Playgroud)

配置:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

控制器:

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();
    }

}
Run Code Online (Sandbox Code Playgroud)

库:

package org.demo.webtemplate.db.repository;

...

public interface CustSysRepository extends JpaRepository<CustSys, Long> {

    List<CustSys> findByFullName(String fullName);
}
Run Code Online (Sandbox Code Playgroud)

实体:

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;
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

application.properties:

jdbc.driverClassName=org.hsqldb.jdbc.JDBCDriver
jdbc.url=jdbc:hsqldb:mem:testdb
jdbc.user=sa
jdbc.pass=
Run Code Online (Sandbox Code Playgroud)

小智 12

您在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;
}
Run Code Online (Sandbox Code Playgroud)

  • 它位于application.properties中. (4认同)