Java Code Examples for org.springframework.beans.factory.BeanFactory#FACTORY_BEAN_PREFIX

The following examples show how to use org.springframework.beans.factory.BeanFactory#FACTORY_BEAN_PREFIX . 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: DeprecatedBeanWarner.java    From blog_demos with Apache License 2.0 6 votes vote down vote up
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
	if (isLogEnabled()) {
		String[] beanNames = beanFactory.getBeanDefinitionNames();
		for (String beanName : beanNames) {
			String nameToLookup = beanName;
			if (beanFactory.isFactoryBean(beanName)) {
				nameToLookup = BeanFactory.FACTORY_BEAN_PREFIX + beanName;
			}
			Class<?> beanType = ClassUtils.getUserClass(beanFactory.getType(nameToLookup));
			if (beanType != null && beanType.isAnnotationPresent(Deprecated.class)) {
				BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
				logDeprecatedBean(beanName, beanType, beanDefinition);
			}
		}
	}
}
 
Example 2
Source File: DeprecatedBeanWarner.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
	if (isLogEnabled()) {
		String[] beanNames = beanFactory.getBeanDefinitionNames();
		for (String beanName : beanNames) {
			String nameToLookup = beanName;
			if (beanFactory.isFactoryBean(beanName)) {
				nameToLookup = BeanFactory.FACTORY_BEAN_PREFIX + beanName;
			}
			Class<?> beanType = ClassUtils.getUserClass(beanFactory.getType(nameToLookup));
			if (beanType != null && beanType.isAnnotationPresent(Deprecated.class)) {
				BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
				logDeprecatedBean(beanName, beanType, beanDefinition);
			}
		}
	}
}
 
Example 3
Source File: DefaultLifecycleProcessor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Retrieve all applicable Lifecycle beans: all singletons that have already been created,
 * as well as all SmartLifecycle beans (even if they are marked as lazy-init).
 * @return the Map of applicable beans, with bean names as keys and bean instances as values
 */
protected Map<String, Lifecycle> getLifecycleBeans() {
	ConfigurableListableBeanFactory beanFactory = getBeanFactory();
	Map<String, Lifecycle> beans = new LinkedHashMap<>();
	String[] beanNames = beanFactory.getBeanNamesForType(Lifecycle.class, false, false);
	for (String beanName : beanNames) {
		String beanNameToRegister = BeanFactoryUtils.transformedBeanName(beanName);
		boolean isFactoryBean = beanFactory.isFactoryBean(beanNameToRegister);
		String beanNameToCheck = (isFactoryBean ? BeanFactory.FACTORY_BEAN_PREFIX + beanName : beanName);
		if ((beanFactory.containsSingleton(beanNameToRegister) &&
				(!isFactoryBean || matchesBeanType(Lifecycle.class, beanNameToCheck, beanFactory))) ||
				matchesBeanType(SmartLifecycle.class, beanNameToCheck, beanFactory)) {
			Object bean = beanFactory.getBean(beanNameToCheck);
			if (bean != this && bean instanceof Lifecycle) {
				beans.put(beanNameToRegister, (Lifecycle) bean);
			}
		}
	}
	return beans;
}
 
Example 4
Source File: DeprecatedBeanWarner.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
	if (isLogEnabled()) {
		String[] beanNames = beanFactory.getBeanDefinitionNames();
		for (String beanName : beanNames) {
			String nameToLookup = beanName;
			if (beanFactory.isFactoryBean(beanName)) {
				nameToLookup = BeanFactory.FACTORY_BEAN_PREFIX + beanName;
			}
			Class<?> beanType = beanFactory.getType(nameToLookup);
			if (beanType != null) {
				Class<?> userClass = ClassUtils.getUserClass(beanType);
				if (userClass.isAnnotationPresent(Deprecated.class)) {
					BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
					logDeprecatedBean(beanName, beanType, beanDefinition);
				}
			}
		}
	}
}
 
Example 5
Source File: DefaultLifecycleProcessor.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Retrieve all applicable Lifecycle beans: all singletons that have already been created,
 * as well as all SmartLifecycle beans (even if they are marked as lazy-init).
 * @return the Map of applicable beans, with bean names as keys and bean instances as values
 */
