Java Code Examples for org.springframework.beans.factory.BeanFactoryAware

The following examples show how to use org.springframework.beans.factory.BeanFactoryAware. These examples are extracted from open source projects. 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 Project: spring-analysis-note   Source File: ParserStrategyUtils.java    License: MIT License 6 votes vote down vote up
/**
 * Invoke {@link BeanClassLoaderAware}, {@link BeanFactoryAware},
 * {@link EnvironmentAware}, and {@link ResourceLoaderAware} contracts
 * if implemented by the given object.
 */
public static void invokeAwareMethods(Object parserStrategyBean, Environment environment,
		ResourceLoader resourceLoader, BeanDefinitionRegistry registry) {

	if (parserStrategyBean instanceof Aware) {
		if (parserStrategyBean instanceof BeanClassLoaderAware) {
			ClassLoader classLoader = (registry instanceof ConfigurableBeanFactory ?
					((ConfigurableBeanFactory) registry).getBeanClassLoader() : resourceLoader.getClassLoader());
			if (classLoader != null) {
				((BeanClassLoaderAware) parserStrategyBean).setBeanClassLoader(classLoader);
			}
		}
		if (parserStrategyBean instanceof BeanFactoryAware && registry instanceof BeanFactory) {
			((BeanFactoryAware) parserStrategyBean).setBeanFactory((BeanFactory) registry);
		}
		if (parserStrategyBean instanceof EnvironmentAware) {
			((EnvironmentAware) parserStrategyBean).setEnvironment(environment);
		}
		if (parserStrategyBean instanceof ResourceLoaderAware) {
			((ResourceLoaderAware) parserStrategyBean).setResourceLoader(resourceLoader);
		}
	}
}
 
Example 2
private void invokeAwareMethods(final String beanName, final Object bean) {
	if (bean instanceof Aware) {
		if (bean instanceof BeanNameAware) {
			((BeanNameAware) bean).setBeanName(beanName);
		}
		if (bean instanceof BeanClassLoaderAware) {
			ClassLoader bcl = getBeanClassLoader();
			if (bcl != null) {
				((BeanClassLoaderAware) bean).setBeanClassLoader(bcl);
			}
		}
		if (bean instanceof BeanFactoryAware) {
			((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
		}
	}
}
 
Example 3
Source Project: spring-analysis-note   Source File: DefaultListableBeanFactory.java    License: MIT License 6 votes vote down vote up
/**
 * Set a custom autowire candidate resolver for this BeanFactory to use
 * when deciding whether a bean definition should be considered as a
 * candidate for autowiring.
 */
public void setAutowireCandidateResolver(final AutowireCandidateResolver autowireCandidateResolver) {
	Assert.notNull(autowireCandidateResolver, "AutowireCandidateResolver must not be null");
	if (autowireCandidateResolver instanceof BeanFactoryAware) {
		if (System.getSecurityManager() != null) {
			AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
				((BeanFactoryAware) autowireCandidateResolver).setBeanFactory(DefaultListableBeanFactory.this);
				return null;
			}, getAccessControlContext());
		}
		else {
			((BeanFactoryAware) autowireCandidateResolver).setBeanFactory(this);
		}
	}
	this.autowireCandidateResolver = autowireCandidateResolver;
}
 
Example 4
Source Project: java-technology-stack   Source File: ParserStrategyUtils.java    License: MIT License 6 votes vote down vote up
/**
 * Invoke {@link BeanClassLoaderAware}, {@link BeanFactoryAware},
 * {@link EnvironmentAware}, and {@link ResourceLoaderAware} contracts
 * if implemented by the given object.
 */
public static void invokeAwareMethods(Object parserStrategyBean, Environment environment,
		ResourceLoader resourceLoader, BeanDefinitionRegistry registry) {

	if (parserStrategyBean instanceof Aware) {
		if (parserStrategyBean instanceof BeanClassLoaderAware) {
			ClassLoader classLoader = (registry instanceof ConfigurableBeanFactory ?
					((ConfigurableBeanFactory) registry).getBeanClassLoader() : resourceLoader.getClassLoader());
			if (classLoader != null) {
				((BeanClassLoaderAware) parserStrategyBean).setBeanClassLoader(classLoader);
			}
		}
		if (parserStrategyBean instanceof BeanFactoryAware && registry instanceof BeanFactory) {
			((BeanFactoryAware) parserStrategyBean).setBeanFactory((BeanFactory) registry);
		}
		if (parserStrategyBean instanceof EnvironmentAware) {
			((EnvironmentAware) parserStrategyBean).setEnvironment(environment);
		}
		if (parserStrategyBean instanceof ResourceLoaderAware) {
			((ResourceLoaderAware) parserStrategyBean).setResourceLoader(resourceLoader);
		}
	}
}
 
