org.springframework.beans.factory.BeanFactory.getType()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(13.0k)|赞(0)|评价(0)|浏览(71)

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

BeanFactory.getType介绍

[英]Determine the type of the bean with the given name. More specifically, determine the type of object that #getBean would return for the given name.

For a FactoryBean, return the type of object that the FactoryBean creates, as exposed by FactoryBean#getObjectType().

Translates aliases back to the corresponding canonical bean name. Will ask the parent factory if the bean cannot be found in this factory instance.
[中]确定具有给定名称的bean的类型。更具体地说,确定#getBean将为给定名称返回的对象类型。
对于FactoryBean,返回由FactoryBean#getObjectType()公开的FactoryBean创建的对象类型。
将别名转换回相应的规范bean名称。将询问父工厂是否在此工厂实例中找不到该bean。

代码示例

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

private boolean matchesBeanType(Class<?> targetType, String beanName, BeanFactory beanFactory) {
  Class<?> beanType = beanFactory.getType(beanName);
  return (beanType != null && targetType.isAssignableFrom(beanType));
}

代码示例来源:origin: org.springframework/spring-context

private boolean matchesBeanType(Class<?> targetType, String beanName, BeanFactory beanFactory) {
  Class<?> beanType = beanFactory.getType(beanName);
  return (beanType != null && targetType.isAssignableFrom(beanType));
}

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

/**
 * Overridden to support the {@link #setTargetBeanName "targetBeanName"} feature.
 */
@Override
public Class<?> getTargetClass() {
  Class<?> targetClass = super.getTargetClass();
  if (targetClass == null && this.targetBeanName != null) {
    Assert.state(this.beanFactory != null, "BeanFactory must be set when using 'targetBeanName'");
    targetClass = this.beanFactory.getType(this.targetBeanName);
  }
  return targetClass;
}

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

/**
 * Look at bean factory metadata to work out whether this bean name,
 * which concludes the interceptorNames list, is an Advisor or Advice,
 * or may be a target.
 * @param beanName bean name to check
 * @return {@code true} if it's an Advisor or Advice
 */
private boolean isNamedBeanAnAdvisorOrAdvice(String beanName) {
  Assert.state(this.beanFactory != null, "No BeanFactory set");
  Class<?> namedBeanClass = this.beanFactory.getType(beanName);
  if (namedBeanClass != null) {
    return (Advisor.class.isAssignableFrom(namedBeanClass) || Advice.class.isAssignableFrom(namedBeanClass));
  }
  // Treat it as an target bean if we can't tell.
  if (logger.isDebugEnabled()) {
    logger.debug("Could not determine type of bean with name '" + beanName +
        "' - assuming it is neither an Advisor nor an Advice");
  }
  return false;
}

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

@Override
public Class<?> getTargetClass() {
  Class<?> targetClass = this.targetClass;
  if (targetClass != null) {
    return targetClass;
  }
  synchronized (this) {
    // Full check within synchronization, entering the BeanFactory interaction algorithm only once...
    targetClass = this.targetClass;
    if (targetClass == null && this.beanFactory != null) {
      // Determine type of the target bean.
      targetClass = this.beanFactory.getType(this.targetBeanName);
      if (targetClass == null) {
        if (logger.isTraceEnabled()) {
          logger.trace("Getting bean with name '" + this.targetBeanName + "' for type determination");
        }
        Object beanInstance = this.beanFactory.getBean(this.targetBeanName);
        targetClass = beanInstance.getClass();
      }
      this.targetClass = targetClass;
    }
    return targetClass;
  }
}

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

@Override
public void setBeanFactory(BeanFactory beanFactory) {
  if (!StringUtils.hasText(this.targetBeanName)) {
    throw new IllegalArgumentException("Property 'targetBeanName' is required");
  }
  if (!StringUtils.hasText(this.methodName)) {
    throw new IllegalArgumentException("Property 'methodName' is required");
  }
  Class<?> beanClass = beanFactory.getType(this.targetBeanName);
  if (beanClass == null) {
    throw new IllegalArgumentException("Can't determine type of bean with name '" + this.targetBeanName + "'");
  }
  this.method = BeanUtils.resolveSignature(this.methodName, beanClass);
  if (this.method == null) {
    throw new IllegalArgumentException("Unable to locate method [" + this.methodName +
        "] on bean [" + this.targetBeanName + "]");
  }
}

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

/**
 * Return the type of the contained bean.
 * <p>If the bean type is a CGLIB-generated class, the original
 * user-defined class is returned.
 */