protected Map<String, Lifecycle> getLifecycleBeans() {
	ConfigurableListableBeanFactory beanFactory = getBeanFactory();
	Map<String, Lifecycle> beans = new LinkedHashMap<>();
	String[] beanNames = beanFactory.getBeanNamesForType(Lifecycle.class, false, false);
	for (String beanName : beanNames) {
		String beanNameToRegister = BeanFactoryUtils.transformedBeanName(beanName);
		boolean isFactoryBean = beanFactory.isFactoryBean(beanNameToRegister);
		String beanNameToCheck = (isFactoryBean ? BeanFactory.FACTORY_BEAN_PREFIX + beanName : beanName);
		if ((beanFactory.containsSingleton(beanNameToRegister) &&
				(!isFactoryBean || matchesBeanType(Lifecycle.class, beanNameToCheck, beanFactory))) ||
				matchesBeanType(SmartLifecycle.class, beanNameToCheck, beanFactory)) {
			Object bean = beanFactory.getBean(beanNameToCheck);
			if (bean != this && bean instanceof Lifecycle) {
				beans.put(beanNameToRegister, (Lifecycle) bean);
			}
		}
	}
	return beans;
}
 
Example 6
Source File: DeprecatedBeanWarner.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
	if (isLogEnabled()) {
		String[] beanNames = beanFactory.getBeanDefinitionNames();
		for (String beanName : beanNames) {
			String nameToLookup = beanName;
			if (beanFactory.isFactoryBean(beanName)) {
				nameToLookup = BeanFactory.FACTORY_BEAN_PREFIX + beanName;
			}
			Class<?> beanType = beanFactory.getType(nameToLookup);
			if (beanType != null) {
				Class<?> userClass = ClassUtils.getUserClass(beanType);
				if (userClass.isAnnotationPresent(Deprecated.class)) {
					BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
					logDeprecatedBean(beanName, beanType, beanDefinition);
				}
			}
		}
	}
}
 
Example 7
Source File: RefreshableScriptTargetSource.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Obtain a fresh target object, retrieving a FactoryBean if necessary.
 */
@Override
protected Object obtainFreshBean(BeanFactory beanFactory, String beanName) {
	return super.obtainFreshBean(beanFactory,
			(this.isFactoryBean ? BeanFactory.FACTORY_BEAN_PREFIX + beanName : beanName));
}
 
Example 8
Source File: RefreshableScriptTargetSource.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Obtain a fresh target object, retrieving a FactoryBean if necessary.
 */
@Override
protected Object obtainFreshBean(BeanFactory beanFactory, String beanName) {
	return super.obtainFreshBean(beanFactory,
			(this.isFactoryBean ? BeanFactory.FACTORY_BEAN_PREFIX + beanName : beanName));
}
 
Example 9
Source File: AbstractEntityManagerFactoryBean.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public SerializedEntityManagerFactoryBeanReference(BeanFactory beanFactory, String beanName) {
	this.beanFactory = beanFactory;
	this.lookupName = BeanFactory.FACTORY_BEAN_PREFIX + beanName;
}
 
Example 10
Source File: ScriptFactoryPostProcessor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
	// We only apply special treatment to ScriptFactory implementations here.
	if (!ScriptFactory.class.isAssignableFrom(beanClass)) {
		return null;
	}

	BeanDefinition bd = this.beanFactory.getMergedBeanDefinition(beanName);
	String scriptFactoryBeanName = SCRIPT_FACTORY_NAME_PREFIX + beanName;
	String scriptedObjectBeanName = SCRIPTED_OBJECT_NAME_PREFIX + beanName;
	prepareScriptBeans(bd, scriptFactoryBeanName, scriptedObjectBeanName);

	ScriptFactory scriptFactory = this.scriptBeanFactory.getBean(scriptFactoryBeanName, ScriptFactory.class);
	ScriptSource scriptSource = getScriptSource(scriptFactoryBeanName, scriptFactory.getScriptSourceLocator());
	boolean isFactoryBean = false;
	try {
		Class<?> scriptedObjectType = scriptFactory.getScriptedObjectType(scriptSource);
		// Returned type may be null if the factory is unable to determine the type.
		if (scriptedObjectType != null) {
			isFactoryBean = FactoryBean.class.isAssignableFrom(scriptedObjectType);
		}
	}
	catch (Exception ex) {
		throw new BeanCreationException(beanName,
				"Could not determine scripted object type for " + scriptFactory, ex);
	}

	long refreshCheckDelay = resolveRefreshCheckDelay(bd);
	if (refreshCheckDelay >= 0) {
		Class<?>[] interfaces = scriptFactory.getScriptInterfaces();
		RefreshableScriptTargetSource ts = new RefreshableScriptTargetSource(this.scriptBeanFactory,
				scriptedObjectBeanName, scriptFactory, scriptSource, isFactoryBean);
		boolean proxyTargetClass = resolveProxyTargetClass(bd);
		String language = (String) bd.getAttribute(LANGUAGE_ATTRIBUTE);
		if (proxyTargetClass && (language == null || !language.equals("groovy"))) {
			throw new BeanDefinitionValidationException(
					"Cannot use proxyTargetClass=true with script beans where language is not 'groovy': '" +
					language + "'");
		}
		ts.setRefreshCheckDelay(refreshCheckDelay);
		return createRefreshableProxy(ts, interfaces, proxyTargetClass);
	}

	if (isFactoryBean) {
		scriptedObjectBeanName = BeanFactory.FACTORY_BEAN_PREFIX + scriptedObjectBeanName;
	}
	return this.scriptBeanFactory.getBean(scriptedObjectBeanName);
}
 
