使用 spring-boot 2.0.0.M4 的版本我遇到了这个问题:
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
我找到了解决方案,需要实现 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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)