gpt4 book ai didi

java - 尝试将 spring boot war 部署到 tomcat8 时 Unresolved 循环引用?

转载 作者:搜寻专家 更新时间:2023-11-01 03:19:05 25 4
gpt4 key购买 nike

我是 spring 的新手,正在尝试使用 spring boot 和 jsp 创建一个网络应用程序,我可以在树莓派上使用 tomcat8 进行部署。我可以通过 sts 在嵌入式 tomcat 实例上部署我的应用程序,我还可以将 war 文件部署到 Jenkins 而不会出现任何错误。但是,当我将 war 添加到 tomcat8 webapps 文件夹并启动 tomcat 时,出现以下错误:

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 类添加setter 方法,但我仍然遇到了问题。任何帮助将不胜感激?

网络配置类

@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/**",
"/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());
}

}

用户服务实现

@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() + "/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));
}
}
});

}
}

最佳答案

我认为你应该通过Spring Reference关于 IoC 容器。

WebSecurityConfig类需要UserDetailsS​​ervice,由UserServiceImpl实现。此外,UserServiceImpl 需要 WebSecurityConfig 提供的 PasswordEncoder。这会导致循环引用。删除构造函数注入(inject)应该足以解决您的问题。

旁注:尽量不要使用构造函数注入(inject)。 Spring 在 DI 方面很聪明,但是如果你使用构造函数注入(inject),你就会迫使 spring 使用你的方式。这也可能导致循环引用错误。

我建议您至少浏览一下这篇文章:https://steveschols.wordpress.com/2012/06/05/i-was-wrong-constructor-vs-setter-injection/

关于java - 尝试将 spring boot war 部署到 tomcat8 时 Unresolved 循环引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36716671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com