org.springframework.context.support.ClassPathXmlApplicationContext类的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(384)

本文整理了Java中org.springframework.context.support.ClassPathXmlApplicationContext类的一些代码示例,展示了ClassPathXmlApplicationContext类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ClassPathXmlApplicationContext类的具体详情如下:
包路径:org.springframework.context.support.ClassPathXmlApplicationContext
类名称:ClassPathXmlApplicationContext

ClassPathXmlApplicationContext介绍

[英]Standalone XML application context, taking the context definition files from the class path, interpreting plain paths as class path resource names that include the package path (e.g. "mypackage/myresource.txt"). Useful for test harnesses as well as for application contexts embedded within JARs.

The config location defaults can be overridden via #getConfigLocations, Config locations can either denote concrete files like "/myfiles/context.xml" or Ant-style patterns like "/myfiles/*-context.xml" (see the org.springframework.util.AntPathMatcher javadoc for pattern details).

Note: In case of multiple config locations, later bean definitions will override ones defined in earlier loaded files. This can be leveraged to deliberately override certain bean definitions via an extra XML file.

This is a simple, one-stop shop convenience ApplicationContext. Consider using the GenericApplicationContext class in combination with an org.springframework.beans.factory.xml.XmlBeanDefinitionReaderfor more flexible context setup.
[中]独立XML应用程序上下文,从类路径获取上下文定义文件,将普通路径解释为包含包路径的类路径资源名称(例如“mypackage/myresource.txt”)。对于测试工具以及嵌入在JAR中的应用程序上下文非常有用。
配置位置默认值可以通过#getConfigLocations覆盖,配置位置可以表示具体的文件,如“/myfiles/context.xml”或Ant样式的模式,如“/myfiles/*-context.xml”(有关模式详细信息,请参阅org.springframework.util.AntPathMatcher javadoc)。
注意:在多个配置位置的情况下,稍后的bean定义将覆盖先前加载的文件中定义的bean定义。这可以用来通过一个额外的XML文件故意覆盖某些bean定义。
这是一个简单的一站式便利应用程序上下文。考虑使用GoNICAppultCortualCorp类与ORG组合。springframework。豆。工厂xml。XmlBeanDefinitionReader用于更灵活的上下文设置。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Test
@SuppressWarnings("resource")
public void testWithDependencyChecking() {
  ApplicationContext ctx = new ClassPathXmlApplicationContext(DEPENDENCY_CHECK_CONTEXT, getClass());
  ctx.getBean("testBean");
}

代码示例来源:origin: spring-projects/spring-framework

@Before
public void setup() {
  ClassPathXmlApplicationContext ctx =
      new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass());
  highPrecedenceAspect = (PrecedenceTestAspect) ctx.getBean("highPrecedenceAspect");
  lowPrecedenceAspect = (PrecedenceTestAspect) ctx.getBean("lowPrecedenceAspect");
  highPrecedenceSpringAdvice = (SimpleSpringBeforeAdvice) ctx.getBean("highPrecedenceSpringAdvice");
  lowPrecedenceSpringAdvice = (SimpleSpringBeforeAdvice) ctx.getBean("lowPrecedenceSpringAdvice");
  testBean = (ITestBean) ctx.getBean("testBean");
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testNoScopedProxy() {
  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
      "org/springframework/context/annotation/scopedProxyNoTests.xml");
  context.getBeanFactory().registerScope("myScope", new SimpleMapScope());
  ScopedProxyTestBean bean = (ScopedProxyTestBean) context.getBean("scopedProxyTestBean");
  // should not be a proxy
  assertFalse(AopUtils.isAopProxy(bean));
  context.close();
}

代码示例来源:origin: apache/incubator-dubbo

/**
   * In order to make sure multicast registry works, need to specify '-Djava.net.preferIPv4Stack=true' before
   * launch the application
   */
  public static void main(String[] args) throws Exception {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-provider.xml");
    context.start();
    System.in.read();
  }
}

代码示例来源:origin: apache/incubator-dubbo

/**
   * In order to make sure multicast registry works, need to specify '-Djava.net.preferIPv4Stack=true' before
   * launch the application
   */
  public static void main(String[] args) {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml");
    context.start();
    DemoService demoService = context.getBean("demoService", DemoService.class);
    String hello = demoService.sayHello("world");
    System.out.println("result: " + hello);
  }
}

代码示例来源:origin: apache/incubator-dubbo

