org.springframework.context.ApplicationContext.getBean()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(131)

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

ApplicationContext.getBean介绍

暂无

代码示例

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

@Test
public void propertyPlaceholderLocation() throws Exception {
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
      "contextNamespaceHandlerTests-location.xml", getClass());
  assertEquals("bar", applicationContext.getBean("foo"));
  assertEquals("foo", applicationContext.getBean("bar"));
  assertEquals("maps", applicationContext.getBean("spam"));
}

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

@Test
public void jaxb2ContextPathMarshaller() {
  Jaxb2Marshaller jaxb2Marshaller = applicationContext.getBean("jaxb2ContextPathMarshaller", Jaxb2Marshaller.class);
  assertNotNull(jaxb2Marshaller);
}

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

@Test
public void testBeanDefinitionRegistryPostProcessorConfig() {
  ApplicationContext ctx = new AnnotationConfigApplicationContext(BeanDefinitionRegistryPostProcessorConfig.class);
  assertTrue(ctx.getBean("myTestBean") instanceof TestBean);
}

代码示例来源: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

private void testMetaClass(String xmlFile) {
  // expect the exception we threw in the custom metaclass to show it got invoked
  try {
    ApplicationContext ctx = new ClassPathXmlApplicationContext(xmlFile);
    Calculator calc = (Calculator) ctx.getBean("delegatingCalculator");
    calc.add(1, 2);
    fail("expected IllegalStateException");
  }
  catch (IllegalStateException ex) {
    assertEquals("Gotcha", ex.getMessage());
  }
}

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

@Before
public void onSetUp() throws Exception {
  ctx = new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass());
  AroundAdviceBindingTestAspect  aroundAdviceAspect = ((AroundAdviceBindingTestAspect) ctx.getBean("testAspect"));
  ITestBean injectedTestBean = (ITestBean) ctx.getBean("testBean");
  assertTrue(AopUtils.isAopProxy(injectedTestBean));
  this.testBeanProxy = injectedTestBean;
  // we need the real target too, not just the proxy...
  this.testBeanTarget = (TestBean) ((Advised) testBeanProxy).getTargetSource().getTarget();
  mockCollaborator = mock(AroundAdviceBindingCollaborator.class);
  aroundAdviceAspect.setCollaborator(mockCollaborator);
}

代码示例来源: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 propertyPlaceholder() throws Exception {
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
      "contextNamespaceHandlerTests-replace.xml", getClass());
  assertEquals("bar", applicationContext.getBean("string"));
  assertEquals("null", applicationContext.getBean("nullString"));
}

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

@Test
public void jaxb2ClassesToBeBoundMarshaller() {
  Jaxb2Marshaller jaxb2Marshaller = applicationContext.getBean("jaxb2ClassesMarshaller", Jaxb2Marshaller.class);
  assertNotNull(jaxb2Marshaller);
}

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

@Test
public void testBothBeansAreProxies() {
  Object tb = ctx.getBean("testBean");
  assertTrue(AopUtils.isAopProxy(tb));
  Object tb2 = ctx.getBean("testBean2");
  assertTrue(AopUtils.isAopProxy(tb2));
}

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

@Test
public void testWithTwoClassesDefinedInTheOneGroovyFile_WrongClassFirst() throws Exception {
  try {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("twoClassesWrongOneFirst.xml", getClass());
    ctx.getBean("messenger", Messenger.class);
    fail("Must have failed: two classes defined in GroovyScriptFactory source, non-Messenger class defined first.");
  }
  // just testing for failure here, hence catching Exception...
  catch (Exception expected) {
  }
}

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

@Test
public void propertyPlaceholderIgnored() throws Exception {
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
      "contextNamespaceHandlerTests-replace-ignore.xml", getClass());
  assertEquals("${bar}", applicationContext.getBean("string"));
  assertEquals("null", applicationContext.getBean("nullString"));
}

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

/**
 * Tests that Configuration classes are registered according to convention
 * @see org.springframework.beans.factory.support.DefaultBeanNameGenerator#generateBeanName
 */
@Test
public void defaultConfigClassBeanNameIsGeneratedProperly() {
  ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
  // attempt to retrieve the instance by its generated bean name
  Config configObject = (Config) context.getBean("annotationConfigApplicationContextTests.Config");
  assertNotNull(configObject);
}

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

@Test
public void withNonStaticBeanMethodAndInterface() {
  MyBeanImpl.initialized = false;
  ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStaticAndInterface.class);
  MyBean bean = ctx.getBean("myBean", MyBean.class);
  assertFalse(MyBeanImpl.initialized);
  bean.doIt();
  assertTrue(MyBeanImpl.initialized);
}

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

@Test
public void appliesAspectToClassWithComplexConstructor() {
  @SuppressWarnings("resource")
  ApplicationContext context = new ClassPathXmlApplicationContext("ObjenesisProxyTests-context.xml", getClass());
  ClassWithComplexConstructor bean = context.getBean(ClassWithComplexConstructor.class);
  bean.method();
  DebugInterceptor interceptor = context.getBean(DebugInterceptor.class);
  assertThat(interceptor.getCount(), is(1L));
  assertThat(bean.getDependency().getValue(), is(1));
}

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

@Test
public void testRefreshableFactoryBean() {
  ApplicationContext context = new ClassPathXmlApplicationContext("groovyContext.xml", getClass());
  Object factory = context.getBean("&refreshableFactory");
  assertTrue(factory instanceof FactoryBean);
  Object result = context.getBean("refreshableFactory");
  assertTrue(result instanceof String);
  assertEquals("test", result);
}

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

/**
 * Tests that specifying @Configuration(value="foo") results in registering
 * the configuration class with bean name 'foo'.
 */
@Test
public void explicitConfigClassBeanNameIsRespected() {
  ApplicationContext context = new AnnotationConfigApplicationContext(ConfigWithCustomName.class);
  // attempt to retrieve the instance by its specified name
  ConfigWithCustomName configObject = (ConfigWithCustomName) context.getBean("customConfigBeanName");
  assertNotNull(configObject);
}

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

@Test
public void withNonStaticBeanMethod() {
  MyBeanImpl.initialized = false;
  ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStatic.class);
  MyBean bean = ctx.getBean("myBean", MyBean.class);
  assertFalse(MyBeanImpl.initialized);
  bean.doIt();
  assertTrue(MyBeanImpl.initialized);
}

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

@Test
public void testFactoryBean() {
  ApplicationContext context = new ClassPathXmlApplicationContext("groovyContext.xml", getClass());
  Object factory = context.getBean("&factory");
  assertTrue(factory instanceof FactoryBean);
  Object result = context.getBean("factory");
  assertTrue(result instanceof String);
  assertEquals("test", result);
}

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

@Test
public void test() {
  ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class, Bar.class);
  assertNotNull(ctx.getBean(Bar.class).foo);
}

相关文章