无法使用 Spring 社交将 UserDetailsService 配置到 WebSecurityConfigurerAdapter
Cannot Configure UserDetailsService into WebSecurityConfigurerAdapter with Spring Social
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private final Log log = LogFactory.getLog(WebSecurityConfig.class);
@Value("${spring.profiles.active}")
private String env;
@Value("${rememberMe.privateKey}")
private String rememberMeKey;
@Resource
private UserDetailsService userService;
@Bean
public RememberMeServices rememberMeServices() {
TokenBasedRememberMeServices rememberMeServices = new TokenBasedRememberMeServices(rememberMeKey, userService);
// See
rememberMeServices.setAlwaysRemember(true);
return rememberMeServices;
}
@Bean
public PasswordEncoder passwordEncoder() {
log.info("Creating password encoder bean");
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/",
"/signup",
"/public/**",
"/users/*",
"/auth/**").permitAll()
.anyRequest().authenticated();
http
.formLogin()
.loginPage("/signin")
.permitAll().and()
.rememberMe().key(rememberMeKey).rememberMeServices(rememberMeServices()).and()
.logout()
.deleteCookies("JSESSIONID")
.permitAll()
.and().apply(new SpringSocialConfigurer());
if (!env.equals("dev"))
http.requiresChannel().anyRequest().requiresSecure();
}
@Autowired
@Override
protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
authManagerBuilder.userDetailsService(userService).passwordEncoder(passwordEncoder());
}
}
作为 UserDetailsService 我有以下内容:
@Service
@Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
public class UserServiceImpl implements UserService, UserDetailsService, SocialUserDetailsService {
private final Logger log = Logger.getLogger(this.getClass());
private UserRepository userRepository;
private PasswordEncoder passwordEncoder;
@Autowired
public UserServiceImpl(UserRepository userRepository,
PasswordEncoder passwordEncoder) {
this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
}
@Override
public SocialUserDetails loadUserByUsername(String username)
throws UsernameNotFoundException {
User user = userRepository.findByEmail(username);
if (user == null)
throw new UsernameNotFoundException(username);
return new UserDetailsImpl(user);
}
@Override
public User findOne(long userId) {
User user = userRepository.findOne(userId);
return user;
}
@Override
@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public User signup(SignupForm signupForm) {
final User user = new User();
user.setEmail(signupForm.getEmail());
user.setName(signupForm.getName());
user.setPassword(passwordEncoder.encode(signupForm.getPassword()));
userRepository.save(user);
MyUtil.logInUser(user);
return user;
}
@Override
@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public void update(long userId, UserEditForm userEditForm) {
User user = userRepository.findOne(userId);
user.setName(userEditForm.getName());
userRepository.save(user);
}
@Override
public SocialUserDetails loadUserByUserId(String userId)
throws UsernameNotFoundException, DataAccessException {
return loadUserByUsername(userId);
}
}
我收到以下错误:
org.springframework.beans.factory.BeanCreationException:创建名称为 'webSecurityConfig' 的 bean 时出错:注入资源依赖项失败;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为 'userServiceImpl' 的 bean 在文件 [/home/hardddisk/applications/tomcat8/webapps/ROOT/WEB-INF/classes/com/kefet/controller/UserServiceImpl 中定义时出错。class]:通过具有索引 0 类型 [[= 的构造函数参数表示的不满足依赖关系28=]]: : 没有为依赖项找到 [com.kefet.controller.UserRepository] 类型的合格 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{};嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有找到类型为 [com.kefet.controller.UserRepository] 的符合条件的 bean 用于依赖项:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{}
在 org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:311)
在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
在 org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:835)
在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
在 org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
在 org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
在 org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
在 org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727)
在 org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5167)
在 org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
在 org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
在 org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
在 org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
在 org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:945)
在 org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1768)
在 java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
在 java.util.concurrent.FutureTask.run(FutureTask.java:266)
在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
在 java.lang.Thread.run(Thread.java:745)
由以下原因引起:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为 'userServiceImpl' 的 bean 在文件 [/home/hardddisk/applications/tomcat8/webapps/ROOT/WEB-INF/classes/com/kefet/controller/UserServiceImpl 中定义时出错。class]:通过具有类型 [[= 的索引 0 的构造函数参数表示的不满足的依赖关系28=]]: : 没有为依赖项找到 [com.kefet.controller.UserRepository] 类型的合格 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{};嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有找到类型为 [com.kefet.controller.UserRepository] 的符合条件的 bean 用于依赖项:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{}
在 org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
在 org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185)
在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143)
在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean 实例 (AbstractAutowireCapableBeanFactory.java:1046)
在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
在 org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
在 org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192)
在 org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
在 org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
在 org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:508)
在 org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:486)
在 org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:615)
在 org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169)
在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
在 org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:308)
... 还有 26 个
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 没有为依赖项找到 [com.kefet.controller.UserRepository] 类型的合格 bean: 预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{}
在 org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373)
在 org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119)
在 org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
在 org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
在 org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
... 还有 44 个
谢谢。
错误消息声称它无法找到 UserRepository class 以自动装配到您的构造函数中。您必须使用 @Component (或它的某些子注释,如 @Repository / @Service )注释此 class 以使其可用于自动装配。
//更新:
首先 - 如果您是从头开始这个项目,我建议您查看 spring boot - 它会为您简化很多配置。
您没有提到您正在尝试在此处使用 spring 数据 jpa。
您必须以某种方式让 spring 知道您的接口在哪里。这是由 @EnableJpaRepositories
完成的。添加
@EnableJpaRepositories(basePackages = "com.kefet.controller")
高于您的 DatabaseConfig
。然后它会抱怨它需要 entityManagerFactory。添加:
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws SQLException {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
DataSource dataSource = dataSource();
entityManagerFactoryBean.setDataSource(dataSource);
entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManagerFactoryBean.setJpaProperties(jpaProperties());
entityManagerFactoryBean.setPackagesToScan("com.kefet.controller");
return entityManagerFactoryBean;
}
@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator() {
return new HibernateExceptionTranslator();
}
protected Properties jpaProperties() {
Properties jpaProperties = new Properties();
jpaProperties.put(org.hibernate.cfg.Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
jpaProperties.put(org.hibernate.cfg.Environment.SHOW_SQL, false);
return jpaProperties;
}
相同 DatabaseConfig
class.
这将解决此 post 的初始问题。您将收到更多错误(No qualifying bean of type [org.springframework.social.facebook.api.Facebook])但您需要一一修复它们。
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private final Log log = LogFactory.getLog(WebSecurityConfig.class);
@Value("${spring.profiles.active}")
private String env;
@Value("${rememberMe.privateKey}")
private String rememberMeKey;
@Resource
private UserDetailsService userService;
@Bean
public RememberMeServices rememberMeServices() {
TokenBasedRememberMeServices rememberMeServices = new TokenBasedRememberMeServices(rememberMeKey, userService);
// See
rememberMeServices.setAlwaysRemember(true);
return rememberMeServices;
}
@Bean
public PasswordEncoder passwordEncoder() {
log.info("Creating password encoder bean");
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/",
"/signup",
"/public/**",
"/users/*",
"/auth/**").permitAll()
.anyRequest().authenticated();
http
.formLogin()
.loginPage("/signin")
.permitAll().and()
.rememberMe().key(rememberMeKey).rememberMeServices(rememberMeServices()).and()
.logout()
.deleteCookies("JSESSIONID")
.permitAll()
.and().apply(new SpringSocialConfigurer());
if (!env.equals("dev"))
http.requiresChannel().anyRequest().requiresSecure();
}
@Autowired
@Override
protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
authManagerBuilder.userDetailsService(userService).passwordEncoder(passwordEncoder());
}
}
作为 UserDetailsService 我有以下内容:
@Service
@Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
public class UserServiceImpl implements UserService, UserDetailsService, SocialUserDetailsService {
private final Logger log = Logger.getLogger(this.getClass());
private UserRepository userRepository;
private PasswordEncoder passwordEncoder;
@Autowired
public UserServiceImpl(UserRepository userRepository,
PasswordEncoder passwordEncoder) {
this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
}
@Override
public SocialUserDetails loadUserByUsername(String username)
throws UsernameNotFoundException {
User user = userRepository.findByEmail(username);
if (user == null)
throw new UsernameNotFoundException(username);
return new UserDetailsImpl(user);
}
@Override
public User findOne(long userId) {
User user = userRepository.findOne(userId);
return user;
}
@Override
@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public User signup(SignupForm signupForm) {
final User user = new User();
user.setEmail(signupForm.getEmail());
user.setName(signupForm.getName());
user.setPassword(passwordEncoder.encode(signupForm.getPassword()));
userRepository.save(user);
MyUtil.logInUser(user);
return user;
}
@Override
@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public void update(long userId, UserEditForm userEditForm) {
User user = userRepository.findOne(userId);
user.setName(userEditForm.getName());
userRepository.save(user);
}
@Override
public SocialUserDetails loadUserByUserId(String userId)
throws UsernameNotFoundException, DataAccessException {
return loadUserByUsername(userId);
}
}
我收到以下错误:
org.springframework.beans.factory.BeanCreationException:创建名称为 'webSecurityConfig' 的 bean 时出错:注入资源依赖项失败;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为 'userServiceImpl' 的 bean 在文件 [/home/hardddisk/applications/tomcat8/webapps/ROOT/WEB-INF/classes/com/kefet/controller/UserServiceImpl 中定义时出错。class]:通过具有索引 0 类型 [[= 的构造函数参数表示的不满足依赖关系28=]]: : 没有为依赖项找到 [com.kefet.controller.UserRepository] 类型的合格 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{};嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有找到类型为 [com.kefet.controller.UserRepository] 的符合条件的 bean 用于依赖项:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{} 在 org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:311) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 在 org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305) 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:835) 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) 在 org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446) 在 org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328) 在 org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) 在 org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727) 在 org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5167) 在 org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 在 org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725) 在 org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701) 在 org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717) 在 org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:945) 在 org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1768) 在 java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) 在 java.util.concurrent.FutureTask.run(FutureTask.java:266) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 在 java.lang.Thread.run(Thread.java:745) 由以下原因引起:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为 'userServiceImpl' 的 bean 在文件 [/home/hardddisk/applications/tomcat8/webapps/ROOT/WEB-INF/classes/com/kefet/controller/UserServiceImpl 中定义时出错。class]:通过具有类型 [[= 的索引 0 的构造函数参数表示的不满足的依赖关系28=]]: : 没有为依赖项找到 [com.kefet.controller.UserRepository] 类型的合格 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{};嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有找到类型为 [com.kefet.controller.UserRepository] 的符合条件的 bean 用于依赖项:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{} 在 org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) 在 org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean 实例 (AbstractAutowireCapableBeanFactory.java:1046) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 在 org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305) 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) 在 org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:508) 在 org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:486) 在 org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:615) 在 org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169) 在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) 在 org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:308) ... 还有 26 个 Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 没有为依赖项找到 [com.kefet.controller.UserRepository] 类型的合格 bean: 预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{} 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) 在 org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813) 在 org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ... 还有 44 个
谢谢。
错误消息声称它无法找到 UserRepository class 以自动装配到您的构造函数中。您必须使用 @Component (或它的某些子注释,如 @Repository / @Service )注释此 class 以使其可用于自动装配。
//更新:
首先 - 如果您是从头开始这个项目,我建议您查看 spring boot - 它会为您简化很多配置。
您没有提到您正在尝试在此处使用 spring 数据 jpa。
您必须以某种方式让 spring 知道您的接口在哪里。这是由 @EnableJpaRepositories
完成的。添加
@EnableJpaRepositories(basePackages = "com.kefet.controller")
高于您的 DatabaseConfig
。然后它会抱怨它需要 entityManagerFactory。添加:
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws SQLException {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
DataSource dataSource = dataSource();
entityManagerFactoryBean.setDataSource(dataSource);
entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManagerFactoryBean.setJpaProperties(jpaProperties());
entityManagerFactoryBean.setPackagesToScan("com.kefet.controller");
return entityManagerFactoryBean;
}
@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator() {
return new HibernateExceptionTranslator();
}
protected Properties jpaProperties() {
Properties jpaProperties = new Properties();
jpaProperties.put(org.hibernate.cfg.Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
jpaProperties.put(org.hibernate.cfg.Environment.SHOW_SQL, false);
return jpaProperties;
}
相同 DatabaseConfig
class.
这将解决此 post 的初始问题。您将收到更多错误(No qualifying bean of type [org.springframework.social.facebook.api.Facebook])但您需要一一修复它们。