16624

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: Description:

Field userRepository in webroot.websrv.auth.service.JwtUserDetailsServiceImpl required a bean named 'entityManagerFactory' that could not be found. Action: Consider defining a bean named 'entityManagerFactory' in your configuration. [WARNING] java.lang.reflect.InvocationTargetException 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

</pre>

Spring boot application start:

package webroot.websrv; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class WebcliApplication { public static void main(String[] args) { SpringApplication.run(WebcliApplication.class, args); } }

The implemented Jwt service JwtUserDetailsServiceImpl:

@Service public class JwtUserDetailsServiceImpl implements UserDetailsService {

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

</pre>

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; @Repository 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.

Thanks

Bruno

Answer1:

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

@Configuration @EnableJpaRepositories(basePackages = "webroot.webserv", entityManagerFactoryRef = "entityManagerFactory", transactionManagerRef = "transactionManager") @EnableTransactionManagement public class JpaConfiguration { @Autowired private Environment environment; @Value("${datasource.sampleapp.maxPoolSize:10}") private int maxPoolSize; /* * Populate SpringBoot DataSourceProperties object directly from application.yml * based on prefix.Thanks to .yml, Hierachical data is mapped out of the box with matching-name * properties of DataSourceProperties object]. */ @Bean @Primary @ConfigurationProperties(prefix = "spring.datasource") public DataSourceProperties dataSourceProperties(){ return new DataSourceProperties(); } /* * Configure HikariCP pooled DataSource. */ @Bean public DataSource dataSource() { DataSourceProperties dataSourceProperties = dataSourceProperties(); HikariDataSource dataSource = (HikariDataSource) org.springframework.boot.jdbc.DataSourceBuilder .create(dataSourceProperties.getClassLoader()) .driverClassName(dataSourceProperties.getDriverClassName()) .url(dataSourceProperties.getUrl()) .username(dataSourceProperties.getUsername()) .password(dataSourceProperties.getPassword()) .type(HikariDataSource.class) .build(); dataSource.setMaximumPoolSize(maxPoolSize); return dataSource; } /* * Entity Manager Factory setup. */ @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException { LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); factoryBean.setDataSource(dataSource()); factoryBean.setPackagesToScan(new String[] { "webroot.websrv" }); factoryBean.setJpaVendorAdapter(jpaVendorAdapter()); factoryBean.setJpaProperties(jpaProperties()); return factoryBean; } /* * Provider specific adapter. */ @Bean public JpaVendorAdapter jpaVendorAdapter() { HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter(); return hibernateJpaVendorAdapter; } /* * Here you can specify any provider specific properties. */ private Properties jpaProperties() { Properties properties = new Properties(); properties.put("hibernate.dialect", environment.getRequiredProperty ("spring.jpa.properties.hibernate.dialect") ); ... return properties; } @Bean @Autowired public PlatformTransactionManager transactionManager(EntityManagerFactory emf) { JpaTransactionManager txManager = new JpaTransactionManager(); txManager.setEntityManagerFactory(emf); return txManager; } } Thanks for the suggestions.

Recommend

  • How to allow default construction when using universal reference in constructor?
  • Help designing a order manager class
  • C++ Object Initialization (Stack)
  • Net core custom user property
  • Error “An array may not have elements of this type”
  • Is there a way to focus on a dynamically created form on Angular 4/Ionic3?
  • Angular/Ionic 2 - what is a provider and what does `static get parameters()` do?
  • In Akka Java actor model, can a router create actors with non-default constructor?
  • How to use Animated.diffClamp in react native
  • Android Arabic Cursor Split
  • Generic Types Return in Typescript
  • How to unit test a Angular 4 component which uses router.paramMap
  • SQLiteOpenHelper: onCreate() method not called on physical device
  • how to avoid the dependencies hell with unit test in angular 2+
  • Visual Studio not stopping on an exception being thrown
  • javascript array numerical key resulting in excess “undefined”
  • SearchView WITHOUT ACTIONBAR [duplicate]
  • Can you pass data to a test fixture just like you pass data to test cases?
  • Java inject implementation using TypeLiteral
  • Calling a constructor through reflection in scala 2.10
  • Get UILabel out of UIButton
  • c++ search a vector for element first seen position
  • Opaque reference instead of PImpl. Is it possible?
  • Why people use prototype in javascript when it is easy to inherit using apply () and call () methods
  • c++ regex_replace not doing intended substitution
  • unable to get jsonEncode in magento2
  • using System.Speech.Synthesis with Windows10 universal app (XAML-C#)
  • GAE: Way to get reference to an HttpSession from its ID?
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • What is the purpose of TaskExecutor in spring?
  • How can I set a binding to a Combox in a UserControl?
  • Create DicomImage from scratch using Dcmtk
  • How do I access an unhandled exception in an MVC Error view?
  • iOS: Detect app start via notification press
  • Spring Data JPA custom method causing PropertyReferenceException
  • C# - Serializing and deserializing static member
  • Sending data from AppleScript to FileMaker records
  • How to include full .NET prerequisite for Wix Burn installer
  • Angular 2 constructor injection vs direct access
  • Is it possible to post an object from jquery to bottle.py?