【问题标题】:How to add property to bean during runtime in Spring如何在 Spring 运行时向 bean 添加属性
【发布时间】:2017-06-16 11:35:45
【问题描述】:

我是 Spring 新手。我有以下 Person bean,其中包含名称、地址和年龄作为属性。现在我想在我的自定义 BeanFactoryPostProcessor 中为 Person bean 添加一个名为性别的新属性。我的 person bean 实现了 AttributeAccessor。

XML 配置文件

<bean id="PersonBean" class="com.mkyong.common.Person">             
   <property name="name" value="mkyong"></property>
   <property name="address" value="address ABC"></property>
   <property name="age" value="29"></property>                           
</bean>                                                                 
<bean class="com.mkyong.common.CustomBeanFactory"></bean>

自定义 BeanFactoryPostProcessor

public class CustomBeanFactory implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) throws BeansException {
    BeanDefinition beanDefinition = arg0.getBeanDefinition("PersonBean");
    beanDefinition.setAttribute("gender", "Male");
    }                                                                       
}

人物类
enter code here

public class Person implements AttributeAccessor{

private String name;
private String address;
private int age;

public Person(){
    System.out.println("Creating bean Person "+this);
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

@Override
public String[] attributeNames() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public Object getAttribute(String arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public boolean hasAttribute(String arg0) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public Object removeAttribute(String arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void setAttribute(String arg0, Object arg1) {
    // TODO Auto-generated method stub
}   
}

客户端程序

public static void main(String [] arg){
    ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"Spring-Autoscan.xml"});

    Person personObj = (Person)context.getBean("PersonBean");
    System.out.println("Value of gender attribute "+personObj.getAttribute("gender"));
}

如果我访问性别,我会得到 null

请告诉我如何动态设置和获取属性。

【问题讨论】:

    标签: java spring


    【解决方案1】:

    您的 setAttribute() 方法为空。它不存储值。

    public void setAttribute(String arg0, Object arg1) {
        // TODO Auto-generated method stub
    } 
    

    在其中创建一个Map,您可以在其中存储值并将其取回。像这样

    private Map<String, Object> map = new HashMap<>();
    
    public void setAttribute(String arg0, Object arg1) {
        map.put(arg0, arg1);
    } 
    
    public Object getAttribute(String arg0) {
        return map.get(arg0);
    }
    

    【讨论】:

    • 是的,我同意。但是Custom BeanFactoryPostProcessor类内部使用的beanDefinition.setAttribute方法有什么用
    猜你喜欢
    • 1970-01-01
    • 2011-12-10
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 2021-08-27
    • 2013-01-03
    相关资源
    最近更新 更多