gpt4 book ai didi

java - 无法从 BeanPostProcessor 中的占位符获取值

转载 作者:行者123 更新时间:2023-12-04 08:04:38 26 4
gpt4 key购买 nike

我正在将 Spring 与 Spring Boot BOM 2.4.0 附带的遗留 Tomcat 应用程序(不是 Spring Boot)一起使用,问题类似于 Spring Expression Language not working但我有一个具体案例。
如果我有一个带有 @Value("${spring.kafka.maximumRequestSize:15728640}") 的依赖类和以下

@Configuration
@Order
public class KafkaTracingDecorator implements ApplicationContextAware {
private KafkaTracing kafkaTracing;

@Override
public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {

kafkaTracing = applicationContext.getBean(KafkaTracing.class);
}
}
有用。以下也有效,但我没有在后处理器上做任何事情
@Configuration
@Order
public class KafkaTracingDecorator implements BeanPostProcessor, ApplicationContextAware {

@Override
public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException {

return bean;
}

@Override
public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {

// kafkaTracing = applicationContext.getBean(KafkaTracing.class);
}
}
但是当我尝试实现 BeanPostProcessorimplement the decorator pattern像这样
@Configuration
@Order
public class KafkaTracingDecorator implements BeanPostProcessor, ApplicationContextAware {

private KafkaTracing kafkaTracing;

@Override
public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException {

if (bean instanceof KafkaProducer) {
return kafkaTracing.producer((KafkaProducer) bean);
} else if (bean instanceof KafkaConsumer) {
return kafkaTracing.consumer((KafkaConsumer) bean);
} else {
return bean;
}
}

@Override
public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {

kafkaTracing = applicationContext.getBean(KafkaTracing.class);
}
}
我得到 Caused by: java.lang.NumberFormatException: For input string: "${spring.kafka.maximumRequestSize:15728640}"在创建 WebApplicationContext 时。
即使我不使用该值也会发生这种情况,因此它不会进入方法
@Configuration
@Order
public class KafkaTracingDecorator implements BeanPostProcessor, ApplicationContextAware {

private KafkaTracing kafkaTracing;

@Override
public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException {

return bean;
}

@Override
public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {

kafkaTracing = applicationContext.getBean(KafkaTracing.class);
}
}
这是一个非常简化的示例,它也以同样的方式失败:
@Configuration
public class KafkaTracingDecorator implements BeanPostProcessor {

@Value("${spring.kafka.maximumRequestSize:15728640}")
private int maxRequestSize;

}
(请注意,这在 Spring Boot 上按预期工作,因此可能与 Spring 的非引导使用有关)
导致错误的部分堆栈跟踪未列出任何自定义代码:
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1413)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:213)
at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:270)
at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:761)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:566)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:401)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:292)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4689)
MVCE 添加: https://github.com/trajano/spring-beanpostproc-mvce链接到 GitHub 问题,以防它是一个错误 https://github.com/spring-projects/spring-framework/issues/26571而不是使用问题。

最佳答案

您需要更新 Decorator通过注册 PropertySourcesPlaceholderConfigurer 来上课 bean 来解决你的问题。
根据Spring Docs on PropertyPlaceholderConfigurer :

PropertyPlaceholderConfigurer is a child class of PlaceholderConfigurerSupport. This child class helps in resolving ${...} placeholders against local properties and/or system properties and environment variables.


因此,配置此 bean 将加载 应用程序属性 从类路径中,它将能够解析 ${..}用于 @Value注释以获取实际值集。
更新 装修 类(class) :
@Configuration
public class Decorator implements BeanPostProcessor {

@Value("${spring.kafka.maximumRequestSize}")
private int maxRequestSize;

@Bean
public static PlaceholderConfigurerSupport placeholderConfigurerSupport() {
PlaceholderConfigurerSupport support = new PropertySourcesPlaceholderConfigurer();
support.setLocations(new ClassPathResource("application.properties"));
return support;
}

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println(maxRequestSize);
return bean;
}
}
应用程序属性 :
spring.kafka.maximumRequestSize=15728640
的控制台日志 Tomcat 8.5.57 运行于 采用OpenJDK 11.0.6 :
...
.....
INFO: Initializing Spring root WebApplicationContext
Feb 20, 2021 9:21:41 AM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
15728640
Feb 20, 2021 9:21:41 AM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext initialized in 518 ms
Feb 20, 2021 9:21:41 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Feb 20, 2021 9:21:41 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1419 ms

关于java - 无法从 BeanPostProcessor 中的占位符获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66278338/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com