使用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
Spring启动应用程序启动:
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);
}
}
已实现的Jwt服务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;
}
这里的用户库:
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);
}
我在这里看到过几次关于这个问题的报道,但大多数情况下,我都认为对于给定的解决方案是可以的。
我是否必须向实体管理器显式定义bean?应该自动注射吗?
我为UserRepository添加了类它扩展了JpaRepository。
谢谢
布鲁诺
我找到了解决方案,有必要实施JPA配置:
@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.
当我启动我的springboot应用程序时,我得到一个错误 对于这个问题,正如您在这里看到的,他们要求添加与我之前删除的相同的依赖项。 这里可能发生了什么问题? pom.xml MaterialRequest(实体)
我被这个问题缠住了。当我尝试运行我的Spring启动应用程序时,我会收到以下消息: 我正在使用Spring Data JPA,如果我理解正确的话,当我使用例如JpaRepository接口时,entityManager会自动实现到我的项目中。我是否遗漏了什么?即使我使用的是JpaRepository,我是否应该为entityManager定义一个bean?我尝试将其更改为CrudRepositor
我正在尝试部署我的spring应用程序。以下是pom的副本。xml文件。 以下是申请的副本。属性文件。 我得到的错误跟踪如下。 我被夹在中间。我几乎没试过什么东西。因为提供entityManager的hibernate JPA启动器是从pom中删除hibernate核心和hibernate实体管理器的。xml也是如此。但我也犯了同样的错误。除此之外,我还创建了自定义datasoruce,如下所示。
我正在开发一个Spring Boot应用程序,在启动服务器时遇到了这个错误。我不确定我是否错误地定义了任何注释或遗漏了任何依赖项。如有任何帮助,我们将不胜感激。 主类: LeagueService.java: LeagueRepository.java
我正在使用JPA开发一个Spring Boot应用程序,遇到了这个错误。我不确定我是否使用了正确的注释或缺少依赖项。任何帮助都将不胜感激。 这是错误消息 userrepository.java user.java
错误: 说明: Application.Properties 当我在@SpringBootApplication后面添加(ScanBasePackages={“nashtech.tiennguyenm3”})时,就会发生此错误。