热卖商品
新闻详情
Spring Boot测试 - 没有合格Bean异常 -Java 学习之路
来自 : www.javaroad.cn/questions/45..
发布时间:2021-03-24
此Spring Boot应用程序依赖于其他三个@Configuration类:AppConfig,SecurityConfig和AuthServerConfig . 对于此问题,SecurityConfig和AppConfig类是相关的(它们包括对PasswordEncoder bean的引用) .
AppConfig(部分)
@Configurationpublic class AppConfig { @Value( ${spring.datasource.url} ) private String datasourceUrl; @Value( ${spring.datasource.driverClassName} ) private String dbDriverClassName; @Value( ${spring.datasource.username} ) private String dbUsername; @Value( ${spring.datasource.password} ) private String dbPassword; @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); /// more code here
SecurityConfig:
@Configuration@EnableGlobalMethodSecurity(prePostEnabled = true)@EnableWebSecuritypublic class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private OAuthUserDetailsService userService; @Autowired private PasswordEncoder passwordEncoder; @Bean @Override protected AuthenticationManager authenticationManager() throws Exception { return super.authenticationManager(); // Hash password @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userService) .passwordEncoder(passwordEncoder); @Override protected void configure(HttpSecurity http) throws Exception { http .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and() .httpBasic() .realmName( test ) .and() .csrf() .disable();
UserRepository类非常简单:
public interface UserRepository extends CrudRepository OAuthUser, Long { public OAuthUser findByUserName(String name);
如何确保为我的测试找到所有必需的bean?谢谢 .
编辑:
如果我尝试在我的测试类中@Autowire bean,我会收到同样的错误 .
@Autowired private PasswordEncoder passwordEncoder;
本文链接: http://benaglobal.immuno-online.com/view-711796.html
发布于 : 2021-03-24
阅读(0)
最新动态
2021-03-24
2021-03-24
2021-03-24
2021-03-24
2021-03-24
2021-03-24
2021-03-24
2021-03-24
2021-03-24
2021-03-24
2021-03-24
2021-03-24
联络我们