b.lopes b.lopes - 3 years ago 832
Java Question

Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found

With the version of spring-boot 2.0.0.M4 I have this issue:


Field userRepository in
webroot.websrv.auth.service.JwtUserDetailsServiceImpl required a bean
named 'entityManagerFactory' that could not be found.


Consider defining a bean named 'entityManagerFactory' in your

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at ...
Caused by: org.springframework.context.ApplicationContextException:
Unable to start web server; nested exception is
org.springframework.boot.web.server.WebServerException: Unable to start
embedded Tomcat

Caused by:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error
creating bean with name 'jwtUserDetailsServiceImpl': Unsatisfied
dependency expressed through method 'setUserRepository' parameter 0;
nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'userRepository': Cannot create inner bean '(inner
bean)#770f146b' of type
[org.springframework.orm.jpa.SharedEntityManagerCreator] while setting
bean property 'entityManager'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name '(inner bean)#770f146b': Cannot resolve reference to
bean 'entityManagerFactory' while setting constructor argument; nested
exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
bean named 'entityManagerFactory' available

Spring boot application start:

package webroot.websrv;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class WebcliApplication {

public static void main(String[] args) {
SpringApplication.run(WebcliApplication.class, args);

The implemented Jwt service JwtUserDetailsServiceImpl:


public class JwtUserDetailsServiceImpl implements
UserDetailsService {

private UserRepository userRepository;

* Injects UserRepository instance
* @param userRepository to inject
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;

Here the UserRepository:

package webroot.websrv.auth.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import webroot.websrv.auth.entity.User;

public interface UserRepository extends JpaRepository<User, Long> {

* Finds user by email
* @param email to look for
* @return user by given email
User findByEmail(String email);

* Finds user by name
* @param name to look for
* @return user by given name
User findByName(String name);

I have saw several time this issue reported here but most of the cases I have it OK for the given solutions.

Do I have to explicit define a bean to the entity manager ? It should be automatically injected no ?

I added the class for the UserRepository it extends a JpaRepository.



Recommended for you: Get network issues from WhatsUp Gold. Not end users.
Answer Source

I found the solution, it was necessary to implement a JpaConfiguration:

@EnableJpaRepositories(basePackages = "webroot.webserv",
    entityManagerFactoryRef = "entityManagerFactory",
    transactionManagerRef = "transactionManager")
public class JpaConfiguration {

private Environment environment;

private int maxPoolSize;

 * Populate SpringBoot DataSourceProperties object directly from 
 * based on prefix.Thanks to .yml, Hierachical data is mapped out of 
the box with matching-name
 * properties of DataSourceProperties object].
@ConfigurationProperties(prefix = "spring.datasource")
public DataSourceProperties dataSourceProperties(){
    return new DataSourceProperties();

 * Configure HikariCP pooled DataSource.
public DataSource dataSource() {
    DataSourceProperties dataSourceProperties = dataSourceProperties();
        HikariDataSource dataSource = (HikariDataSource) 

        return dataSource;

 * Entity Manager Factory setup.
public LocalContainerEntityManagerFactoryBean entityManagerFactory() 
throws NamingException {
    LocalContainerEntityManagerFactoryBean factoryBean = new 
    factoryBean.setPackagesToScan(new String[] { "webroot.websrv" });
    return factoryBean;

 * Provider specific adapter.
public JpaVendorAdapter jpaVendorAdapter() {
    HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new 
    return hibernateJpaVendorAdapter;

 * Here you can specify any provider specific properties.
private Properties jpaProperties() {
    Properties properties = new Properties();
    return properties;

public PlatformTransactionManager 
transactionManager(EntityManagerFactory emf) {
    JpaTransactionManager txManager = new JpaTransactionManager();
    return txManager;

Thanks for the suggestions.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download