RuntimeBeanReference设置为静态字段

时间:2017-06-29 13:05:37

标签: java spring

我有一个带有静态字段的类,我想在运行时为此字段设置一个bean引用。我就是这样写的。

@Service
public class Beauty extends Base{
    public static staticService;

    @Override
    protected void initField() {
        initBean()
    }
}

Base.class

public abstract class Base implements BeanFactoryAware, BeanFactoryPostProcessor {

    private BeanFactory beanFactory;

    private ConfigurableListableBeanFactory configurableListableBeanFactory;

    protected void initBean(){
        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClassName("com.StaticService");
        beanDefinition.setInitMethodName("init");

        MutablePropertyValues values = new MutablePropertyValues();
        values.addPropertyValue("fieldA", "aValue");
        values.addPropertyValue("fieldB, "bValue");

        beanDefinition.setPropertyValues(values);

        String beanName = "staticService";
        ((BeanDefinitionRegistry) beanFactory).registerBeanDefinition(beanName, beanDefinition);
        String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(configurableListableBeanFactory, this.getClass());
        if (null == beanNames || beanNames.length > 1) {
            throw new RuntimeException("error with multi client");
        }
        BeanDefinition beautyBean = ((BeanDefinitionRegistry) beanFactory).getBeanDefinition(beanNames[0]);
        beautyBean.getPropertyValues().addPropertyValue(new PropertyValue(staticService, new RuntimeBeanReference(beanName)));
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        this.configurableListableBeanFactory = beanFactory;
        initField();
    }

    protected abstract void initField();
}

但是当我使用Beauty时。 staticService然后得到null。如何使它工作。

BTW:因为我想得到staticService bean可以被代理,所以我不能使用applicationContext.getBean来设置在被叫bean之前的字段。

1 个答案:

答案 0 :(得分:0)

试试这个:

public abstract class Base implements BeanFactoryAware, BeanFactoryPostProcessor{    
    private AutowireCapableBeanFactory beanFactory;

    private ConfigurableListableBeanFactory configurableListableBeanFactory;

    protected void initBean(){
        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClassName("com.StaticService.StaticService");
        beanDefinition.setInitMethodName("init");

        MutablePropertyValues values = new MutablePropertyValues();
        values.addPropertyValue("fieldA", "aValue");
        values.addPropertyValue("fieldB", "bValue");
        beanDefinition.setPropertyValues(values);

        String beanName = "staticService";
        ((BeanDefinitionRegistry) beanFactory)
                     .registerBeanDefinition(beanName, beanDefinition);
        beanFactory.autowireBeanProperties(this,
                      AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) 
                                        throws BeansException {
        this.beanFactory = (AutowireCapableBeanFactory) beanFactory;
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) 
                                                        throws BeansException {
        this.configurableListableBeanFactory = beanFactory;
        initBean();
    }

    protected abstract void initField();
}

和美容服务

@Service
public class Beauty extends Base{
    public static StaticService staticService;

    @Override
    protected void initField() {
        initBean();
    }

    public StaticService getStaticService() {
        return staticService;
    }

    public void setStaticService(StaticService staticService) {
        this.staticService = staticService;
    }
}