< method>中方法setUserService的参数0设置为0.需要类型为< service>的bean.找不到 [英] Parameter 0 of method setUserService in <method> required a bean of type <service> that could not be found

查看:145
本文介绍了< method>中方法setUserService的参数0设置为0.需要类型为< service>的bean.找不到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经解决了这个问题,找不到正在发生的事情....尝试使用@ComponentScan,尝试命名我的服务,似乎没有任何作用.

I have been through this issue and couldn't find what is going on.... tried to @ComponentScan, tried to name my services, none seems to work.

03:35:05,193 WARN  [org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext] (ServerService Thread Pool -- 81) Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfiguration': Unsatisfied dependency expressed through method 'setAuthenticationProvider' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'daoAuthenticationProvider' defined in class path resource [com/ipayso/config/SecurityConfiguration.class]: Unsatisfied dependency expressed through method 'daoAuthenticationProvider' parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDetailsService': Unsatisfied dependency expressed through method 'setUserService' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.ipayso.services.UserService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
03:35:05,193 INFO  [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] (ServerService Thread Pool -- 81) Closing JPA EntityManagerFactory for persistence unit 'default'
03:35:05,194 INFO  [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 81) HHH000227: Running hbm2ddl schema export
03:35:05,196 INFO  [stdout] (ServerService Thread Pool -- 81) Hibernate: drop table if exists customer cascade
03:35:05,200 INFO  [stdout] (ServerService Thread Pool -- 81) Hibernate: drop table if exists private_key cascade
03:35:05,205 INFO  [stdout] (ServerService Thread Pool -- 81) Hibernate: drop table if exists public_key cascade
03:35:05,208 INFO  [stdout] (ServerService Thread Pool -- 81) Hibernate: drop table if exists user_login cascade
03:35:05,211 INFO  [stdout] (ServerService Thread Pool -- 81) Hibernate: drop sequence hibernate_sequence
03:35:05,213 INFO  [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 81) HHH000230: Schema export complete
03:35:05,223 WARN  [org.springframework.boot.SpringApplication] (ServerService Thread Pool -- 81) Error handling failed (Error creating bean with name 'delegatingApplicationListener' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available)
03:35:05,262 WARN  [org.jboss.modules] (ServerService Thread Pool -- 81) Failed to define class org.springframework.boot.autoconfigure.data.rest.SpringBootRepositoryRestConfigurer in Module "deployment.devipayso-1.0.war:main" from Service Module Loader: java.lang.NoClassDefFoundError: Failed to link org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestConfigurer (Module "deployment.devipayso-1.0.war:main" from Service Module Loader): org/springframework/data/rest/webmvc/config/RepositoryRestConfigurerAdapter

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method setUserService in com.ipayso.services.security.UserDetailsServiceImpl required a bean of type 'com.ipayso.services.UserService' that could not be found.


Action:

Consider defining a bean of type 'com.ipayso.services.UserService' in your configuration.

我的SecurityConfiguration:

package com.project.config
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

private AuthenticationProvider authenticationProvider;

@Autowired
@Qualifier("daoAuthenticationProvider")
public void setAuthenticationProvider(AuthenticationProvider authenticationProvider) {
    this.authenticationProvider = authenticationProvider;
}

@Bean
public PasswordEncoder passwordEncoder(StrongPasswordEncryptor passwordEncryptor){
    PasswordEncoder passwordEncoder = new PasswordEncoder();
    passwordEncoder.setPasswordEncryptor(passwordEncryptor);
    return passwordEncoder;
}

@Bean
public DaoAuthenticationProvider daoAuthenticationProvider(PasswordEncoder passwordEncoder, UserDetailsService userDetailsService){
    DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
    daoAuthenticationProvider.setPasswordEncoder(passwordEncoder);
    daoAuthenticationProvider.setUserDetailsService(userDetailsService);
    return daoAuthenticationProvider;
}

