2016-04-19 11 views
3

私は春に新しく、springbootとjspを使ってWebアプリケーションを作成しようとしていますが、これはラズベリーパイのtomcat8を使って展開できます。埋め込みのTomcatインスタンス上にstを使ってアプリケーションをデプロイすることができます。また、Jenkinsにwarファイルをエラーなく展開することもできます。私は、Webアプリケーションのフォルダをtomcat8とTomcatを起動するために戦争を追加するときしかし、私は次のエラーを取得する:tomcat8にスプリングブート戦争を展開しようとすると、未解決の循環参照がありますか?

2016-04-19 10:54:41.384 WARN 5525 --- [ost-startStop-1] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.rcctv.controllers.UserController.setUserService(com.rcctv.services.UserService); nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl' defined in file [/usr/share/tomcat8/webapps/RaspberryCCTV-0.0.1-SNAPSHOT/WEB-INF/classes/com/rcctv/services/UserServiceImpl.class]: Unsatisfied dependency expressed through constructor argument with index 1 of type [org.springframework.security.crypto.password.PasswordEncoder]: : Error creating bean with name 'webSecurityConfig': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'userServiceImpl': Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSecurityConfig': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'userServiceImpl': Requested bean is currently in creation: Is there an unresolvable circular reference? 

私は@Lazyと私のコンフィギュレーションクラスに注釈を付けてみました、私のuserServiceImplクラスにセッターメソッドを追加しましたが、私はまだ持っています問題。どんな助けでも大変感謝していますか?

WebConfigのクラス

@Configuration 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Value("${rememberMe.privateKey}") 
    private String rememberMeKey; 

    @Value("${spring.profiles.active}") 
    private String env; 

    @Resource 
    private UserDetailsService userService; 

    @Bean 
    public HibernateJpaSessionFactoryBean sessionFactory() { 
     return new HibernateJpaSessionFactoryBean(); 
    } 

    @Bean 
    public RememberMeServices rememberMeServices() { 
     TokenBasedRememberMeServices rememberMeServices = new TokenBasedRememberMeServices(rememberMeKey, userService); 
     return rememberMeServices; 
    } 

    @Bean 
    public PasswordEncoder passwordEncoder() { 
     return new BCryptPasswordEncoder(); 
    } 

    @Bean 
    @Override 
    public AuthenticationManager authenticationManagerBean() throws Exception { 
     return super.authenticationManagerBean(); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .antMatchers("/", 
         "/home", 
         "/error", 
         "/signup", 
         "/forgot-password", 
         "/reset-password/*", 
         "/public/**", 
         "https://stackoverflow.com/users/*").permitAll() 
       .anyRequest().authenticated(); 
     http 
      .formLogin() 
       .loginPage("/login") 
       .defaultSuccessUrl("/raspberrycctv") 
       .permitAll().and() 
      .rememberMe().key(rememberMeKey).rememberMeServices(rememberMeServices()).and() 
      .logout() 
       .permitAll(); 

     if (!env.equals("dev")) 
      http.requiresChannel().anyRequest().requiresSecure(); 
    } 

    @Autowired 
    @Override 
    protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception { 
     authManagerBuilder.userDetailsService(userService).passwordEncoder(passwordEncoder()); 
    } 

} 

UserSeviceImpl

@Service 
@Transactional(propagation=Propagation.SUPPORTS, readOnly=true) 
public class UserServiceImpl implements UserService, UserDetailsService { 

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

    private UserRepository userRepository; 
    private PasswordEncoder passwordEncoder; 
    private MailSender mailSender; 

    @Autowired 
    public UserServiceImpl(UserRepository userRepository, 
      PasswordEncoder passwordEncoder, 
      MailSender mailSender) { 

     this.mailSender = mailSender; 
     this.userRepository = userRepository; 
     this.passwordEncoder = passwordEncoder; 

    } 

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

    @Autowired 
    public void setPasswordEncoder(PasswordEncoder passwordEncoder) { 
     this.passwordEncoder = passwordEncoder; 
    } 

    @Autowired 
    public void setMailSender(MailSender mailSender) { 
     this.mailSender = mailSender; 
    } 

    @Override 
    @Transactional(propagation=Propagation.REQUIRED, readOnly=false) 
    public void signup(SignupForm signupForm) { 
     final User user = new User(); 
     user.setEmail(signupForm.getEmail()); 
     user.setName(signupForm.getName()); 
     user.setPassword(passwordEncoder.encode(signupForm.getPassword())); 
     user.getRoles().add(Role.UNVERIFIED); 
     user.setVerificationCode(RandomStringUtils.randomAlphanumeric(16)); 
     userRepository.save(user); 

     TransactionSynchronizationManager.registerSynchronization(
       new TransactionSynchronizationAdapter() { 
        @Override 
        public void afterCommit() { 
         try { 
          String verifyLink = Utilities.hostUrl() + "https://stackoverflow.com/users/" + user.getVerificationCode() + "/verify"; 
          mailSender.send(user.getEmail(), Utilities.getMessage("verifySubject"), Utilities.getMessage("verifyEmail", verifyLink)); 
          logger.info("Verification mail to " + user.getEmail() + " queued."); 
         } catch (MessagingException e) { 
          logger.error(ExceptionUtils.getStackTrace(e)); 
         } 
        } 
      }); 

    } 
} 

答えて

1

私はあなたのIoCコンテナについてSpring Referenceて行くべきだと思います。

WebSecurityConfigクラスには、UserServiceImplによって実装されるUserDetailsS​​erviceが必要です。また、UserServiceImplには、WebSecurityConfigが提供するPasswordEncoderが必要です。これは循環参照を引き起こす。コンストラクタインジェクションを削除するだけで問題を解決できるはずです。

サイドノート:コンストラクタインジェクションを使用しないでください。 DIについては春が賢明ですが、コンストラクタインジェクションを使用する場合は、春にあなたの方法を使用するように強制しています。これにより、循環参照エラーが発生することもあります。私の問題を解決しhttps://steveschols.wordpress.com/2012/06/05/i-was-wrong-constructor-vs-setter-injection/

+0

私は、少なくともこの記事をすくい取るすることをお勧めいたします。どうもありがとう! – LiamMalone

関連する問題