当前位置: 首页 > 知识库问答 >
问题:

Spring特性PlaceHolder配置器断开Autowire

淳于凯
2023-03-14

我对Spring和JasyptEncryptablePropertyPlaceHolderConfigure有问题。似乎我正在断开HandlerStageInterceptor组件的自动接线。当我删除与占位符相关的配置中的最后三个bean时,一切都按预期工作,拦截器被注册。当我将bean留在内存中时,通常自动连接的拦截器为null,Spring会抛出一个NullPointerException

我很难调试这个问题。所讨论的自动连线组件不包含任何需要解析/解密的属性的@Value用法,也不包含它的任何依赖项。

@Configuration
@ComponentScan({"com.example.package"})
@EnableWebMvc
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {
    @Autowired
    private HandlerStageInterceptor handlerStageInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        Logger.getAnonymousLogger().info("===== Registry: " + handlerStageInterceptor);
        registry.addInterceptor(handlerStageInterceptor);
    }

    @Bean
    public EnvironmentStringPBEConfig setupEnvironmentStringPBEConfig() {
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();

        config.setAlgorithm("PBEWithMD5AndDES");
        config.setPasswordEnvName("APP_ENCRYPTION_PASSWORD");

        return config;
    }

    @Bean
    public StandardPBEStringEncryptor setupStringEncryptor() {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();

        encryptor.setConfig(setupEnvironmentStringPBEConfig());

        return encryptor;
    }

    @Bean
    public PropertyPlaceholderConfigurer setupPropertyPlaceholderConfigurer() {
        EncryptablePropertyPlaceholderConfigurer configurer = new EncryptablePropertyPlaceholderConfigurer(setupStringEncryptor());

        configurer.setLocation(new DefaultResourceLoader().getResource("classpath:properties/application.${RUN_ENV}.properties"));
        configurer.setIgnoreResourceNotFound(false);
        configurer.setIgnoreUnresolvablePlaceholders(false);

        return configurer;
    }
}

堆栈跟踪:

java.lang.IllegalArgumentException: Interceptor is required
    org.springframework.util.Assert.notNull(Assert.java:112)
    org.springframework.web.servlet.config.annotation.InterceptorRegistration.<init>(InterceptorRegistration.java:49)
    org.springframework.web.servlet.config.annotation.InterceptorRegistry.addInterceptor(InterceptorRegistry.java:45)
    com.example.package.configuration.ApplicationConfiguration.addInterceptors(ApplicationConfiguration.java:52)
    org.springframework.web.servlet.config.annotation.WebMvcConfigurerComposite.addInterceptors(WebMvcConfigurerComposite.java:105)
    org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration.addInterceptors(DelegatingWebMvcConfiguration.java:55)
    org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.getInterceptors(WebMvcConfigurationSupport.java:226)
    org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.requestMappingHandlerMapping(WebMvcConfigurationSupport.java:198)
    org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$8a18d4f1.CGLIB$requestMappingHandlerMapping$15(<generated>)
    org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$8a18d4f1$$FastClassBySpringCGLIB$$c1cb4193.invoke(<generated>)
    org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312)
    org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$8a18d4f1.requestMappingHandlerMapping(<generated>)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:483)
    org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)
    org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:586)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1094)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:989)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:530)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:74)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1015)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:652)
    org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1575)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1533)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    java.lang.Thread.run(Thread.java:744)

根据Jukka的要求:

java.lang.RuntimeException: This shit is off-the-chain
    com.biblioboard.journal.configuration.ApplicationConfiguration.addInterceptors(ApplicationConfiguration.java:62)
    org.springframework.web.servlet.config.annotation.WebMvcConfigurerComposite.addInterceptors(WebMvcConfigurerComposite.java:105)
    org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration.addInterceptors(DelegatingWebMvcConfiguration.java:55)
    org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.getInterceptors(WebMvcConfigurationSupport.java:226)
    org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.requestMappingHandlerMapping(WebMvcConfigurationSupport.java:198)
    org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$53b9af71.CGLIB$requestMappingHandlerMapping$27(<generated>)
    org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$53b9af71$$FastClassBySpringCGLIB$$5c73fc1c.invoke(<generated>)
    org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312)
    org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$53b9af71.requestMappingHandlerMapping(<generated>)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:483)
    org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)
    org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:586)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1094)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:989)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:530)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:74)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1015)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:652)
    org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1575)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1533)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    java.lang.Thread.run(Thread.java:744)

共有1个答案

吴浩博
2023-03-14

我也面临同样的问题,只是用PropertiesFactoryBean解决了这个问题。我还想知道为什么PropertyPlaceHolderConfigure会阻止自动布线。

 类似资料:
  • 我可以看到,我可以使用以下代码以编程方式将状态设置为强制打开:CircuitBreaker强制打开状态 但是有没有一种方法可以设置一个属性,在应用程序启动时立即将状态设置为此,以便可以与测试一起使用呢?

  • 我有一个关于带Resilience4J(不仅仅是Resilience4J)的Spring Cloud断路器的快速问题。 这两个项目都非常棒。然而,目前,我们经常以撤退告终。这意味着,当第三方服务真的很好时,我们最终还是会选择后退。 这可能是我自己的问题,因此,我想问一个特定配置的问题。 我想告诉当前配置做以下操作:断路器。(我将使用好的,坏的,和一半好/一半坏的状态。 > 当状态是一半好/一半坏:

  • 特性开关(Feature Gates)是 Kubernetes 中用来开启实验性功能的配置,可以通过选项 --feature-gates 来给不同的组件(如 kube-apiserver、kube-controller-manager、kube-scheduler、kubelet、kube-proxy等)开启功能特性。 开关名称 默认开启 阶段 支持版本 Accelerators false Al

  • 我试图初始化log4j记录器使用SpringIOC和使用财产onfigurator.configure配置记录器,但财产onfigurator.configure不工作。 我的问题是如何将配置(log4j.properties)传递给Logger类? 波姆。xml 混淆班 log4j。性质

  • 根据入门指南(https://Resilience4j.readme.io/docs/get-started-3)和演示项目(https://github.com/Resilience4j/Resilience4j-spring-boot2-demo),我想自己测试它,创建一个更简单的测试应用程序。 代码如下:

  • 熔断是 linkerd 用于从负载均衡中去除不健康的服务实例的机制。可以在连接级别和请求级别检测不健康的实例。通过使用熔断,linkerd 可以将尝试路由最终失败的请求所花费的时间降到最低,从而释放资源并避免导致级联故障。 下面描述 linkerd 提供的两种类型的熔断器。 他们在 Finagle 的熔断器文档 中更详细的讲述。 快速失败 快速失败熔断发生在连接级别。启用快速失败时,在尝试向服务主