Is it possible from Spring to inject the result of calling a method on a ref bean?

前端 未结 3 488
清歌不尽
清歌不尽 2020-12-07 14:38

Is it possible from Spring to inject the result of calling a method on a ref bean?

I\'m trying to refactor some cut/pasted code from two separate projects into a co

3条回答
  •  情书的邮戳
    2020-12-07 15:05

    Or in Spring 2.x, by using a BeanPostProcessor

    Typically, bean post processors are used for checking the validity of bean properties or altering bean properties (what you want to) according to particular criteria.

    public class MyClientBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware {
    
        private ApplicationContext applicationContext;
        public void setApplicationContext(ApplicationContext applicationContext) {
            this.applicationContext = applicationContext;
        }
    
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            return bean;
        }
    
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if((bean instanceof MyClient)) && (beanName.equals("MyClient"))) {
                Myregistry registryService = (Myregistry) applicationContext.getBean("registryService");
    
               ((MyClient) bean).setEndPoint(registryService.getEndPoint("bar"));
            }
    
            return bean;
        }
    }
    

    And register your BeanPostProcessor

    
    

提交回复
热议问题