3

スプリング ブート アプリが起動し、Tomcat が実行され、最終的に終了する前にエラーがスローされます。

エラー

アプリを実行すると、次のAutowiredエラーが表示されます。

2016-07-29 02:18:24.737 ERROR 44715 --- [  restartedMain] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSecurityConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private myapp.MyUserDetailsService myapp.WebSecurityConfig.userDetailsService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [myapp.MyUserDetailsService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]

詳細

私のセキュリティ構成:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
        http
        .authorizeRequests()
        .antMatchers("/", "/register").permitAll()
        .anyRequest().authenticated()
        .and()
        .formLogin()
        .loginPage("/")
        .permitAll()
        .and()
        .logout()
        .permitAll();
}

@Autowired
private MyUserDetailsService userDetailsService;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
}

}

MyUserDetails:

@Service
@Transactional
public class MyUserDetailsService implements UserDetailsService {

@Autowired
private UserRepository userRepository;


public MyUserDetailsService() {
        super();
}

@Override
public UserDetails loadUserByUsername(String userName)
throws UsernameNotFoundException {
        User user = userRepository.findByUserName(userName);
        if (user == null) {
                return null;
        }
        List<GrantedAuthority> auth = AuthorityUtils
                                      .commaSeparatedStringToAuthorityList("ROLE_USER");
        String password = user.getPassword();
        return new org.springframework.security.core.userdetails.User(userName, password,
                                                                      auth);
}



}

Git Repoで完全なソースコードを見つけることができます

4

4 に答える 4

6

commit でプロジェクトを試しました3b9a6a2eが、実際にはエラーが異なります。

org.springframework.beans.factory.BeanCreationException: 'webSecurityConfig' という名前の Bean の作成中にエラーが発生しました: 自動配線された依存関係の注入に失敗しました。ネストされた例外は org.springframework.beans.factory.BeanCreationException: Could not autowire フィールドです: private myapp.MyUserDetailsS​​ervice myapp.WebSecurityConfig.userDetailsS​​ervice; ネストされた例外は java.lang.IllegalArgumentException です: myapp.MyUserDetailsS​​ervice フィールド myapp.WebSecurityConfig.userDetailsS​​ervice を com.sun.proxy.$Proxy80 に設定できません

あなたの質問はSpring can't create beansの複製になります。解決策:交換

@Autowired
private MyUserDetailsService userDetailsService;

@Autowired
private UserDetailsService userDetailsService;

Springがプロキシにラップする理由がないため、ソリューション(を削除@Transactional)は機能します。@TransactionalMyUserDetailsService

于 2016-07-29T09:38:58.160 に答える
2

作業@Transactionalからの削除。MyUserDetailsService理由はわかりません。誰かに説明がある場合は、投稿してください。回答としてマークします。

再確認しました。これは本当に正しいです。自分で確認したい場合は、840f0753bdca1592d9070c74bc87f30e8e2f87a7上記のリポジトリの commit を確認して、テストしてください。

間違っていると思うので、この回答に反対票を投じるのをやめてください。

于 2016-07-28T21:58:33.470 に答える
1

please add Annotation: @ComponentScan("") on main configuration class.

If you want to create bean/service for specific directory then you can give package path inside @ComponentScan("package.to.scan")

于 2016-07-29T05:48:43.153 に答える
1

@ComponentScanWebSecurityConfigに追加します。

で注釈が付けられているため、コンポーネントのスキャンにより、これは MyUserDetailsS​​ervice に接続する必要があり@Serviceます。

パッケージ名をパラメータとしてアノテーションに渡して、コンポーネント スキャンの範囲を設定できます。次に例を示します。

@ComponentScan("com.company.team")
于 2016-07-28T21:36:19.557 に答える