SpringApplication子类

时间:2015-09-18 07:30:31

标签: spring-boot

我已经对SpringApplication进行了细分,以便执行递归引导:

package platform;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;

public class ContextOverrideSpringApplication extends SpringApplication {
    private ConfigurableApplicationContext overridingApplicationContext;

    public ContextOverrideSpringApplication(Class src, ConfigurableApplicationContext overridingApplicationContext) {
        super(src);
        this.overridingApplicationContext = overridingApplicationContext;
    }

    @Override
    protected void postProcessApplicationContext(ConfigurableApplicationContext context) {

        Utils.mergeProperties(context.getEnvironment().getPropertySources(),
                overridingApplicationContext.getEnvironment().getPropertySources(),true);
        DefaultListableBeanFactory  appBeanFact = (DefaultListableBeanFactory) context.getBeanFactory();
        ConfigurableListableBeanFactory ovrBeanFact = overridingApplicationContext.getBeanFactory();
        for (String beanName : ovrBeanFact.getBeanDefinitionNames()) {
            if (appBeanFact.containsBeanDefinition(beanName)) {
                appBeanFact.removeBeanDefinition(beanName);
            }
            appBeanFact.registerBeanDefinition(beanName,ovrBeanFact.getBeanDefinition(beanName));
        }
    }

}

当我使用run(args ...)调用它时,它可以工作,但使用不同的运行,例如run(Target.class),不应用覆盖。

我在这里做错了什么?

修订:

@SpringBootApplication
public class MySpringBootApplication {
    public static String BOOT_STRAPPER_NAME = "bootStrapper";

    public static void main(String[] args)  {

        CommandLinePropertySource commandLinePropertySource = new SimpleCommandLinePropertySource(args);
        MutablePropertySources mutProps = new MutablePropertySources();
        mutProps.addLast(commandLinePropertySource);
        PropertySources propSources = mutProps;

        Class target = MySpringBootApplication.class;

        AnnotationConfigApplicationContext bootCtx =
                new AnnotationConfigApplicationContext();
        bootCtx.scan(MySpringBootApplication.class.getPackage().getName());
        ConfigurableApplicationContext ctx = bootCtx;

        while (ctx.containsBean(BOOT_STRAPPER_NAME)) {
            // run the boot strapper
            BootStrapper bootStrapper = (BootStrapper) ctx.getBeanFactory().getBean(BOOT_STRAPPER_NAME);
            System.out.println("Running bootStrapper: " + bootStrapper.getClass().getTypeName());
            bootStrapper.setCallingContext(ctx);
            bootStrapper.init();
            ctx = bootStrapper.getCalledContext();
        }

        // initialise with overrides
        ContextOverrideSpringApplication app = new ContextOverrideSpringApplication(target,ctx);
        ctx = app.run(args);  // can't use a config class here

    }

} 

1 个答案:

答案 0 :(得分:0)

我在查看grepcode.com中的源代码后发现了这个问题。这个问题是SpringApplication中的一个错误。

只使用args的run()方法是动态的,因此它将调用我的重写方法。

但是另外两个run()方法是静态的。这些代码实例化一个SpringApplication类对象,然后调用run对此进行运行。所以我忽略了ContextOverrideSpringApplication中的覆盖。