Example #1
0
 @Before
 public void setUp() {
   ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
   context.setConfigLocation(
       "/org/springframework/xd/dirt/module/ModuleDeployerTests-context.xml");
   OptionUtils.configureRuntime(new SingleNodeOptions(), context.getEnvironment());
   context.refresh();
   moduleDeployer = context.getBean(ModuleDeployer.class);
 }
  private ClassPathXmlApplicationContext getApplicationContext() {

    // We need to use ClassPathXmlApplicationContext here and pass a ClassLoader because
    // every plugin has his own classloader and Spring won't be able to find the xml file in
    // the default system classloader

    ClassLoader classLoader = this.getClass().getClassLoader();
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext();
    ctx.setClassLoader(classLoader);
    ctx.setConfigLocation("spring-context.xml");
    ctx.refresh();
    return ctx;
  }
  @Override
  protected void startOverride(Future<Void> startFuture) throws Exception {
    connector = new SpringVerticleConnector();
    final ClassPathXmlApplicationContext spring =
        new ClassPathXmlApplicationContext(createVertxContext(connector));
    spring.setId(contextName);
    spring.getEnvironment().setActiveProfiles("production");
    spring.setConfigLocation(contextName);

    spring.refresh();

    spring.start();
    this.spring = spring;
    connector.startFuture().compose(e -> startFuture.complete(), startFuture);
  }
  /**
   * Creates a new instance of ContainerLauncher.
   *
   * @param options The options that select transport, analytics, and other infrastructure options.
   * @param parentContext an optional parent context to set on the XDContainer's ApplicationContext.
   * @return a new ContainerLauncher instance
   */
  @SuppressWarnings("resource")
  public ContainerLauncher createContainerLauncher(
      ContainerOptions options, ApplicationContext parentContext) {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
    context.setConfigLocation(LAUNCHER_CONFIG_LOCATION);

    OptionUtils.configureRuntime(options, context.getEnvironment());

    if (parentContext == null) {
      parentContext = createParentContext();
    }

    context.setParent(parentContext);
    context.refresh();
    context.registerShutdownHook();

    ContainerLauncher launcher = context.getBean(ContainerLauncher.class);
    return launcher;
  }
 @Before
 public void createSpringContext() {
   try {
     log.info("creating spring context");
     PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
     Properties properties = new Properties();
     properties.setProperty("temp.dir", TMPDIR);
     configurer.setProperties(properties);
     ctx = new ClassPathXmlApplicationContext();
     ctx.addBeanFactoryPostProcessor(configurer);
     // ctx.setConfigLocation(
     // "org/drools/container/spring/beans/persistence/beansVarPersistence.xml" );
     ctx.setConfigLocation(
         "org/drools/container/spring/beans/persistence/beansVarPersistence_Env.xml");
     ctx.refresh();
   } catch (Exception e) {
     log.error("can't create spring context", e);
     throw new RuntimeException(e);
   }
 }
  public final void loadNode() {

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();

    PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
    configurer.setProperties(props);

    context.addBeanFactoryPostProcessor(configurer);
    context.setConfigLocation("net\\dfs\\remote\\filestorage\\spring-client.xml");
    context.refresh();
    context.start();

    /*		FileLocationTrackerImpl hash = new FileLocationTrackerImpl();
    		hash.removeAll();
    */
    log.info("Client Started");

    FileReceiverSupport receiveFile = (FileReceiverSupport) context.getBean("receiveFile");

    receiveFile.connectJavaSpace();
    receiveFile.retrieveFile();
  }
  /**
   * 根据配置文件名称获取ApplicationContext
   *
   * @param xmlName
   * @return
   */
  public static ApplicationContext loadContext(String xmlName) {
    ClassPathXmlApplicationContext context = contexts.get(xmlName);
    if (context == null) {
      if (StringUtils.isNotBlank(xmlName)) {
        List<Object> beanList = new ArrayList<Object>();
        context = new ClassPathXmlApplicationContext();
        context.setConfigLocation(xmlName);

        /** 添加BeanFactoryPostProcessor的目的是获得原始的bean对象, 直接getBean拿到的可能是代理后的对象 */
        context.addBeanFactoryPostProcessor(createProcessor(beanList));

        /** 在初始化之前先把context放到缓存中 防止在初始化过程中存在循环调用,反复初始化 造成程序卡死 */
        contexts.put(xmlName, context);
        context.refresh();

        for (Object bean : beanList) {
          // 解决跨locator的依赖
          resolveInjects(bean);
        }
      }
    }
    return context;
  }