简体   繁体   English

Spring 引导 - 如何避免在 Spring 引导中使用新的 class 注释为组件

[英]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.

相关问题 带有 spring boot @component 注释类的构建器模式 - Builder pattern with spring boot @component annotated class 使用 Spring Boot 在侦听器中调用用 @RequestScope 注释的类 - Calling a class annotated with @RequestScope inside a Listener using Spring boot Spring Boot - 如何通过名称获取用@Service 注释的类 - Spring Boot - how to get a class annotated with @Service by it's name 如何使用自定义注释在Spring Boot中隐藏带注释的字段? - How to hide annotated fields in Spring Boot using custom annotations? Spring Boot应用程序无法实例化一个类,除非使用Autowired对其进行了注释 - Spring Boot application cannot instantiate a class unless it is annotated with Autowired 避免加入类Spring Boot的多余字段 - Avoid unwanted fields of joining class spring boot 在Spring Boot应用程序中,如何为每个自定义注释的类调用我的代码? - In Spring Boot application, how can my code be called for each custom-annotated class? 如何避免在 Spring Boot 集成测试中使用拦截器 - How to avoid using an interceptor in Spring boot integration tests 如何避免使用Spring Boot通过另一个库配置外部库 - How to avoid to configure an external library by another library using Spring Boot 如何在 Spring Boot 单元测试中配置用@Mock 注释的字段 - How to configure field annotated with @Mock in Spring Boot Unit test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM