[英]Spring boot - How to avoid using new in Spring boot class annotated as component
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected AuthenticationManager authenticationManager() throws Exception {
listOfProviders.add(racfAuthenticationProvider);
listOfProviders.add(ldapAuthenticationProvider());
return new CustomProviderManager(listOfProviders); // <- have a look here
}
}
CustomProviderManager declaration: CustomProviderManager声明:
@Component
public class CustomProviderManager extends ProviderManager {
private AuthenticationEventPublisher eventPublisher = new NullEventPublisher();
private AuthenticationManager parent;
private boolean eraseCredentialsAfterAuthentication = true;
public CustomProviderManager(List<AuthenticationProvider> providers) {
super(providers);
}
As can be seen from the code snippet above:从上面的代码片段可以看出:
return new CustomProviderManager(listOfProviders);
Produces the following error:产生以下错误:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customProviderManager' defined in file [/opt/hsd/tomcat-9.0.43/webapps/csesonlineapp/WEB-INF/classes/gov/nm/cses/gen/CustomProviderManager.class]: Unsatisfied dependency expressed through constructor parameter 0; org.springframework.beans.factory.UnsatisfiedDependencyException:在文件 [/opt/hsd/tomcat-9.0.43/webapps/csesonlineapp/WEB-INF/classes/gov/nm/cses/gen 中定义的名称为“customProviderManager”的创建 bean 时出错/CustomProviderManager.class]:通过构造函数参数0表示的不满足的依赖关系; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityConfig' defined in file [/opt/hsd/tomcat-9.0.43/webapps/csesonlineapp/WEB-INF/classes/gov/nm/cses/gen/SpringSecurityConfig.class]: Post-processing of merged bean definition failed;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在文件 [/opt/hsd/tomcat-9.0.43/webapps/csesonlineapp/WEB-INF/classes/gov/nm/ 中定义的名称为“springSecurityConfig”的 bean 创建时出错cses/gen/SpringSecurityConfig.class]:合并bean定义的后处理失败; nested exception is java.lang.IllegalStateException: Failed to introspect Class [gov.nm.cses.gen.SpringSecurityConfig] from ClassLoader [ParallelWebappClassLoader^M context: csesonlineapp^M delegate: false^M ----------> Parent Classloader:^M java.net.URLClassLoader@38bc8ab5嵌套异常是 java.lang.IllegalStateException: 无法从 ClassLoader [ParallelWebappClassLoader^M context: csesonlineapp^M delegate: false^M ---------- > 父类加载器:^M java.net.URLClassLoader@38bc8ab5
I believe this is happening due to a new
operator instead of Autowiring when classes are annotated as Component.我相信这是由于当类被注释为组件时使用new
运算符而不是自动装配而发生的。 Please correct me if I am wrong.如果我错了,请纠正我。
How can I avoid using new in the above case?在上述情况下如何避免使用 new ?
Why do you want the class CustomProviderManager
to be component - using @Component?为什么您希望 class CustomProviderManager
成为组件 - 使用 @Component?
Just remove @Component from the class, the problem will be gone.只需从 class 中删除 @Component,问题就会消失。
If you want autowire AuthenticationManager
, just autowire in SpringSecurityConfig
class如果你想要自动装配AuthenticationManager
,只需在SpringSecurityConfig
class 中自动装配
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.