作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在将 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);
}
}
但是当我尝试实现
BeanPostProcessor
至
implement 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.
${..}
用于
@Value
注释以获取实际值集。
@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/
我是一名优秀的程序员,十分优秀!