Example 5
private void invokeAwareMethods(final String beanName, final Object bean) {
	if (bean instanceof Aware) {
		if (bean instanceof BeanNameAware) {
			((BeanNameAware) bean).setBeanName(beanName);
		}
		if (bean instanceof BeanClassLoaderAware) {
			ClassLoader bcl = getBeanClassLoader();
			if (bcl != null) {
				((BeanClassLoaderAware) bean).setBeanClassLoader(bcl);
			}
		}
		if (bean instanceof BeanFactoryAware) {
			((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
		}
	}
}
 
Example 6
Source Project: java-technology-stack   Source File: DefaultListableBeanFactory.java    License: MIT License 6 votes vote down vote up
/**
 * Set a custom autowire candidate resolver for this BeanFactory to use
 * when deciding whether a bean definition should be considered as a
 * candidate for autowiring.
 */
public void setAutowireCandidateResolver(final AutowireCandidateResolver autowireCandidateResolver) {
	Assert.notNull(autowireCandidateResolver, "AutowireCandidateResolver must not be null");
	if (autowireCandidateResolver instanceof BeanFactoryAware) {
		if (System.getSecurityManager() != null) {
			AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
				((BeanFactoryAware) autowireCandidateResolver).setBeanFactory(DefaultListableBeanFactory.this);
				return null;
			}, getAccessControlContext());
		}
		else {
			((BeanFactoryAware) autowireCandidateResolver).setBeanFactory(this);
		}
	}
	this.autowireCandidateResolver = autowireCandidateResolver;
}
 
Example 7
Source Project: spring-init   Source File: FunctionalInstallerListener.java    License: Apache License 2.0 6 votes vote down vote up
public static void invokeAwareMethods(Object target, Environment environment, ResourceLoader resourceLoader,
		BeanDefinitionRegistry registry) {

	if (target instanceof Aware) {
		if (target instanceof BeanClassLoaderAware) {
			ClassLoader classLoader = (registry instanceof ConfigurableBeanFactory
					? ((ConfigurableBeanFactory) registry).getBeanClassLoader() : resourceLoader.getClassLoader());
			if (classLoader != null) {
				((BeanClassLoaderAware) target).setBeanClassLoader(classLoader);
			}
		}
		if (target instanceof BeanFactoryAware && registry instanceof BeanFactory) {
			((BeanFactoryAware) target).setBeanFactory((BeanFactory) registry);
		}
		if (target instanceof EnvironmentAware) {
			((EnvironmentAware) target).setEnvironment(environment);
		}
		if (target instanceof ResourceLoaderAware) {
			((ResourceLoaderAware) target).setResourceLoader(resourceLoader);
		}
	}
}
 
Example 8
Source Project: lams   Source File: ParserStrategyUtils.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Invoke {@link BeanClassLoaderAware}, {@link BeanFactoryAware},
 * {@link EnvironmentAware}, and {@link ResourceLoaderAware} contracts
 * if implemented by the given object.
 */
public static void invokeAwareMethods(Object parserStrategyBean, Environment environment,
		ResourceLoader resourceLoader, BeanDefinitionRegistry registry) {

	if (parserStrategyBean instanceof Aware) {
		if (parserStrategyBean instanceof BeanClassLoaderAware) {
			ClassLoader classLoader = (registry instanceof ConfigurableBeanFactory ?
					((ConfigurableBeanFactory) registry).getBeanClassLoader() : resourceLoader.getClassLoader());
			((BeanClassLoaderAware) parserStrategyBean).setBeanClassLoader(classLoader);
		}
		if (parserStrategyBean instanceof BeanFactoryAware && registry instanceof BeanFactory) {
			((BeanFactoryAware) parserStrategyBean).setBeanFactory((BeanFactory) registry);
		}
		if (parserStrategyBean instanceof EnvironmentAware) {
			((EnvironmentAware) parserStrategyBean).setEnvironment(environment);
		}
		if (parserStrategyBean instanceof ResourceLoaderAware) {
			((ResourceLoaderAware) parserStrategyBean).setResourceLoader(resourceLoader);
		}
	}
}
 
Example 9
/**
 * Set a custom autowire candidate resolver for this BeanFactory to use
 * when deciding whether a bean definition should be considered as a
 * candidate for autowiring.
 */