@Override
public void stop() {
  try {
    if (context != null) {
      context.stop();
      context.close();
      context = null;
    }
  } catch (Throwable e) {
    logger.error(e.getMessage(), e);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void staticScriptWithNullReturnValue() {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("bshContext.xml", getClass());
  assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerWithConfig"));
  ConfigurableMessenger messenger = (ConfigurableMessenger) ctx.getBean("messengerWithConfig");
  messenger.setMessage(null);
  assertNull(messenger.getMessage());
  assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testJsr223FromTagWithInterface() throws Exception {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd-jsr223.xml", getClass());
  assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerWithInterface"));
  Messenger messenger = (Messenger) ctx.getBean("messengerWithInterface");
  assertFalse(AopUtils.isAopProxy(messenger));
}

代码示例来源:origin: spring-projects/spring-framework

@Before
public void setup() {
  this.context = new ClassPathXmlApplicationContext(
      "scheduledTasksContext.xml", ScheduledTasksBeanDefinitionParserTests.class);
  this.registrar = this.context.getBeansOfType(
      ScheduledTaskRegistrar.class).values().iterator().next();
  this.testBean = this.context.getBean("testBean");
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testChildWithProxy() throws Exception {
  ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(CONTEXT_WILDCARD);
  ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(
      new String[] {CHILD_WITH_PROXY_CONTEXT}, ctx);
  assertTrue(AopUtils.isAopProxy(child.getBean("assemblerOne")));
  assertTrue(AopUtils.isAopProxy(child.getBean("assemblerTwo")));
  ctx.close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void staticWithScriptReturningInstance() {
  ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bshContext.xml", getClass());
  assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerInstance"));
  Messenger messenger = (Messenger) ctx.getBean("messengerInstance");
  String desiredMessage = "Hello World!";
  assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
  assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
  ctx.close();
  assertNull(messenger.getMessage());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testContextWithClassNameThatContainsPlaceholder() {
  ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(CLASS_WITH_PLACEHOLDER_CONTEXT, getClass());
  assertTrue(ctx.containsBean("someMessageSource"));
  assertTrue(ctx.getBean("someMessageSource") instanceof StaticMessageSource);
  ctx.close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testSingleConfigLocation() {
  ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(FQ_SIMPLE_CONTEXT);
  assertTrue(ctx.containsBean("someMessageSource"));
  ctx.close();
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Return a bean factory with attributes and EnterpriseServices configured.
 */
protected BeanFactory getBeanFactory() throws IOException {
  return new ClassPathXmlApplicationContext(DEFAULT_CONTEXT, CLASS);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void customAnnotationUsedForBothComponentScanAndQualifier() {
  ClassPathXmlApplicationContext context = loadContext("customAnnotationUsedForBothComponentScanAndQualifierTests.xml");
  KustomAnnotationAutowiredBean testBean = (KustomAnnotationAutowiredBean) context.getBean("testBean");
  assertNotNull(testBean.getDependency());
  context.close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testCustomBeanNameGenerator() {
  ApplicationContext context = new ClassPathXmlApplicationContext(
      "org/springframework/context/annotation/customNameGeneratorTests.xml");
  assertTrue(context.containsBean("testing.fooServiceImpl"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testFactoryBeanAndApplicationListener() {
  ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(CONTEXT_WILDCARD);
  ctx.getBeanFactory().registerSingleton("manualFBAAL", new FactoryBeanAndApplicationListener());
  assertEquals(2, ctx.getBeansOfType(ApplicationListener.class).size());
  ctx.close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testWithBeanNameAutoProxyCreator() {
  ClassPathXmlApplicationContext bf = newContext("withBeanNameAutoProxyCreator.xml");
  ITestBean tb = (ITestBean) bf.getBean("adrian");
  assertEquals(68, tb.getAge());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void aspectjTypeFilter() {
  ClassPathXmlApplicationContext context = loadContext("aspectjTypeFilterTests.xml");
  assertTrue(context.containsBean("fooServiceImpl"));
  assertTrue(context.containsBean("stubFooDao"));
  assertFalse(context.containsBean("scopedProxyTestBean"));
  context.close();
}

代码示例来源:origin: spring-projects/spring-framework

@After
public void after() {
  if (enabled != null) {
    System.setProperty("ENABLED", enabled);
  }
  else {
    System.clearProperty("ENABLED");
  }
  if (context != null) {
    context.close();
  }
}

相关文章

微信公众号

最新文章

更多