@Autowired
public void configureAuthManager(AuthenticationManagerBuilder authenticationManagerBuilder){
    authenticationManagerBuilder.authenticationProvider(authenticationProvider);
}

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
    httpSecurity
            .authorizeRequests().antMatchers("/", "/home", "/login", "/signup").permitAll()
        .and()
            .formLogin()
            .loginPage("/login")
            .failureUrl("/login-error")
        .and()
            .logout()
            .logoutSuccessUrl("/home");
}
}

我的UserDetailServiceImpl:

package com.project.service.security
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

private UserService userService;
private Converter<User, UserDetails> userUserDetailsConverter;

@Autowired
@Qualifier(value = "userService")
public void setUserService(UserService userService) {
    this.userService = userService;
}

@Autowired
@Qualifier(value = "userToUserDetails")
public void setUserUserDetailsConverter(Converter<User, UserDetails> userUserDetailsConverter) {
    this.userUserDetailsConverter = userUserDetailsConverter;
}

@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
    return userUserDetailsConverter.convert(userService.findByEmail(email));
}
}

我的UserServiceImpl:

package com.project.service
@Service
@Profile("springdatajpa")
public class UserServiceImpl implements UserService{

private UserRepository userRepository;

@Autowired
public void setUserRepository(UserRepository userRepository) {
    this.userRepository = userRepository;
 }

private EncryptionService encryptionService;

@Autowired
public void setEncryptionService(EncryptionService encryptionService) {
    this.encryptionService = encryptionService;
}


@Override
public List<?> listAll() {
    List<User> users = new ArrayList<>();
    userRepository.findAll().forEach(users::add); //fun with Java 8
    return users;
}

@Override
public User getById(Integer id) {
    return userRepository.findOne(id);
}

@Override
public User saveOrUpdate(User domainObject) {
    if(domainObject.getPassword() != null){
        domainObject.setEncryptedPassword(encryptionService.encryptString(domainObject.getPassword()));
    }
    return userRepository.save(domainObject);
}
@Override
@Transactional
public void delete(Integer id) {
    userRepository.delete(id);
}

@Override
public User findByEmail(String email) {
    return userRepository.findByEmail(email);
}
}

我的CommonBeanConfig:

package com.project.config
@Configuration
public class CommonBeanConfig {

@Bean
public StrongPasswordEncryptor strongEncryptor(){
    StrongPasswordEncryptor encryptor = new StrongPasswordEncryptor();
    return encryptor;
}
}

我的App:

package com.project
@SpringBootApplication
public class App extends SpringBootServletInitializer{

public static void main(String[] args) {
    SpringApplication.run(applicationClass, args);
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(applicationClass);
}

private static Class<App> applicationClass = App.class;
}

我的RepositoryConfiguration:

package com.project.config
@Configuration
@EnableAutoConfiguration
@EntityScan(basePackages = {"com.ipayso.model"})
@EnableJpaRepositories(basePackages = {"com.ipayso.repositories"})
@EnableTransactionManagement
public class RepositoryConfiguration {

}

推荐答案

您要在此处使用@Qualifier(value = "userService")查找UserService:

@Autowired
@Qualifier(value = "userService")
public void setUserService(UserService userService) {
    this.userService = userService;
}

但是您没有它,因为您的UserServiceImpl注释为@Service而未提供其ID.

but you don't have it, since your UserServiceImpl annotated as @Service without providing it's id.

要将id设置为您的UserServiceImpl,您需要使用@Service("userService")对其进行注释.但是,如果您只有一个UserService实现,则只需从设置器中删除@Qualifier(value = "userService"),因为它是多余的.

To set id to your your UserServiceImpl, you need to annotate it with @Service("userService"). But if you have a single UserService implementation, just delete @Qualifier(value = "userService") from setter, because it's redundant.

在我看来,这不是唯一一个必须删除@Qualifier的地方.

And it seems to me, it's not the only place, where you have to remove a @Qualifier.

@Qualifier批注来选择确切的bean.如果只有一个,则不需要使用它.

@Qualifier annotation is needed to select exact bean if you have a number of beans of the same type. If you have a single one, you don't need to use it.

这篇关于&lt; method&gt;中方法setUserService的参数0设置为0.需要类型为&lt; service&gt;的bean.找不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