public void setAutowireCandidateResolver(final AutowireCandidateResolver autowireCandidateResolver) {
	Assert.notNull(autowireCandidateResolver, "AutowireCandidateResolver must not be null");
	if (autowireCandidateResolver instanceof BeanFactoryAware) {
		if (System.getSecurityManager() != null) {
			AccessController.doPrivileged(new PrivilegedAction<Object>() {
				@Override
				public Object run() {
					((BeanFactoryAware) autowireCandidateResolver).setBeanFactory(DefaultListableBeanFactory.this);
					return null;
				}
			}, getAccessControlContext());
		}
		else {
			((BeanFactoryAware) autowireCandidateResolver).setBeanFactory(this);
		}
	}
	this.autowireCandidateResolver = autowireCandidateResolver;
}
 
Example 10
Source Project: blog_demos   Source File: DefaultListableBeanFactory.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Set a custom autowire candidate resolver for this BeanFactory to use
 * when deciding whether a bean definition should be considered as a
 * candidate for autowiring.
 */
public void setAutowireCandidateResolver(final AutowireCandidateResolver autowireCandidateResolver) {
	Assert.notNull(autowireCandidateResolver, "AutowireCandidateResolver must not be null");
	if (autowireCandidateResolver instanceof BeanFactoryAware) {
		if (System.getSecurityManager() != null) {
			final BeanFactory target = this;
			AccessController.doPrivileged(new PrivilegedAction<Object>() {
				@Override
				public Object run() {
					((BeanFactoryAware) autowireCandidateResolver).setBeanFactory(target);
					return null;
				}
			}, getAccessControlContext());
		}
		else {
			((BeanFactoryAware) autowireCandidateResolver).setBeanFactory(this);
		}
	}
	this.autowireCandidateResolver = autowireCandidateResolver;
}
 
Example 11
/**
 * Invoke {@link ResourceLoaderAware}, {@link BeanClassLoaderAware} and
 * {@link BeanFactoryAware} contracts if implemented by the given {@code bean}.
 */
private void invokeAwareMethods(Object importStrategyBean) {
	if (importStrategyBean instanceof Aware) {
		if (importStrategyBean instanceof EnvironmentAware) {
			((EnvironmentAware) importStrategyBean).setEnvironment(this.environment);
		}
		if (importStrategyBean instanceof ResourceLoaderAware) {
			((ResourceLoaderAware) importStrategyBean).setResourceLoader(this.resourceLoader);
		}
		if (importStrategyBean instanceof BeanClassLoaderAware) {
			ClassLoader classLoader = (this.registry instanceof ConfigurableBeanFactory ?
					((ConfigurableBeanFactory) this.registry).getBeanClassLoader() :
					this.resourceLoader.getClassLoader());
			((BeanClassLoaderAware) importStrategyBean).setBeanClassLoader(classLoader);
		}
		if (importStrategyBean instanceof BeanFactoryAware && this.registry instanceof BeanFactory) {
			((BeanFactoryAware) importStrategyBean).setBeanFactory((BeanFactory) this.registry);
		}
	}
}
 
Example 12
/**
 * Set a custom autowire candidate resolver for this BeanFactory to use
 * when deciding whether a bean definition should be considered as a
 * candidate for autowiring.
 */
public void setAutowireCandidateResolver(final AutowireCandidateResolver autowireCandidateResolver) {
	Assert.notNull(autowireCandidateResolver, "AutowireCandidateResolver must not be null");
	if (autowireCandidateResolver instanceof BeanFactoryAware) {
		if (System.getSecurityManager() != null) {
			final BeanFactory target = this;
			AccessController.doPrivileged(new PrivilegedAction<Object>() {
				@Override
				public Object run() {
					((BeanFactoryAware) autowireCandidateResolver).setBeanFactory(target);
					return null;
				}
			}, getAccessControlContext());
		}
		else {
			((BeanFactoryAware) autowireCandidateResolver).setBeanFactory(this);
		}
	}
	this.autowireCandidateResolver = autowireCandidateResolver;
}
 
Example 13
/**
 * Set a custom autowire candidate resolver for this BeanFactory to use
 * when deciding whether a bean definition should be considered as a
 * candidate for autowiring.
 */
public void setAutowireCandidateResolver(final AutowireCandidateResolver autowireCandidateResolver) {
	Assert.notNull(autowireCandidateResolver, "AutowireCandidateResolver must not be null");
	if (autowireCandidateResolver instanceof BeanFactoryAware) {
		if (System.getSecurityManager() != null) {
			final BeanFactory target = this;
			AccessController.doPrivileged(new PrivilegedAction<Object>() {
				@Override
                   public Object run() {
					((BeanFactoryAware) autowireCandidateResolver).setBeanFactory(target);
					return null;
				}
			}, getAccessControlContext());
		}
		else {
			((BeanFactoryAware) autowireCandidateResolver).setBeanFactory(this);
		}
	}
	this.autowireCandidateResolver = autowireCandidateResolver;
}
 
