2016-07-28 15 views
1

springブートアプリケーションが起動し、tomcatが実行され、最終的に死ぬ前にエラーがスローされます。SpringブートのカスタムUserDetailsS​​erviceでAutowireが動作しない

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:

私のアプリを実行している

エラー

は私にこのAutowiredエラーが発生します

@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); 
} 



} 
あなたはWebSecurityConfigに @ComponentScanを追加 Git Repo

+0

私はGit Repo URL経由でコードベースを共有するというアイデアが気に入っています。もしSOが、質問を掲示している間に提出されたオプションのGit Repoを提供すれば、それは完全なソースを見て質問によく答えるのに役立つでしょう。 – JavaHopper

答えて

5

内のパッケージパスを与えることができ、私はあなたのプロジェクトがで3b9a6a2eをコミットしようとしたし、エラーが実際に異なっている:

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 java.lang.IllegalArgumentException: Can not set myapp.MyUserDetailsService field myapp.WebSecurityConfig.userDetailsService to com.sun.proxy.$Proxy80

あなたの質問はSpring can't create beansの複製になります。ソリューション:@Transactionalずに春がプロキシでMyUserDetailsServiceをラップするために理由はないので、あなたのソリューション

@Autowired 
private UserDetailsService userDetailsService; 

@Autowired 
private MyUserDetailsService userDetailsService; 

を交換するには、(@Transactionalを削除する)動作します。

+0

これを説明していただきありがとうございます!しかし、私はSpringを渡して、MyUserDetailsS​​erviceから自分の設定を探す方法を知っていますか? – amingilani

+0

「バネ知っている」とはどういう意味ですか? – Roman

+0

私は、「MyUserDetailsS​​ervice」から自分の設定を探す方法をSpringが知っていますか?申し訳ありませんが、私は急いで入力しているときに私は誤植の束を作る。 – amingilani

1

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

これは、@Serviceと注釈されているので、component scanningのためMyUserDetailsS​​erviceに配線されます。

パッケージ名は、例えば、成分走査の範囲を設定する注釈にパラメータとして渡すことができる。MyUserDetailsServiceから@Transactional取り外し

@ComponentScan("com.company.team") 
+2

これは本当に良い解決ではありません。あなたはBeanがスキャンされないという事実を隠しています。 –

+1

@Service Beansもスキャンされます。スキャンが使われていないという事実は本当の問題です。 –

+1

@ ComponentScanでbasePackageClassesが定義されていない場合、注釈を宣言するクラスのパッケージからスキャンが行われることに注意してください。 –

1

が働きました。理由は分かりません。誰かが説明をしている場合は、投稿してください。答えとしてマークします。

これは本当に正しいです。自分で確認したい場合は、上記のリポジトリに対してコミット840f0753bdca1592d9070c74bc87f30e8e2f87a7をチェックしてテストしてください。

あなたがと思うので、この回答をdownvotingしないでくださいそれは間違っています。

+1

それは本当に意味をなさない、あなたはいくつかの間に編集したチャンスは何ですか?たとえば、UserF40の変更にはあなたに指示があり、それらを試してみると、あなたのプロジェクトは再構築されなかったので、それはうまくいかなかったでしょうか? –

+0

@amingilani '@ Transactional'を読んで再構築しても、それでも動作しますか?もしそうなら、それはそれを解決しませんでした。 – UserF40

+0

@ Jean-Franç[email protected] UserF40がチェックされているだけで動作しません。 '@ Transactional'を削除すると動作します。 – amingilani

1

注釈を追加してください:@ComponentScan("") をメイン設定クラスに追加してください。

あなたが特定のディレクトリのための豆/サービスを作成したい場合、あなたは@ComponentScan("package.to.scan")

関連する問題