Spring Java Config |如何从BeanDefinition获取IndexedArgumentsValue
考虑以下示例: 配置类 @配置 公共类MyTestConfig{Spring Java Config |如何从BeanDefinition获取IndexedArgumentsValue,spring,spring-java-config,Spring,Spring Java Config,考虑以下示例: 配置类 @配置 公共类MyTestConfig{ @Value("${const.arg.a}") String constArgA; @Value("${const.arg.b}") String constArgB; @Bean public static PropertyPlaceholderConfigurer properties(){ SystemPropertyPlaceholderConfigurer propertyConfigurer = new S
@Value("${const.arg.a}") String constArgA;
@Value("${const.arg.b}") String constArgB;
@Bean
public static PropertyPlaceholderConfigurer properties(){
SystemPropertyPlaceholderConfigurer propertyConfigurer = new SystemPropertyPlaceholderConfigurer();
propertyConfigurer.setLocation( "test.properties" );
propertyConfigurer.setPropertyPrefixValue("TEST");
propertyConfigurer.setIgnoreUnresolvablePlaceholders( true );
return propertyConfigurer;
}
@Bean
public SpringTest springTest(){
return new SpringTest(constArgA, constArgB);
}
}
豆
公开课春季考试{
private String constArgA;
private String constArgB;
public SpringTest(String constArgA, String constArgB){
this.constArgA = constArgA;
this.constArgB = constArgB;
System.out.println("constArgA " + constArgA);
System.out.println("constArgB " + constArgB);
}
public void sayHello(){
System.out.println("Hello, I hope this works");
System.out.println("constArgA " + constArgA);
System.out.println("constArgB " + constArgB);
}
}
SystemPropertyPlaceholderConfigurer是PropertyPlaceholderConfigurer的扩展,我们在其中尝试操作一些Bean构造函数参数
对于我的用例,我需要访问构造函数参数,但是如果我从ConfigurableListableBeanFactory获得BeanDefinition,我会看到一个空列表
准确地说,Bean“SpringTest”的以下所有内容都是空的:
beanDefinition.getPropertyValues();
beanDefinition.getConstructorArgumentValues().getIndexedArgumentValues();
beanDefinition.getConstructorArgumentValues().getGenericArgumentValues();
我希望在ConstructorArgumentValues中接收constrarga和constrargb
现在,如果我将基于Java的配置替换为基于Xml的配置,如下所示:
现在,如果我尝试访问我的BeanDefinition,我将在中获得2个构造函数参数:
beanDefinition.getConstructorArgumentValues.getIndexedArgumentValues
您能帮助我理解这两种配置方式的区别吗?如果我想使用基于Java的配置,如何访问bean上的构造函数参数?我基本上需要将属性文件中的属性作为构造函数参数注入,并需要通过我的PropertyPlaceHolderConfigure实现对其执行一些处理。在@configuration类中,我通常这样做
@Configuration
@PropertySource("classpath:/test.properties")
public class MyTestConfig {
@Resource
private Environment env;
@Bean
public SpringTest springTest(){
return new SpringTest(env.getRequiredProperty("const.arg.a"), env.getRequiredProperty("const.arg.b"));
}
}
谢谢你的回复,但我想理解的是,为什么我的Bean“SpringTest”没有返回任何东西:BeanDefinition BeanDefinition=configurableListableBeanFactory.getBeanSpringTest.class;beanDefinition.GetPropertyValue;beanDefinition.getConstructorArgumentValues.getIndexedArgumentValues;beanDefinition.getConstructorArgumentValues.getGenericArgumentValues;如果我使用基于Java的配置,bean上的构造函数参数返回空列表,但是如果我使用基于xml的配置,则返回2个参数。
@Configuration
@PropertySource("classpath:/test.properties")
public class MyTestConfig {
@Resource
private Environment env;
@Bean
public SpringTest springTest(){
return new SpringTest(env.getRequiredProperty("const.arg.a"), env.getRequiredProperty("const.arg.b"));
}
}