【发布时间】:2018-03-01 03:39:55
【问题描述】:
使用 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 boot 应用启动:
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;
}
这里是 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。
谢谢
布鲁诺
【问题讨论】:
-
你在项目中使用了哪个持久化提供者?
-
我为 UserRepository 添加了类,它扩展了 JpaRepository。
-
JPA 持久提供者可能是
eclipselink,hibernate,spring-data等。我认为这可能会有所帮助 docs.spring.io/spring-data/jpa/docs/1.6.0.RELEASE/reference/… -
是的,使用 spring-data:
org.springframework.data spring-data-jpa -
你的 UserRepository 看起来有点奇怪......这是我的:@Repository public interface UserRepository extends JpaRepository
{ }
标签: java spring-boot spring-security jwt