Java Code Examples for org.springframework.beans.factory.BeanFactory#getType()

The following examples show how to use org.springframework.beans.factory.BeanFactory#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HandlerMethod.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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();
}
 
Example 2
Source File: MethodLocatingFactoryBean.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@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 + "]");
	}
}
 
Example 3
Source File: MethodLocatingFactoryBean.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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 + "]");
	}
}
 
Example 4
Source File: HandlerMethod.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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();
	this.description = initDescription(this.beanType, this.method);
}
 
Example 5
Source File: HandlerMethod.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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();
}
 
Example 6
Source File: MethodLocatingFactoryBean.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 + "]");
	}
}
 
Example 7
Source File: MethodLocatingFactoryBean.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 + "]");
	}
}
 
Example 8
Source File: SpringBeanELResolver.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
@Override
public Class<?> getType(ELContext elContext, Object base, Object property) throws ELException {
	if (base == null) {
		String beanName = property.toString();
		BeanFactory bf = getBeanFactory(elContext);
		if (bf.containsBean(beanName)) {
			elContext.setPropertyResolved(true);
			return bf.getType(beanName);
		}
	}
	return null;
}
 
Example 9
Source File: ScopedProxyFactoryBean.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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);

	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());
}
 
Example 10
Source File: CompositeBeanFactory.java    From rice with Educational Community License v2.0 5 votes vote down vote up
@Override
public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
	for (BeanFactory f : factories) {
		try {
			Class<?> c = f.getType(name);
			if (c != null) {
				return c;
			}	
		} catch (BeansException e) {
			LOG.info("bean exception", e);
		}
	}
	return null;
}
 
Example 11
Source File: ScopedProxyFactoryBean.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);

	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());
}
 
Example 12
Source File: BeanFactoryAspectInstanceFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example 13
Source File: ScopedProxyFactoryBean.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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());
}
 
Example 14
Source File: BeanFactoryAspectInstanceFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example 15
Source File: AbstractBeanFactory.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
@Nullable
public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
	String beanName = transformedBeanName(name);

	// Check manually registered singletons.
	Object beanInstance = getSingleton(beanName, false);
	if (beanInstance != null && beanInstance.getClass() != NullBean.class) {
		if (beanInstance instanceof FactoryBean && !BeanFactoryUtils.isFactoryDereference(name)) {
			return getTypeForFactoryBean((FactoryBean<?>) beanInstance);
		}
		else {
			return beanInstance.getClass();
		}
	}

	// No singleton instance found -> check bean definition.
	BeanFactory parentBeanFactory = getParentBeanFactory();
	if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
		// No bean definition found in this factory -> delegate to parent.
		return parentBeanFactory.getType(originalBeanName(name));
	}

	RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);

	// Check decorated bean definition, if any: We assume it'll be easier
	// to determine the decorated bean's type than the proxy's type.
	BeanDefinitionHolder dbd = mbd.getDecoratedDefinition();
	if (dbd != null && !BeanFactoryUtils.isFactoryDereference(name)) {
		RootBeanDefinition tbd = getMergedBeanDefinition(dbd.getBeanName(), dbd.getBeanDefinition(), mbd);
		Class<?> targetClass = predictBeanType(dbd.getBeanName(), tbd);
		if (targetClass != null && !FactoryBean.class.isAssignableFrom(targetClass)) {
			return targetClass;
		}
	}

	Class<?> beanClass = predictBeanType(beanName, mbd);

	// Check bean class whether we're dealing with a FactoryBean.
	if (beanClass != null && FactoryBean.class.isAssignableFrom(beanClass)) {
		if (!BeanFactoryUtils.isFactoryDereference(name)) {
			// If it's a FactoryBean, we want to look at what it creates, not at the factory class.
			return getTypeForFactoryBean(beanName, mbd);
		}
		else {
			return beanClass;
		}
	}
	else {
		return (!BeanFactoryUtils.isFactoryDereference(name) ? beanClass : null);
	}
}
 