@Nullable
public Class<?> getBeanType() {
  Class<?> beanType = (this.bean instanceof String ?
      obtainBeanFactory().getType((String) this.bean) : this.bean.getClass());
  return (beanType != null ? ClassUtils.getUserClass(beanType) : null);
}

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

/**
 * Create a BeanFactoryAspectInstanceFactory, providing a type that AspectJ should
 * introspect to create AJType metadata. Use if the BeanFactory may consider the type
 * to be a subclass (as when using CGLIB), and the information should relate to a superclass.
 * @param beanFactory the BeanFactory to obtain instance(s) from
 * @param name the name of the bean
 * @param type the type that should be introspected by AspectJ
 * ({@code null} indicates resolution through {@link BeanFactory#getType} via the bean name)
 */
public BeanFactoryAspectInstanceFactory(BeanFactory beanFactory, String name, @Nullable Class<?> type) {
  Assert.notNull(beanFactory, "BeanFactory must not be null");
  Assert.notNull(name, "Bean name must not be null");
  this.beanFactory = beanFactory;
  this.name = name;
  Class<?> resolvedType = type;
  if (type == null) {
    resolvedType = beanFactory.getType(name);
    Assert.notNull(resolvedType, "Unresolvable bean type - explicitly specify the aspect class");
  }
  this.aspectMetadata = new AspectMetadata(resolvedType, name);
}

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

/**
 * Create an instance from a bean name, a method, and a {@code BeanFactory}.
 * The method {@link #createWithResolvedBean()} may be used later to
 * re-create the {@code HandlerMethod} with an initialized bean.
 */
public HandlerMethod(String beanName, BeanFactory beanFactory, Method method) {
  Assert.hasText(beanName, "Bean name is required");
  Assert.notNull(beanFactory, "BeanFactory is required");
  Assert.notNull(method, "Method is required");
  this.bean = beanName;
  this.beanFactory = beanFactory;
  Class<?> beanType = beanFactory.getType(beanName);
  if (beanType == null) {
    throw new IllegalStateException("Cannot resolve bean type for bean with name '" + beanName + "'");
  }
  this.beanType = ClassUtils.getUserClass(beanType);
  this.method = method;
  this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
  this.parameters = initMethodParameters();
}

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

/**
 * Determine the order for this factory's target aspect, either
 * an instance-specific order expressed through implementing the
 * {@link org.springframework.core.Ordered} interface (only
 * checked for singleton beans), or an order expressed through the
 * {@link org.springframework.core.annotation.Order} annotation
 * at the class level.
 * @see org.springframework.core.Ordered
 * @see org.springframework.core.annotation.Order
 */
@Override
public int getOrder() {
  Class<?> type = this.beanFactory.getType(this.name);
  if (type != null) {
    if (Ordered.class.isAssignableFrom(type) && this.beanFactory.isSingleton(this.name)) {
      return ((Ordered) this.beanFactory.getBean(this.name)).getOrder();
    }
    return OrderUtils.getOrder(type, Ordered.LOWEST_PRECEDENCE);
  }
  return Ordered.LOWEST_PRECEDENCE;
}

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

/**
 * Create an instance from a bean name, a method, and a {@code BeanFactory}.
 * The method {@link #createWithResolvedBean()} may be used later to
 * re-create the {@code HandlerMethod} with an initialized bean.
 */
public HandlerMethod(String beanName, BeanFactory beanFactory, Method method) {
  Assert.hasText(beanName, "Bean name is required");
  Assert.notNull(beanFactory, "BeanFactory is required");
  Assert.notNull(method, "Method is required");
  this.bean = beanName;
  this.beanFactory = beanFactory;
  Class<?> beanType = beanFactory.getType(beanName);
  if (beanType == null) {
    throw new IllegalStateException("Cannot resolve bean type for bean with name '" + beanName + "'");
  }
  this.beanType = ClassUtils.getUserClass(beanType);
  this.method = method;
  this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
  this.parameters = initMethodParameters();
  evaluateResponseStatus();
}

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

/**
 * Return the type of the proxy. Will check the singleton instance if
 * already created, else fall back to the proxy interface (in case of just
 * a single one), the target bean type, or the TargetSource's target class.
 * @see org.springframework.aop.TargetSource#getTargetClass
 */
@Override
public Class<?> getObjectType() {
  synchronized (this) {
    if (this.singletonInstance != null) {
      return this.singletonInstance.getClass();
    }
  }
  Class<?>[] ifcs = getProxiedInterfaces();
  if (ifcs.length == 1) {
    return ifcs[0];
  }
  else if (ifcs.length > 1) {
    return createCompositeInterface(ifcs);
  }
  else if (this.targetName != null && this.beanFactory != null) {
    return this.beanFactory.getType(this.targetName);
  }
  else {
    return getTargetClass();
  }
}

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