Example 11
Source File: RefreshableScriptTargetSource.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Obtain a fresh target object, retrieving a FactoryBean if necessary.
 */
@Override
protected Object obtainFreshBean(BeanFactory beanFactory, String beanName) {
	return super.obtainFreshBean(beanFactory,
			(this.isFactoryBean ? BeanFactory.FACTORY_BEAN_PREFIX + beanName : beanName));
}
 
Example 12
Source File: AbstractEntityManagerFactoryBean.java    From java-technology-stack with MIT License 4 votes vote down vote up
public SerializedEntityManagerFactoryBeanReference(BeanFactory beanFactory, String beanName) {
	this.beanFactory = beanFactory;
	this.lookupName = BeanFactory.FACTORY_BEAN_PREFIX + beanName;
}
 
Example 13
Source File: ScriptFactoryPostProcessor.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
	// We only apply special treatment to ScriptFactory implementations here.
	if (!ScriptFactory.class.isAssignableFrom(beanClass)) {
		return null;
	}

	Assert.state(this.beanFactory != null, "No BeanFactory set");
	BeanDefinition bd = this.beanFactory.getMergedBeanDefinition(beanName);
	String scriptFactoryBeanName = SCRIPT_FACTORY_NAME_PREFIX + beanName;
	String scriptedObjectBeanName = SCRIPTED_OBJECT_NAME_PREFIX + beanName;
	prepareScriptBeans(bd, scriptFactoryBeanName, scriptedObjectBeanName);

	ScriptFactory scriptFactory = this.scriptBeanFactory.getBean(scriptFactoryBeanName, ScriptFactory.class);
	ScriptSource scriptSource = getScriptSource(scriptFactoryBeanName, scriptFactory.getScriptSourceLocator());
	boolean isFactoryBean = false;
	try {
		Class<?> scriptedObjectType = scriptFactory.getScriptedObjectType(scriptSource);
		// Returned type may be null if the factory is unable to determine the type.
		if (scriptedObjectType != null) {
			isFactoryBean = FactoryBean.class.isAssignableFrom(scriptedObjectType);
		}
	}
	catch (Exception ex) {
		throw new BeanCreationException(beanName,
				"Could not determine scripted object type for " + scriptFactory, ex);
	}

	long refreshCheckDelay = resolveRefreshCheckDelay(bd);
	if (refreshCheckDelay >= 0) {
		Class<?>[] interfaces = scriptFactory.getScriptInterfaces();
		RefreshableScriptTargetSource ts = new RefreshableScriptTargetSource(this.scriptBeanFactory,
				scriptedObjectBeanName, scriptFactory, scriptSource, isFactoryBean);
		boolean proxyTargetClass = resolveProxyTargetClass(bd);
		String language = (String) bd.getAttribute(LANGUAGE_ATTRIBUTE);
		if (proxyTargetClass && (language == null || !language.equals("groovy"))) {
			throw new BeanDefinitionValidationException(
					"Cannot use proxyTargetClass=true with script beans where language is not 'groovy': '" +
					language + "'");
		}
		ts.setRefreshCheckDelay(refreshCheckDelay);
		return createRefreshableProxy(ts, interfaces, proxyTargetClass);
	}

	if (isFactoryBean) {
		scriptedObjectBeanName = BeanFactory.FACTORY_BEAN_PREFIX + scriptedObjectBeanName;
	}
	return this.scriptBeanFactory.getBean(scriptedObjectBeanName);
}
 