Example 14
Source Project: beihu-boot   Source File: ApiBootDataSourceSwitchAdvisor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * init config
 */
public ApiBootDataSourceSwitchAdvisor(ApiBootDataSourceSwitchAnnotationInterceptor apiBootDataSourceSwitchAnnotationInterceptor) {
    // build pointcut instance
    this.pointcut = buildPointcut();
    // build advice instance
    this.advice = apiBootDataSourceSwitchAnnotationInterceptor;
    if (this.advice instanceof BeanFactoryAware) {
        ((BeanFactoryAware) this.advice).setBeanFactory(beanFactory);
    }
}
 
Example 15
Source Project: spring-analysis-note   Source File: AsyncAnnotationAdvisor.java    License: MIT License 5 votes vote down vote up
/**
 * Set the {@code BeanFactory} to be used when looking up executors by qualifier.
 */
@Override
public void setBeanFactory(BeanFactory beanFactory) {
	if (this.advice instanceof BeanFactoryAware) {
		((BeanFactoryAware) this.advice).setBeanFactory(beanFactory);
	}
}
 
Example 16
Source Project: spring-analysis-note   Source File: ConfigurationClassEnhancer.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
	Field field = ReflectionUtils.findField(obj.getClass(), BEAN_FACTORY_FIELD);
	Assert.state(field != null, "Unable to find generated BeanFactory field");
	field.set(obj, args[0]);

	// Does the actual (non-CGLIB) superclass implement BeanFactoryAware?
	// If so, call its setBeanFactory() method. If not, just exit.
	if (BeanFactoryAware.class.isAssignableFrom(ClassUtils.getUserClass(obj.getClass().getSuperclass()))) {
		return proxy.invokeSuper(obj, args);
	}
	return null;
}
 
Example 17
/**
 * Create a new AbstractAutowireCapableBeanFactory.
 */
public AbstractAutowireCapableBeanFactory() {
	super();
	ignoreDependencyInterface(BeanNameAware.class);
	ignoreDependencyInterface(BeanFactoryAware.class);
	ignoreDependencyInterface(BeanClassLoaderAware.class);
}
 
Example 18
Source Project: java-technology-stack   Source File: AsyncAnnotationAdvisor.java    License: MIT License 5 votes vote down vote up
/**
 * Set the {@code BeanFactory} to be used when looking up executors by qualifier.
 */
@Override
public void setBeanFactory(BeanFactory beanFactory) {
	if (this.advice instanceof BeanFactoryAware) {
		((BeanFactoryAware) this.advice).setBeanFactory(beanFactory);
	}
}
 
Example 19
Source Project: java-technology-stack   Source File: ConfigurationClassEnhancer.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
	Field field = ReflectionUtils.findField(obj.getClass(), BEAN_FACTORY_FIELD);
	Assert.state(field != null, "Unable to find generated BeanFactory field");
	field.set(obj, args[0]);

	// Does the actual (non-CGLIB) superclass implement BeanFactoryAware?
	// If so, call its setBeanFactory() method. If not, just exit.
	if (BeanFactoryAware.class.isAssignableFrom(ClassUtils.getUserClass(obj.getClass().getSuperclass()))) {
		return proxy.invokeSuper(obj, args);
	}
	return null;
}
 
Example 20
/**
 * Create a new AbstractAutowireCapableBeanFactory.
 */
public AbstractAutowireCapableBeanFactory() {
	super();
	ignoreDependencyInterface(BeanNameAware.class);
	ignoreDependencyInterface(BeanFactoryAware.class);
	ignoreDependencyInterface(BeanClassLoaderAware.class);
}
 
Example 21
Source Project: api-boot   Source File: ApiBootResourceLoadAdvisor.java    License: Apache License 2.0 5 votes vote down vote up
public ApiBootResourceLoadAdvisor(ApiBootResourceLoadMethodInterceptor apiBootResourceLoadMethodInterceptor) {
    // build pointcut instance
    this.pointcut = buildPointcut();
    // build advice instance
    this.advice = apiBootResourceLoadMethodInterceptor;
    if (this.advice instanceof BeanFactoryAware) {
        ((BeanFactoryAware) this.advice).setBeanFactory(beanFactory);
    }
}
 
