2016-08-17 30 views
0

私は春のブート1.4.0を使用しており、コードをコンパイルするときに循環依存の例外が発生しています。私のプロジェクト構造は標準で、同じように構造化されたプロジェクトはSpring Boot 1.3.5を使ってうまく動作します。私は、循環依存関係例外を削除する方法を見つけることを望んでいます。春のブート1.4.0循環依存の例外

プロジェクトのGradleは

dependencies { 
    compile('org.springframework.boot:spring-boot-starter-security') 
    compile('org.springframework.boot:spring-boot-starter-web') 
    compile('org.springframework.boot:spring-boot-starter-data-mongodb') 
    compile('org.springframework.boot:spring-boot-starter-data-rest') 
    compile('org.springframework.boot:spring-boot-starter-actuator') 
    compile('org.springframework.hateoas:spring-hateoas') 
    testCompile('org.springframework.boot:spring-boot-starter-test') 
} 

ファイルマイプロジェクト構造は、下から上に、MongoDBは、リポジトリ、サービス、コントローラ、UserDetailsS​​erviceと春のセキュリティ設定を含む、標準的なMVCパターンに従っています。

次のよう

リポジトリ

@Repository 
public interface LrqaPersonRepository extends MongoRepository<LrqaPerson,  String> { 

    public LrqaPerson findByEmail(String email); 
    public List<LrqaPerson> findByCountry(String country); 

} 

サービス

@Autowired 
     public LrqaPersonService(LrqaPersonRepository lrqaPersonRepository, 
          PasswordEncoder encoder){ 

     this.lrqaPersonRepository = lrqaPersonRepository; 
     this.encoder = encoder; 
     } 


     public LrqaPerson findSingleLrqaPerson(String id){ 
       logger.info("LrqaPersonService method findSingleLrqaPerson() invoked: " + LocalDateTime.now()); 
       LrqaPerson lrqaPerson = this.lrqaPersonRepository.findOne(id); 
       return lrqaPerson; 
      } 

UserDetailsS​​ervice

@Service("personDetailsService") 
public class PersonDetailsService implements UserDetailsService { 

private static final Logger logger =  LoggerFactory.getLogger(PersonDetailsService.class); 

private List<GrantedAuthority> auth; 

@Autowired 
LrqaPersonService personService; 

@Override 
public UserDetails loadUserByUsername(String email) 
     throws UsernameNotFoundException { 

    logger.info("LrqaPersonDetailsService loadUserByUsername() invoked: " + LocalDateTime.now()); 
    LrqaPerson person = personService.findLrqaPersonByEmail(email); 

    if (person == null) { 
     throw new UsernameNotFoundException("Invalid Email Username"); 
    } 

    if (person.getAuthority().equals("user")) { 
     logger.info("LrqaPersonDetailsService authority privilege detected: " + person.getAuthority()); 
     auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"); 
    } 

    if (person.getAuthority().equals("admin")) { 
     logger.info("LrqaPersonDetailsService authority privilege detected: " + person.getAuthority()); 
     auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ADMIN, ROLE_USER"); 
    } 

    return new org.springframework.security.core.userdetails.User(
      email, person.getPassword(), auth); 

} 
} 

コントローラ

@RestController 
public class PeopleController { 


private final LrqaPersonService personService; 
private static final Logger logger = LoggerFactory.getLogger(PeopleController.class); 

@Autowired 
public PeopleController(LrqaPersonService personService){ 
    this.personService = personService; 
} 

@RequestMapping(value = "/allpeople", method = RequestMethod.GET) 
public List<LrqaPerson> getAllLrqaPeople(){ 
    logger.info("LrqaGenericController method getAllLrqaPeople() invoked: " + LocalDateTime.now()); 
    List<LrqaPerson> lrqaPeople = this.personService.findAllLrqaPersons(); 
    return lrqaPeople; 
} 

私は./gradlew bootRun --debugを実行しているに乗る例外は次のとおり

11:12:36.141 [QUIET] [system.out] *************************** 
11:12:36.141 [QUIET] [system.out] APPLICATION FAILED TO START 
11:12:36.141 [QUIET] [system.out] *************************** 
11:12:36.141 [QUIET] [system.out] 
11:12:36.141 [QUIET] [system.out] Description: 
11:12:36.142 [QUIET] [system.out] 
11:12:36.142 [QUIET] [system.out] There is a circular dependency between 4 beans in the application context: 
11:12:36.142 [QUIET] [system.out] - peopleController defined in file [/Users/frank***/Development/IntelliJ/springAngularJSTutorial/build/classes/main /org/frank/***/controller/PeopleController.class] 
11:12:36.142 [QUIET] [system.out] - lrqaPersonService defined in file [/Users/frank***/Development/IntelliJ/springAngularJSTutorial/build/classes/main/org/frank/***/service/LrqaPersonService.class] 
11:12:36.142 [QUIET] [system.out] - securityConfig 
11:12:36.142 [QUIET] [system.out] - personDetailsService (field org.frank.***.service.LrqaPersonService org.frank.***.security.PersonDetailsService.personService) 
11:12:36.142 [QUIET] [system.out] - lrqaPersonService 
11:12:36.142 [QUIET] [system.out] 
11:12:36.142 [QUIET] [system.out] 
11:12:36.180 [DEBUG] [org.gradle.process.internal.DefaultExecHandle] Changing state to: FAILED 

例外は私のセキュリティ設定は次のとおりです。

@Configuration 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

@Resource(name = "personDetailsService") 
private PersonDetailsService personDetailsService; 

@Autowired 
public void configureAuth(AuthenticationManagerBuilder auth) 
     throws Exception { 
    auth.inMemoryAuthentication() 
      .withUser("****").password("****").roles("USER"); 
} 

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http 
      .authorizeRequests() 
      .antMatchers("/user", "/index.html", "/home.html", "/login.html", "/").permitAll() 
      .anyRequest().authenticated().and() 
      .formLogin().and() 
      .httpBasic().and() 
      .csrf() 
      .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()); 
    ; 
} 
@Bean 
public PasswordEncoder passwordEncoder() { 
    PasswordEncoder encoder = new BCryptPasswordEncoder(); 
    return encoder; 
} 
@Override 
protected void configure(AuthenticationManagerBuilder builder) throws  Exception {    builder.userDetailsService(personDetailsService).passwordEncoder(passwordEncoder()); 
} 
} 

私は気づきましたさまざまなフォーラムでオンラインで問題の一部をカバーしています。循環依存関係の例外に対する解決策があり、Boot 1.3.5アプリケーション構造が例外を生成しない理由を説明できる箇所があれば、私にとって大きな助けになります。つまり、1.3.5から1.4.0の間で変更されたものです。これに関連して[この質問に適した簡単な説明がある場合]。

答えて

2

メッセージには、アプリケーションの4つのBeanの間に循環依存関係が存在することが記載されています。サイクルがあることをより詳細に:

  • PeopleControllerLrqaPersonService
  • LrqaPersonServiceに依存するSecurityConfig
  • SecurityConfigによって生成されるPasswordEncoderに依存PersonDetailsService
  • PersonDetailsServiceあなたはLrqaPersonService

に依存に依存それを破る必要がある何とかサイクル。

コードを見ると、になぜPasswordEncoderが注入されているのかは不明ですが、使用されていないようです。最も簡単な解決策は、LrqaPersonServiceからPasswordEncoderを削除することです。

あなたには、いくつかの理由のためにそれを行うことができない場合、私はあなたが別の@ConfigurationのクラスにSecurityConfigからPasswordEncoderため@Bean方法を動かすことで悪循環を断ち切ることができると思います。