gpt4 book ai didi

java - Spring bean 模板

转载 作者:行者123 更新时间:2023-12-01 13:53:23 24 4
gpt4 key购买 nike

假设有一个像这样的 bean 连接示例:

<bean id="abstractTemplateBean" class="com.somepackage.AbstractTemplateOfSomething" abstract="true">

<property name="templateproperties">
<map merge="true">
<entry key="componentCode">
<bean parent="someTemplate">
<property name="name" value="ThisIsTheValueForOverride" />
</bean>
</entry>
</map>
</property>
</bean>

<bean id="concreteBean" parent="abstractTemplateBean">

</bean>

是否有一种方法可以覆盖 templateProperties 映射中的属性 name 值,而无需编写整个样板文件。只是为了拥有相同类型的占位符,例如:

    <bean parent="someTemplate">
...
<property name="name" value="${this_is_place_holder}" />
...
</bean>

然后在声明具体 bean 后为此占位符提供值,如下所示:

<bean id="concreteBean" parent="abstractTemplateBean">
<property id="${this_is_place_holder}" value="concrete value for abstract template" />
</bean>

谢谢

最佳答案

我只是使用FactoryBean编写了一个演示来解决您的问题

这个想法是使用 FactoryBean 从属性填充目标 bean

详细代码

PropertySetFactoryBean.java

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

public class PropertySetFactoryBean implements FactoryBean{

private Object target;

public void setTarget(Object target) {
this.target = target;
}

private Properties properties;

public void setProperties(Properties properties) {
this.properties = properties;
}

private boolean populated = false;

private synchronized void populate() throws InvocationTargetException, IllegalAccessException {
if(populated) return;

BeanUtils.populate(target, properties);

populated = true;
}

public Object getObject() throws Exception {
populate();
return target;
}

public Class getObjectType() {
return target.getClass();
}

public boolean isSingleton() {
return false;
}
}

Bean.java

public class Bean {


private String val1;

public String getVal2() {
return val2;
}

public void setVal2(String val2) {
this.val2 = val2;
}

public String getVal1() {
return val1;
}

public void setVal1(String val1) {
this.val1 = val1;
}

private String val2;

}

测试.xml

<bean id="bean" class="PropertySetFactoryBean">
<property name="target">
<bean class="Bean" >
</bean>
</property>
<property name="properties">
<props>
<prop key="${key1}" >${val1}</prop>
</props>
</property>
</bean>


<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties">
<map>
<entry key="key1">
<value>val2</value>
</entry>
<entry key="val1">
<value>115551</value>
</entry>
</map>
</property>
</bean>

测试

    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("test.xml");
Bean bean = (Bean) applicationContext.getBean("bean");
System.out.println(bean.getVal1());
System.out.println(bean.getVal2());

输出

null
115551

关于java - Spring bean 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19790611/

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