Example 22
Source Project: api-boot   Source File: ApiBootRateLimiterAdvisor.java    License: Apache License 2.0 5 votes vote down vote up
public ApiBootRateLimiterAdvisor(ApiBootRateLimiterMethodInterceptor apiBootRateLimiterMethodInterceptor) {
    // build pointcut instance
    this.pointcut = buildPointcut();
    // build advice instance
    this.advice = apiBootRateLimiterMethodInterceptor;
    if (this.advice instanceof BeanFactoryAware) {
        ((BeanFactoryAware) this.advice).setBeanFactory(beanFactory);
    }
}
 
Example 23
public ApiBootMessagePushClientSwitchAdvisor(ApiBootMessagePushSwitchAnnotationInterceptor apiBootMessagePushSwitchAnnotationInterceptor) {
    // build pointcut instance
    this.pointcut = buildPointcut();
    // build advice instance
    this.advice = apiBootMessagePushSwitchAnnotationInterceptor;
    if (this.advice instanceof BeanFactoryAware) {
        ((BeanFactoryAware) this.advice).setBeanFactory(beanFactory);
    }
}
 
Example 24
Source Project: api-boot   Source File: ApiBootDataSourceSwitchAdvisor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Initialize global variables using constructor
 *
 * @param apiBootDataSourceSwitchAnnotationInterceptor {@link ApiBootDataSourceSwitchAnnotationInterceptor}
 */
public ApiBootDataSourceSwitchAdvisor(ApiBootDataSourceSwitchAnnotationInterceptor apiBootDataSourceSwitchAnnotationInterceptor) {
    // build pointcut instance
    this.pointcut = buildPointcut();
    // build advice instance
    this.advice = apiBootDataSourceSwitchAnnotationInterceptor;
    if (this.advice instanceof BeanFactoryAware) {
        ((BeanFactoryAware) this.advice).setBeanFactory(beanFactory);
    }
}
 
Example 25
Source Project: micronaut-spring   Source File: SpringAwareListener.java    License: Apache License 2.0 5 votes vote down vote up
private void wireAwareObjects(Object bean) {
    if (bean instanceof EnvironmentAware) {
        ((EnvironmentAware) bean).setEnvironment(environmentProvider.get());
    }
    if (bean instanceof BeanFactoryAware) {
        ((BeanFactoryAware) bean).setBeanFactory(beanFactoryProvider.get());
    }
    if (bean instanceof ApplicationContextAware) {
        ((ApplicationContextAware) bean).setApplicationContext(applicationContextProvider.get());
    }
}
 
Example 26
Source Project: lams   Source File: AsyncAnnotationAdvisor.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Set the {@code BeanFactory} to be used when looking up executors by qualifier.
 */
@Override
public void setBeanFactory(BeanFactory beanFactory) {
	if (this.advice instanceof BeanFactoryAware) {
		((BeanFactoryAware) this.advice).setBeanFactory(beanFactory);
	}
}
 
Example 27
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
	Field field = ReflectionUtils.findField(obj.getClass(), BEAN_FACTORY_FIELD);
	Assert.state(field != null, "Unable to find generated BeanFactory field");
	field.set(obj, args[0]);

	// Does the actual (non-CGLIB) superclass implement BeanFactoryAware?
	// If so, call its setBeanFactory() method. If not, just exit.
	if (BeanFactoryAware.class.isAssignableFrom(ClassUtils.getUserClass(obj.getClass().getSuperclass()))) {
		return proxy.invokeSuper(obj, args);
	}
	return null;
}
 
Example 28
@Override
public boolean isMatch(Method candidateMethod) {
	return (candidateMethod.getName().equals("setBeanFactory") &&
			candidateMethod.getParameterTypes().length == 1 &&
			BeanFactory.class == candidateMethod.getParameterTypes()[0] &&
			BeanFactoryAware.class.isAssignableFrom(candidateMethod.getDeclaringClass()));
}
 
Example 29
/**
 * Create a new AbstractAutowireCapableBeanFactory.
 */
public AbstractAutowireCapableBeanFactory() {
	super();
	ignoreDependencyInterface(BeanNameAware.class);
	ignoreDependencyInterface(BeanFactoryAware.class);
	ignoreDependencyInterface(BeanClassLoaderAware.class);
}
 
Example 30
private void invokeAwareMethods(final String beanName, final Object bean) {
	if (bean instanceof Aware) {
		if (bean instanceof BeanNameAware) {
			((BeanNameAware) bean).setBeanName(beanName);
		}
		if (bean instanceof BeanClassLoaderAware) {
			((BeanClassLoaderAware) bean).setBeanClassLoader(getBeanClassLoader());
		}
		if (bean instanceof BeanFactoryAware) {
			((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
		}
	}
}