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

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

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

ApplicationContext.getBeanNamesForType介绍

暂无

代码示例

代码示例来源:origin: alibaba/druid

public void afterPropertiesSet() throws Exception {
  Assert.notNull(targetBeanType, "targetType cannot be null");
  String[] beanNames = context.getBeanNamesForType(targetBeanType);
  Collections.addAll(this.beanNames, beanNames);
}

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

/**
 * Determine the names of candidate beans in the application context.
 * @since 5.1
 * @see #setDetectHandlerMethodsInAncestorContexts
 * @see BeanFactoryUtils#beanNamesForTypeIncludingAncestors
 */
protected String[] getCandidateBeanNames() {
  return (this.detectHandlerMethodsInAncestorContexts ?
      BeanFactoryUtils.beanNamesForTypeIncludingAncestors(obtainApplicationContext(), Object.class) :
      obtainApplicationContext().getBeanNamesForType(Object.class));
}

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

@Test
public void testInlineJsr223FromTag() throws Exception {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd-jsr223.xml", getClass());
  assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("inlineMessenger"));
  Messenger messenger = (Messenger) ctx.getBean("inlineMessenger");
  assertFalse(AopUtils.isAopProxy(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

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

代码示例来源: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 testStaticScriptWithInstance() throws Exception {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyContext.xml", getClass());
  assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerInstance"));
  Messenger messenger = (Messenger) ctx.getBean("messengerInstance");
  assertFalse("Shouldn't get proxy when refresh is disabled", AopUtils.isAopProxy(messenger));
  assertFalse("Scripted object should not be instance of Refreshable", messenger instanceof Refreshable);
  String desiredMessage = "Hello World!";
  assertEquals("Message is incorrect", desiredMessage, 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("jsr223-with-xsd.xml", getClass());
  assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerWithInterface"));
  Messenger messenger = (Messenger) ctx.getBean("messengerWithInterface");
  assertFalse(AopUtils.isAopProxy(messenger));
  assertEquals("Hello World!", messenger.getMessage());
}

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

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

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

@Test
public void testStaticScriptWithInstanceUsingJsr223() throws Exception {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyContextWithJsr223.xml", getClass());
  assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerInstance"));
  Messenger messenger = (Messenger) ctx.getBean("messengerInstance");
  assertFalse("Shouldn't get proxy when refresh is disabled", AopUtils.isAopProxy(messenger));
  assertFalse("Scripted object should not be instance of Refreshable", messenger instanceof Refreshable);
  String desiredMessage = "Hello World!";
  assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
  assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
}

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

@Test
public void testStaticScriptWithInlineDefinedInstance() throws Exception {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyContext.xml", getClass());
  assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerInstanceInline"));
  Messenger messenger = (Messenger) ctx.getBean("messengerInstanceInline");
  assertFalse("Shouldn't get proxy when refresh is disabled", AopUtils.isAopProxy(messenger));
  assertFalse("Scripted object should not be instance of Refreshable", messenger instanceof Refreshable);
  String desiredMessage = "Hello World!";
  assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
  assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
}

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

@Test
public void testStaticScriptWithInlineDefinedInstanceUsingJsr223() throws Exception {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyContextWithJsr223.xml", getClass());
  assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerInstanceInline"));
  Messenger messenger = (Messenger) ctx.getBean("messengerInstanceInline");
  assertFalse("Shouldn't get proxy when refresh is disabled", AopUtils.isAopProxy(messenger));
  assertFalse("Scripted object should not be instance of Refreshable", messenger instanceof Refreshable);
  String desiredMessage = "Hello World!";
  assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
  assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
}

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

@Test
public void testInlineJsr223FromTagWithInterface() throws Exception {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("jsr223-with-xsd.xml", getClass());
  assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("inlineMessengerWithInterface"));
  Messenger messenger = (Messenger) ctx.getBean("inlineMessengerWithInterface");
  assertFalse(AopUtils.isAopProxy(messenger));
  assertEquals("Hello World!", messenger.getMessage());
}

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

@Test  // SPR-6268
public void testRefreshableFromTagProxyTargetClass() throws Exception {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd-proxy-target-class.xml",
      getClass());
  assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("refreshableMessenger"));
  Messenger messenger = (Messenger) ctx.getBean("refreshableMessenger");
  assertTrue(AopUtils.isAopProxy(messenger));
  assertTrue(messenger instanceof Refreshable);
  assertEquals("Hello World!", messenger.getMessage());
  assertTrue(ctx.getBeansOfType(ConcreteMessenger.class).values().contains(messenger));
  // Check that AnnotationUtils works with concrete proxied script classes
  assertNotNull(AnnotationUtils.findAnnotation(messenger.getClass(), Component.class));
}

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