Example 14
Source File: RefreshableScriptTargetSource.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Obtain a fresh target object, retrieving a FactoryBean if necessary.
 */
@Override
protected Object obtainFreshBean(BeanFactory beanFactory, String beanName) {
	return super.obtainFreshBean(beanFactory,
			(this.isFactoryBean ? BeanFactory.FACTORY_BEAN_PREFIX + beanName : beanName));
}
 
Example 15
Source File: ScriptFactoryPostProcessor.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
	// We only apply special treatment to ScriptFactory implementations here.
	if (!ScriptFactory.class.isAssignableFrom(beanClass)) {
		return null;
	}

	BeanDefinition bd = this.beanFactory.getMergedBeanDefinition(beanName);
	String scriptFactoryBeanName = SCRIPT_FACTORY_NAME_PREFIX + beanName;
	String scriptedObjectBeanName = SCRIPTED_OBJECT_NAME_PREFIX + beanName;
	prepareScriptBeans(bd, scriptFactoryBeanName, scriptedObjectBeanName);

	ScriptFactory scriptFactory = this.scriptBeanFactory.getBean(scriptFactoryBeanName, ScriptFactory.class);
	ScriptSource scriptSource = getScriptSource(scriptFactoryBeanName, scriptFactory.getScriptSourceLocator());
	boolean isFactoryBean = false;
	try {
		Class<?> scriptedObjectType = scriptFactory.getScriptedObjectType(scriptSource);
		// Returned type may be null if the factory is unable to determine the type.
		if (scriptedObjectType != null) {
			isFactoryBean = FactoryBean.class.isAssignableFrom(scriptedObjectType);
		}
	}
	catch (Exception ex) {
		throw new BeanCreationException(beanName,
				"Could not determine scripted object type for " + scriptFactory, ex);
	}

	long refreshCheckDelay = resolveRefreshCheckDelay(bd);
	if (refreshCheckDelay >= 0) {
		Class<?>[] interfaces = scriptFactory.getScriptInterfaces();
		RefreshableScriptTargetSource ts = new RefreshableScriptTargetSource(this.scriptBeanFactory,
				scriptedObjectBeanName, scriptFactory, scriptSource, isFactoryBean);
		boolean proxyTargetClass = resolveProxyTargetClass(bd);
		String language = (String) bd.getAttribute(LANGUAGE_ATTRIBUTE);
		if (proxyTargetClass && (language == null || !language.equals("groovy"))) {
			throw new BeanDefinitionValidationException(
					"Cannot use proxyTargetClass=true with script beans where language is not 'groovy': '" +
					language + "'");
		}
		ts.setRefreshCheckDelay(refreshCheckDelay);
		return createRefreshableProxy(ts, interfaces, proxyTargetClass);
	}

	if (isFactoryBean) {
		scriptedObjectBeanName = BeanFactory.FACTORY_BEAN_PREFIX + scriptedObjectBeanName;
	}
	return this.scriptBeanFactory.getBean(scriptedObjectBeanName);
}
 
Example 16
Source File: AbstractEntityManagerFactoryBean.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public SerializedEntityManagerFactoryBeanReference(BeanFactory beanFactory, String beanName) {
	this.beanFactory = beanFactory;
	this.lookupName = BeanFactory.FACTORY_BEAN_PREFIX + beanName;
}
 