@Test
public void testPrototypeInstancesAreNotEqual() {
  assertTrue("Has correct object type", ITestBean.class.isAssignableFrom(factory.getType("prototype")));
  ITestBean test2 = (ITestBean) factory.getBean("prototype");
  ITestBean test2_1 = (ITestBean) factory.getBean("prototype");
  assertTrue("Prototype instances !=", test2 != test2_1);
  assertTrue("Prototype instances equal", test2.equals(test2_1));
  assertTrue("Has correct object type", ITestBean.class.isAssignableFrom(factory.getType("prototype")));
}

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

@Override
public void setBeanFactory(BeanFactory beanFactory) {
  if (!(beanFactory instanceof ConfigurableBeanFactory)) {
    throw new IllegalStateException("Not running in a ConfigurableBeanFactory: " + beanFactory);
  }
  ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) beanFactory;
  this.scopedTargetSource.setBeanFactory(beanFactory);
  ProxyFactory pf = new ProxyFactory();
  pf.copyFrom(this);
  pf.setTargetSource(this.scopedTargetSource);
  Assert.notNull(this.targetBeanName, "Property 'targetBeanName' is required");
  Class<?> beanType = beanFactory.getType(this.targetBeanName);
  if (beanType == null) {
    throw new IllegalStateException("Cannot create scoped proxy for bean '" + this.targetBeanName +
        "': Target type could not be determined at the time of proxy creation.");
  }
  if (!isProxyTargetClass() || beanType.isInterface() || Modifier.isPrivate(beanType.getModifiers())) {
    pf.setInterfaces(ClassUtils.getAllInterfacesForClass(beanType, cbf.getBeanClassLoader()));
  }
  // Add an introduction that implements only the methods on ScopedObject.
  ScopedObject scopedObject = new DefaultScopedObject(cbf, this.scopedTargetSource.getTargetBeanName());
  pf.addAdvice(new DelegatingIntroductionInterceptor(scopedObject));
  // Add the AopInfrastructureBean marker to indicate that the scoped proxy
  // itself is not subject to auto-proxying! Only its target bean is.
  pf.addInterface(AopInfrastructureBean.class);
  this.proxy = pf.getProxy(cbf.getBeanClassLoader());
}

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

"] does not contain specified controller advice bean '" + beanName + "'");
beanType = this.beanFactory.getType(beanName);
this.order = initOrderFromBeanType(beanType);

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

@Test
public void configWithObjectReturnType() {
  BeanFactory factory = initBeanFactory(ConfigWithNonSpecificReturnTypes.class);
  assertEquals(Object.class, factory.getType("stringBean"));
  assertFalse(factory.isTypeMatch("stringBean", String.class));
  String stringBean = factory.getBean("stringBean", String.class);
  assertEquals("foo", stringBean);
}

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

ProxyFactoryBean config = (ProxyFactoryBean) factory.getBean("&test1");
assertTrue("Has correct object type", ITestBean.class.isAssignableFrom(config.getObjectType()));
assertTrue("Has correct object type", ITestBean.class.isAssignableFrom(factory.getType("test1")));
assertTrue("Has correct object type", ITestBean.class.isAssignableFrom(factory.getType("test1")));

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

@Test(expected = IllegalArgumentException.class)
public void testWhenTargetBeanClassCannotBeResolved() {
  factory.setTargetBeanName(BEAN_NAME);
  factory.setMethodName("toString()");
  factory.setBeanFactory(beanFactory);
  verify(beanFactory).getType(BEAN_NAME);
}

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

@Test(expected = IllegalArgumentException.class)
@SuppressWarnings("unchecked")
public void testWhereMethodCannotBeResolved() {
  given(beanFactory.getType(BEAN_NAME)).willReturn((Class)String.class);
  factory.setTargetBeanName(BEAN_NAME);
  factory.setMethodName("loadOfOld()");
  factory.setBeanFactory(beanFactory);
}

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

@Test
@SuppressWarnings("unchecked")
public void testSunnyDayPath() throws Exception {
  given(beanFactory.getType(BEAN_NAME)).willReturn((Class)String.class);
  factory.setTargetBeanName(BEAN_NAME);
  factory.setMethodName("toString()");
  factory.setBeanFactory(beanFactory);
  Object result = factory.getObject();
  assertNotNull(result);
  assertTrue(result instanceof Method);
  Method method = (Method) result;
  assertEquals("Bingo", method.invoke("Bingo"));
}

相关文章