Announcement Announcement Module
Collapse
No announcement yet.
set property on context managed bean programmatically? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • set property on context managed bean programmatically?

    I have a requirement of accessing an ApplicationContext managed bean and setting properties on it programmatically. Bean class type is not known until runtime or relevant.

    Its very simple to just use a bean wrapper to do this:

    <code>
    private void overrideBeanProperty(ApplicationContext context, String beanName,
    String name, String value) throws BeansException {

    Object bean = context.getBean(beanName);
    BeanWrapperImpl bw = new BeanWrapperImpl(bean);
    bw.setPropertyValue(name, value);
    }
    </code>

    This works; getting the bean again from the context has the correct prop. But, is this Springish? If the bean partakes in some context autowiring or aop stuff it may not satisfy some contracts.

    I've been studyig the API but have had no luck with other methods such as getting a child or root bean definition and calling
    setPropertyValues(MutablePropertyValues ). I looked at AbstractAutoWireCapableBeanFactory.applyBeanProper tyValues(Object, String) but it seems this is for framework developers not "users". Maybe I'm missing something obvious, of course.

    Any suggestions?

  • #2
    Here's an example from my application:

    Properties properties = new Properties();
    context = new ClassPathXmlApplicationContext(CONTEXT_LOCATIONS, false, parentContext);
    properties.put("jmsConsumers.messageSelector", "Name = '" + name + "'");
    PropertyOverrideConfigurer poc = new PropertyOverrideConfigurer();
    poc.setProperties(properties);
    context.addBeanFactoryPostProcessor(poc);
    context.refresh();
    And it is completely "springish" (oh, and don't fret, it isn't obvious!)

    Cheers
    Steve

    Comment


    • #3
      Excellent!!!!

      Thanks.

      Comment

      Working...
      X