Example 16
Source File: AbstractBeanFactory.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
@Nullable
public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
	String beanName = transformedBeanName(name);

	// Check manually registered singletons.
	Object beanInstance = getSingleton(beanName, false);
	if (beanInstance != null && beanInstance.getClass() != NullBean.class) {
		if (beanInstance instanceof FactoryBean && !BeanFactoryUtils.isFactoryDereference(name)) {
			return getTypeForFactoryBean((FactoryBean<?>) beanInstance);
		}
		else {
			return beanInstance.getClass();
		}
	}

	// No singleton instance found -> check bean definition.
	BeanFactory parentBeanFactory = getParentBeanFactory();
	if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
		// No bean definition found in this factory -> delegate to parent.
		return parentBeanFactory.getType(originalBeanName(name));
	}

	RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);

	// Check decorated bean definition, if any: We assume it'll be easier
	// to determine the decorated bean's type than the proxy's type.
	BeanDefinitionHolder dbd = mbd.getDecoratedDefinition();
	if (dbd != null && !BeanFactoryUtils.isFactoryDereference(name)) {
		RootBeanDefinition tbd = getMergedBeanDefinition(dbd.getBeanName(), dbd.getBeanDefinition(), mbd);
		Class<?> targetClass = predictBeanType(dbd.getBeanName(), tbd);
		if (targetClass != null && !FactoryBean.class.isAssignableFrom(targetClass)) {
			return targetClass;
		}
	}

	Class<?> beanClass = predictBeanType(beanName, mbd);

	// Check bean class whether we're dealing with a FactoryBean.
	if (beanClass != null && FactoryBean.class.isAssignableFrom(beanClass)) {
		if (!BeanFactoryUtils.isFactoryDereference(name)) {
			// If it's a FactoryBean, we want to look at what it creates, not at the factory class.
			return getTypeForFactoryBean(beanName, mbd);
		}
		else {
			return beanClass;
		}
	}
	else {
		return (!BeanFactoryUtils.isFactoryDereference(name) ? beanClass : null);
	}
}
 
Example 17
Source File: AbstractBeanFactory.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
	String beanName = transformedBeanName(name);

	// Check manually registered singletons.
	Object beanInstance = getSingleton(beanName, false);
	if (beanInstance != null) {
		if (beanInstance instanceof FactoryBean && !BeanFactoryUtils.isFactoryDereference(name)) {
			return getTypeForFactoryBean((FactoryBean<?>) beanInstance);
		}
		else {
			return beanInstance.getClass();
		}
	}
	else if (containsSingleton(beanName) && !containsBeanDefinition(beanName)) {
		// null instance registered
		return null;
	}

	else {
		// No singleton instance found -> check bean definition.
		BeanFactory parentBeanFactory = getParentBeanFactory();
		if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
			// No bean definition found in this factory -> delegate to parent.
			return parentBeanFactory.getType(originalBeanName(name));
		}

		RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);

		// Check decorated bean definition, if any: We assume it'll be easier
		// to determine the decorated bean's type than the proxy's type.
		BeanDefinitionHolder dbd = mbd.getDecoratedDefinition();
		if (dbd != null && !BeanFactoryUtils.isFactoryDereference(name)) {
			RootBeanDefinition tbd = getMergedBeanDefinition(dbd.getBeanName(), dbd.getBeanDefinition(), mbd);
			Class<?> targetClass = predictBeanType(dbd.getBeanName(), tbd);
			if (targetClass != null && !FactoryBean.class.isAssignableFrom(targetClass)) {
				return targetClass;
			}
		}

		Class<?> beanClass = predictBeanType(beanName, mbd);

		// Check bean class whether we're dealing with a FactoryBean.
		if (beanClass != null && FactoryBean.class.isAssignableFrom(beanClass)) {
			if (!BeanFactoryUtils.isFactoryDereference(name)) {
				// If it's a FactoryBean, we want to look at what it creates, not at the factory class.
				return getTypeForFactoryBean(beanName, mbd);
			}
			else {
				return beanClass;
			}
		}
		else {
			return (!BeanFactoryUtils.isFactoryDereference(name) ? beanClass : null);
		}
	}
}
 
Example 18
Source File: DefaultLifecycleProcessor.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private boolean matchesBeanType(Class<?> targetType, String beanName, BeanFactory beanFactory) {
	Class<?> beanType = beanFactory.getType(beanName);
	return (beanType != null && targetType.isAssignableFrom(beanType));
}
 
Example 19
Source File: BeanFactoryAspectInstanceFactory.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Create a BeanFactoryAspectInstanceFactory. AspectJ will be called to
 * introspect to create AJType metadata using the type returned for the
 * given bean name from the BeanFactory.
 * @param beanFactory BeanFactory to obtain instance(s) from
 * @param name name of the bean
 */
public BeanFactoryAspectInstanceFactory(BeanFactory beanFactory, String name) {
	this(beanFactory, name, beanFactory.getType(name));
}
 
Example 20
Source File: BeanFactoryAspectInstanceFactory.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Create a BeanFactoryAspectInstanceFactory. AspectJ will be called to
 * introspect to create AJType metadata using the type returned for the
 * given bean name from the BeanFactory.
 * @param beanFactory BeanFactory to obtain instance(s) from
 * @param name name of the bean
 */
public BeanFactoryAspectInstanceFactory(BeanFactory beanFactory, String name) {
	this(beanFactory, name, beanFactory.getType(name));
}