java - 运行 SpringBoot jar 文件时出现 BeanCreation 异常
问题描述
我正在使用一些密码编码器和用户帐户创建一个 Spirng 安全应用程序。我在运行 .jar 文件时遇到问题。当我在 IDE 中运行它时,一切都很好。似乎必须在创建 userService 之前完成创建名为 passwordEncoder 的 Bean。我不知道如何执行这样的创作。或者也许我错了,问题是其他的。这是代码:
豆:
@Bean
public BCryptPasswordEncoder passwordEncoder() {
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
return bCryptPasswordEncoder;
}
用户服务
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
@Autowired
public UserService(UserRepository userRepository,
RoleRepository roleRepository,
BCryptPasswordEncoder bCryptPasswordEncoder) {
this.userRepository = userRepository;
this.roleRepository = roleRepository;
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
}
安全配置
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userDetailsService)
.passwordEncoder(bCryptPasswordEncoder);
}
例外
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'instalator': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService' defined in URL [jar:file:/home/suchar/Dokumenty/Java%20Training/JDict/target/JDictionary-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/pl/micsoc/dictionary/service/UserService.class]: Unsatisfied dependency expressed through constructor parameter 2; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfig': Unsatisfied dependency expressed through field 'bCryptPasswordEncoder'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'passwordEncoder': Requested bean is currently in creation: Is there an unresolvable circular reference?
解决方案
试试这个UserService
private BCryptPasswordEncoder bCryptPasswordEncoder;
@Autowired
public UserService(UserRepository userRepository,
RoleRepository roleRepository,
@Lazy BCryptPasswordEncoder bCryptPasswordEncoder) {
this.userRepository = userRepository;
this.roleRepository = roleRepository;
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
}
推荐阅读
- r - 我将如何在 58 个(变量)列上运行测试以比较 2 个不同的数据框
- excel - Excel VBA 将值添加到与特定标题匹配的整个列
- angular - 角度组件 html 无法识别 mat-libraries
- python - 如何在气流中获取 commit_id 和分支信息
- heroku - 解决 Heroku 上的 @salesforce 模块
- linux - 如何将 ssh 命令的日志记录到脚本中的文件中
- python - TKinter 库:不确定为什么当类固有 tk.Frame 时 menuButton 上的图像不出现
- python - Pyspark:将密集向量转换为列
- python - discord.py 将相同的字符串分配给 json 字典两次
- graphviz - 如何使用python在graphviz中将部分节点设置为相同等级