@Test
public void testRefreshableJsr223FromTagWithInterface() throws Exception {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("jsr223-with-xsd.xml", getClass());
  assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("refreshableMessengerWithInterface"));
  Messenger messenger = (Messenger) ctx.getBean("refreshableMessengerWithInterface");
  assertTrue(AopUtils.isAopProxy(messenger));
  assertTrue(messenger instanceof Refreshable);
  assertEquals("Hello World!", messenger.getMessage());
}

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

@Test
public void testRefreshableJsr223FromTag() throws Exception {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd-jsr223.xml", getClass());
  assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("refreshableMessenger"));
  Messenger messenger = (Messenger) ctx.getBean("refreshableMessenger");
  assertTrue(AopUtils.isAopProxy(messenger));
  assertTrue(messenger instanceof Refreshable);
  assertEquals("Hello World!", messenger.getMessage());
}

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

@Test
public void staticScript() {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("bshContext.xml", getClass());
  assertTrue(Arrays.asList(ctx.getBeanNamesForType(Calculator.class)).contains("calculator"));
  assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messenger"));
  Calculator calc = (Calculator) ctx.getBean("calculator");
  Messenger messenger = (Messenger) ctx.getBean("messenger");
  assertFalse("Scripted object should not be instance of Refreshable", calc instanceof Refreshable);
  assertFalse("Scripted object should not be instance of Refreshable", messenger instanceof Refreshable);
  assertEquals(calc, calc);
  assertEquals(messenger, messenger);
  assertTrue(!messenger.equals(calc));
  assertTrue(messenger.hashCode() != calc.hashCode());
  assertTrue(!messenger.toString().equals(calc.toString()));
  assertEquals(5, calc.add(2, 3));
  String desiredMessage = "Hello World!";
  assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
  assertTrue(ctx.getBeansOfType(Calculator.class).values().contains(calc));
  assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
}

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

@Test
public void testRefreshableFromTag() throws Exception {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd.xml", getClass());
  assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("refreshableMessenger"));
  Messenger messenger = (Messenger) ctx.getBean("refreshableMessenger");
  CallCounter countingAspect = (CallCounter) ctx.getBean("getMessageAspect");
  assertTrue(AopUtils.isAopProxy(messenger));
  assertTrue(messenger instanceof Refreshable);
  assertEquals(0, countingAspect.getCalls());
  assertEquals("Hello World!", messenger.getMessage());
  assertEquals(1, countingAspect.getCalls());
  assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
}

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

@Test
public void testStaticScript() throws Exception {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyContext.xml", getClass());
  assertTrue(Arrays.asList(ctx.getBeanNamesForType(Calculator.class)).contains("calculator"));
  assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messenger"));
  Calculator calc = (Calculator) ctx.getBean("calculator");
  Messenger messenger = (Messenger) ctx.getBean("messenger");
  assertFalse("Shouldn't get proxy when refresh is disabled", AopUtils.isAopProxy(calc));
  assertFalse("Shouldn't get proxy when refresh is disabled", AopUtils.isAopProxy(messenger));
  assertFalse("Scripted object should not be instance of Refreshable", calc instanceof Refreshable);
  assertFalse("Scripted object should not be instance of Refreshable", messenger instanceof Refreshable);
  assertEquals(calc, calc);
  assertEquals(messenger, messenger);
  assertTrue(!messenger.equals(calc));
  assertTrue(messenger.hashCode() != calc.hashCode());
  assertTrue(!messenger.toString().equals(calc.toString()));
  String desiredMessage = "Hello World!";
  assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
  assertTrue(ctx.getBeansOfType(Calculator.class).values().contains(calc));
  assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
}

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

@Test
public void testStaticScriptUsingJsr223() throws Exception {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyContextWithJsr223.xml", getClass());
  assertTrue(Arrays.asList(ctx.getBeanNamesForType(Calculator.class)).contains("calculator"));
  assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messenger"));
  Calculator calc = (Calculator) ctx.getBean("calculator");
  Messenger messenger = (Messenger) ctx.getBean("messenger");
  assertFalse("Shouldn't get proxy when refresh is disabled", AopUtils.isAopProxy(calc));
  assertFalse("Shouldn't get proxy when refresh is disabled", AopUtils.isAopProxy(messenger));
  assertFalse("Scripted object should not be instance of Refreshable", calc instanceof Refreshable);
  assertFalse("Scripted object should not be instance of Refreshable", messenger instanceof Refreshable);
  assertEquals(calc, calc);
  assertEquals(messenger, messenger);
  assertTrue(!messenger.equals(calc));
  assertTrue(messenger.hashCode() != calc.hashCode());
  assertTrue(!messenger.toString().equals(calc.toString()));
  String desiredMessage = "Hello World!";
  assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
  assertTrue(ctx.getBeansOfType(Calculator.class).values().contains(calc));
  assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
}

相关文章