gpt4 book ai didi

spring - 如何在实现 Condition/ConfigurationCondition 接口(interface)的类中使用 @Value 或 Environment

转载 作者:行者123 更新时间:2023-12-02 09:08:14 28 4
gpt4 key购买 nike

我只使用 JavaConfig。

我有以下声明:

@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}

根据以下帖子,JavaConfig 是强制性的: Spring 3.2 @value annotation with pure java configuration does not work, but Environment.getProperty works

以下代码工作完美(通过测试目的有许多@Values):

@Configuration
public class ActiveMQServerConfiguration {

@Value("${localhost.address}")
private String localHost;

@Value("${remotehost.address}")
private String remoteHost;

@Value("${localhost.port}")
private Integer localPort;

@Value("${remotehost.port}")
private Integer remotePort;

@Bean(name="connectionFactory")
@Conditional(LocalHostStatusCondition.class)
public ActiveMQConnectionFactory localConnectionFactory(
@Value("${localhost.protocol}") String protocol,
@Value("${localhost.address}") String host,
@Value("${localhost.port}") String port ){

System.out.println("protocol: "+protocol);
System.out.println("host: "+host);
System.out.println("port: "+port);

System.out.println("localHost: "+localHost);
System.out.println("localPort: "+localPort);
System.out.println("remoteHost: "+remoteHost);
System.out.println("remotePort: "+remotePort);

我可以在控制台/终端中看到

阿尔法

protocol: tcp
host: 127.0.0.1
port: 61616
localHost: 127.0.0.1
localPort: 61616
remoteHost: 192.168.1.34
remotePort: 61616

但是以下内容并不按预期工作:

public class LocalHostStatusCondition implements Condition {

private static final Logger logger = LoggerFactory.getLogger(LocalHostStatusCondition.class);

@Value("${localhost.address}")
private String localHost;

@Value("${remotehost.address}")
private String remoteHost;

@Value("${localhost.port}")
private Integer localPort;

@Value("${remotehost.port}")
private Integer remotePort;

@Autowired
private Environment environment;

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
logger.info("LocalHostStatusCondition...");

System.out.println("localHost: "+localHost);
System.out.println("localPort: "+localPort);
System.out.println("remoteHost: "+remoteHost);
System.out.println("remotePort: "+remotePort);
System.out.println("Env..." + environment.getProperty("localhost.address", String.class) );

几乎相同,甚至与环境一起工作

输出为:

测试版

localHost: null
localPort: null
remoteHost: null
remotePort: null
[WARNING]
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at com.manuel.jordan.infrastructure.support.LocalHostStatusCondition.matches(LocalHostStatusCondition.java:42)

根据Condition API :

Conditions must follow the same restrictions as BeanFactoryPostProcessor and take care to never interact with bean instances. For more fine-grained control of conditions that interact with @Configuration beans consider the ConfigurationCondition interface.

关于ConfigurationCondition 我已阅读此文When to use Spring @ConfigurationCondition vs. @Condition?也是

我已编辑以下内容:

public class LocalHostStatusCondition implements ConfigurationCondition {



@Override
public ConfigurationPhase getConfigurationPhase() {
return ConfigurationPhase.PARSE_CONFIGURATION;
}

如果我使用:

  • ConfigurationPhase.REGISTER_BEAN 我得到与 Beta 相同的输出
  • ConfigurationPhase.PARSE_CONFIGURATION; 我没有收到任何错误,但是...

但是控制台/终端没有显示任何有关句子的信息,我的意思是,System.out 没有打印(我不明白这种奇怪的行为)。

我该如何解决这个问题?

I need have working @Value or Environment, it with the purpose to let know the application if should connect to a local or remote ActiveMQ server.

是的,我已阅读以下内容: How do I delay evaluation of a Spring @Conditional configuration annotation?好像没有解决办法……

谢谢

最佳答案

你有

@Conditional(LocalHostStatusCondition.class)

Spring 采用您指定的 Class 类型并实例化它以使用其 matches 方法。它不会将实例视为 bean,不会对其执行 Autowiring ,也不会进行任何 @Value 处理。后者对于 BeanFactoryPostProcessor 也适用。

如果需要,您可以从 ConditionContext 检索Environment。另一种方法是自己加载属性。

关于spring - 如何在实现 Condition/ConfigurationCondition 接口(interface)的类中使用 @Value 或 Environment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25276463/

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