Example 17
Source File: ScriptFactoryPostProcessor.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
	// We only apply special treatment to ScriptFactory implementations here.
	if (!ScriptFactory.class.isAssignableFrom(beanClass)) {
		return null;
	}

	Assert.state(this.beanFactory != null, "No BeanFactory set");
	BeanDefinition bd = this.beanFactory.getMergedBeanDefinition(beanName);
	String scriptFactoryBeanName = SCRIPT_FACTORY_NAME_PREFIX + beanName;
	String scriptedObjectBeanName = SCRIPTED_OBJECT_NAME_PREFIX + beanName;
	prepareScriptBeans(bd, scriptFactoryBeanName, scriptedObjectBeanName);

	ScriptFactory scriptFactory = this.scriptBeanFactory.getBean(scriptFactoryBeanName, ScriptFactory.class);
	ScriptSource scriptSource = getScriptSource(scriptFactoryBeanName, scriptFactory.getScriptSourceLocator());
	boolean isFactoryBean = false;
	try {
		Class<?> scriptedObjectType = scriptFactory.getScriptedObjectType(scriptSource);
		// Returned type may be null if the factory is unable to determine the type.
		if (scriptedObjectType != null) {
			isFactoryBean = FactoryBean.class.isAssignableFrom(scriptedObjectType);
		}
	}
	catch (Exception ex) {
		throw new BeanCreationException(beanName,
				"Could not determine scripted object type for " + scriptFactory, ex);
	}

	long refreshCheckDelay = resolveRefreshCheckDelay(bd);
	if (refreshCheckDelay >= 0) {
		Class<?>[] interfaces = scriptFactory.getScriptInterfaces();
		RefreshableScriptTargetSource ts = new RefreshableScriptTargetSource(this.scriptBeanFactory,
				scriptedObjectBeanName, scriptFactory, scriptSource, isFactoryBean);
		boolean proxyTargetClass = resolveProxyTargetClass(bd);
		String language = (String) bd.getAttribute(LANGUAGE_ATTRIBUTE);
		if (proxyTargetClass && (language == null || !language.equals("groovy"))) {
			throw new BeanDefinitionValidationException(
					"Cannot use proxyTargetClass=true with script beans where language is not 'groovy': '" +
					language + "'");
		}
		ts.setRefreshCheckDelay(refreshCheckDelay);
		return createRefreshableProxy(ts, interfaces, proxyTargetClass);
	}

	if (isFactoryBean) {
		scriptedObjectBeanName = BeanFactory.FACTORY_BEAN_PREFIX + scriptedObjectBeanName;
	}
	return this.scriptBeanFactory.getBean(scriptedObjectBeanName);
}
 
Example 18
Source File: AbstractAutoProxyCreator.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Build a cache key for the given bean class and bean name.
 * <p>Note: As of 4.2.3, this implementation does not return a concatenated
 * class/name String anymore but rather the most efficient cache key possible:
 * a plain bean name, prepended with {@link BeanFactory#FACTORY_BEAN_PREFIX}
 * in case of a {@code FactoryBean}; or if no bean name specified, then the
 * given bean {@code Class} as-is.
 * @param beanClass the bean class
 * @param beanName the bean name
 * @return the cache key for the given class and name
 */
protected Object getCacheKey(Class<?> beanClass, String beanName) {
	if (StringUtils.hasLength(beanName)) {
		return (FactoryBean.class.isAssignableFrom(beanClass) ?
				BeanFactory.FACTORY_BEAN_PREFIX + beanName : beanName);
	}
	else {
		return beanClass;
	}
}
 
Example 19
Source File: AbstractAutoProxyCreator.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Build a cache key for the given bean class and bean name.
 * <p>Note: As of 4.2.3, this implementation does not return a concatenated
 * class/name String anymore but rather the most efficient cache key possible:
 * a plain bean name, prepended with {@link BeanFactory#FACTORY_BEAN_PREFIX}
 * in case of a {@code FactoryBean}; or if no bean name specified, then the
 * given bean {@code Class} as-is.
 * @param beanClass the bean class
 * @param beanName the bean name
 * @return the cache key for the given class and name
 */
protected Object getCacheKey(Class<?> beanClass, @Nullable String beanName) {
	if (StringUtils.hasLength(beanName)) {
		return (FactoryBean.class.isAssignableFrom(beanClass) ?
				BeanFactory.FACTORY_BEAN_PREFIX + beanName : beanName);
	}
	else {
		return beanClass;
	}
}
 
Example 20
Source File: AbstractAutoProxyCreator.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * Build a cache key for the given bean class and bean name.
 * <p>Note: As of 4.2.3, this implementation does not return a concatenated
 * class/name String anymore but rather the most efficient cache key possible:
 * a plain bean name, prepended with {@link BeanFactory#FACTORY_BEAN_PREFIX}
 * in case of a {@code FactoryBean}; or if no bean name specified, then the
 * given bean {@code Class} as-is.
 * @param beanClass the bean class
 * @param beanName the bean name
 * @return the cache key for the given class and name
 */
protected Object getCacheKey(Class<?> beanClass, String beanName) {
	if (StringUtils.hasLength(beanName)) {
		return (FactoryBean.class.isAssignableFrom(beanClass) ?
				BeanFactory.FACTORY_BEAN_PREFIX + beanName : beanName);
	}
	else {
		return beanClass;
	}
}