Java Code Examples for org.springframework.aop.scope.ScopedProxyUtils#isScopedTarget()

The following examples show how to use org.springframework.aop.scope.ScopedProxyUtils#isScopedTarget() . 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: DataSourceDecoratorBeanPostProcessor.java    From spring-boot-data-source-decorator with Apache License 2.0 5 votes vote down vote up
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (bean instanceof DataSource
            && !ScopedProxyUtils.isScopedTarget(beanName)
            && !getDataSourceDecoratorProperties().getExcludeBeans().contains(beanName)) {
        DataSource dataSource = (DataSource) bean;
        DataSource decoratedDataSource = dataSource;
        Map<String, DataSourceDecorator> decorators = new LinkedHashMap<>();
        applicationContext.getBeansOfType(DataSourceDecorator.class)
                .entrySet()
                .stream()
                .sorted(Entry.comparingByValue(AnnotationAwareOrderComparator.INSTANCE))
                .forEach(entry -> decorators.put(entry.getKey(), entry.getValue()));
        List<DataSourceDecorationStage> decoratedDataSourceChainEntries = new ArrayList<>();
        for (Entry<String, DataSourceDecorator> decoratorEntry : decorators.entrySet()) {
            String decoratorBeanName = decoratorEntry.getKey();
            DataSourceDecorator decorator = decoratorEntry.getValue();

            DataSource dataSourceBeforeDecorating = decoratedDataSource;
            decoratedDataSource = Objects.requireNonNull(decorator.decorate(beanName, decoratedDataSource),
                    "DataSourceDecorator (" + decoratorBeanName + ", " + decorator + ") should not return null");

            if (dataSourceBeforeDecorating != decoratedDataSource) {
                decoratedDataSourceChainEntries.add(0, new DataSourceDecorationStage(decoratorBeanName, decorator, decoratedDataSource));
            }
        }
        if (dataSource != decoratedDataSource) {
            ProxyFactory factory = new ProxyFactory(bean);
            factory.setProxyTargetClass(true);
            factory.addInterface(DecoratedDataSource.class);
            factory.addAdvice(new DataSourceDecoratorInterceptor(beanName, dataSource, decoratedDataSource, decoratedDataSourceChainEntries));
            return factory.getProxy();
        }
    }
    return bean;
}
 
Example 2
Source File: MBeanExporter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Performs the actual autodetection process, delegating to an
 * {@code AutodetectCallback} instance to vote on the inclusion of a
 * given bean.
 * @param callback the {@code AutodetectCallback} to use when deciding
 * whether to include a bean or not
 */
private void autodetect(AutodetectCallback callback) {
	Set<String> beanNames = new LinkedHashSet<String>(this.beanFactory.getBeanDefinitionCount());
	beanNames.addAll(Arrays.asList(this.beanFactory.getBeanDefinitionNames()));
	if (this.beanFactory instanceof ConfigurableBeanFactory) {
		beanNames.addAll(Arrays.asList(((ConfigurableBeanFactory) this.beanFactory).getSingletonNames()));
	}
	for (String beanName : beanNames) {
		if (!isExcluded(beanName) && !isBeanDefinitionAbstract(this.beanFactory, beanName)) {
			try {
				Class<?> beanClass = this.beanFactory.getType(beanName);
				if (beanClass != null && callback.include(beanClass, beanName)) {
					boolean lazyInit = isBeanDefinitionLazyInit(this.beanFactory, beanName);
					Object beanInstance = (!lazyInit ? this.beanFactory.getBean(beanName) : null);
					if (!ScopedProxyUtils.isScopedTarget(beanName) && !this.beans.containsValue(beanName) &&
							(beanInstance == null ||
									!CollectionUtils.containsInstance(this.beans.values(), beanInstance))) {
						// Not already registered for JMX exposure.
						this.beans.put(beanName, (beanInstance != null ? beanInstance : beanName));
						if (logger.isInfoEnabled()) {
							logger.info("Bean with name '" + beanName + "' has been autodetected for JMX exposure");
						}
					}
					else {
						if (logger.isDebugEnabled()) {
							logger.debug("Bean with name '" + beanName + "' is already registered for JMX exposure");
						}
					}
				}
			}
			catch (CannotLoadBeanClassException ex) {
				if (this.allowEagerInit) {
					throw ex;
				}
				// otherwise ignore beans where the class is not resolvable
			}
		}
	}
}
 
Example 3
Source File: MBeanExporter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Performs the actual autodetection process, delegating to an
 * {@code AutodetectCallback} instance to vote on the inclusion of a
 * given bean.
 * @param callback the {@code AutodetectCallback} to use when deciding
 * whether to include a bean or not
 */
private void autodetect(AutodetectCallback callback) {
	Set<String> beanNames = new LinkedHashSet<String>(this.beanFactory.getBeanDefinitionCount());
	beanNames.addAll(Arrays.asList(this.beanFactory.getBeanDefinitionNames()));
	if (this.beanFactory instanceof ConfigurableBeanFactory) {
		beanNames.addAll(Arrays.asList(((ConfigurableBeanFactory) this.beanFactory).getSingletonNames()));
	}
	for (String beanName : beanNames) {
		if (!isExcluded(beanName) && !isBeanDefinitionAbstract(this.beanFactory, beanName)) {
			try {
				Class<?> beanClass = this.beanFactory.getType(beanName);
				if (beanClass != null && callback.include(beanClass, beanName)) {
					boolean lazyInit = isBeanDefinitionLazyInit(this.beanFactory, beanName);
					Object beanInstance = (!lazyInit ? this.beanFactory.getBean(beanName) : null);
					if (!ScopedProxyUtils.isScopedTarget(beanName) && !this.beans.containsValue(beanName) &&
							(beanInstance == null ||
									!CollectionUtils.containsInstance(this.beans.values(), beanInstance))) {
						// Not already registered for JMX exposure.
						this.beans.put(beanName, (beanInstance != null ? beanInstance : beanName));
						if (logger.isInfoEnabled()) {
							logger.info("Bean with name '" + beanName + "' has been autodetected for JMX exposure");
						}
					}
					else {
						if (logger.isDebugEnabled()) {
							logger.debug("Bean with name '" + beanName + "' is already registered for JMX exposure");
						}
					}
				}
			}
			catch (CannotLoadBeanClassException ex) {
				if (this.allowEagerInit) {
					throw ex;
				}
				// otherwise ignore beans where the class is not resolvable
			}
		}
	}
}
 
Example 4
Source File: MBeanExporter.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Performs the actual autodetection process, delegating to an
 * {@code AutodetectCallback} instance to vote on the inclusion of a
 * given bean.
 * @param callback the {@code AutodetectCallback} to use when deciding
 * whether to include a bean or not
 */
private void autodetect(Map<String, Object> beans, AutodetectCallback callback) {
	Assert.state(this.beanFactory != null, "No BeanFactory set");
	Set<String> beanNames = new LinkedHashSet<>(this.beanFactory.getBeanDefinitionCount());
	Collections.addAll(beanNames, this.beanFactory.getBeanDefinitionNames());
	if (this.beanFactory instanceof ConfigurableBeanFactory) {
		Collections.addAll(beanNames, ((ConfigurableBeanFactory) this.beanFactory).getSingletonNames());
	}

	for (String beanName : beanNames) {
		if (!isExcluded(beanName) && !isBeanDefinitionAbstract(this.beanFactory, beanName)) {
			try {
				Class<?> beanClass = this.beanFactory.getType(beanName);
				if (beanClass != null && callback.include(beanClass, beanName)) {
					boolean lazyInit = isBeanDefinitionLazyInit(this.beanFactory, beanName);
					Object beanInstance = null;
					if (!lazyInit) {
						beanInstance = this.beanFactory.getBean(beanName);
						if (!beanClass.isInstance(beanInstance)) {
							continue;
						}
					}
					if (!ScopedProxyUtils.isScopedTarget(beanName) && !beans.containsValue(beanName) &&
							(beanInstance == null ||
									!CollectionUtils.containsInstance(beans.values(), beanInstance))) {
						// Not already registered for JMX exposure.
						beans.put(beanName, (beanInstance != null ? beanInstance : beanName));
						if (logger.isDebugEnabled()) {
							logger.debug("Bean with name '" + beanName + "' has been autodetected for JMX exposure");
						}
					}
					else {
						if (logger.isTraceEnabled()) {
							logger.trace("Bean with name '" + beanName + "' is already registered for JMX exposure");
						}
					}
				}
			}
			catch (CannotLoadBeanClassException ex) {
				if (this.allowEagerInit) {
					throw ex;
				}
				// otherwise ignore beans where the class is not resolvable
			}
		}
	}
}
 
Example 5
Source File: MBeanExporter.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Performs the actual autodetection process, delegating to an
 * {@code AutodetectCallback} instance to vote on the inclusion of a
 * given bean.
 * @param callback the {@code AutodetectCallback} to use when deciding
 * whether to include a bean or not
 */
private void autodetect(Map<String, Object> beans, AutodetectCallback callback) {
	Assert.state(this.beanFactory != null, "No BeanFactory set");
	Set<String> beanNames = new LinkedHashSet<>(this.beanFactory.getBeanDefinitionCount());
	Collections.addAll(beanNames, this.beanFactory.getBeanDefinitionNames());
	if (this.beanFactory instanceof ConfigurableBeanFactory) {
		Collections.addAll(beanNames, ((ConfigurableBeanFactory) this.beanFactory).getSingletonNames());
	}

	for (String beanName : beanNames) {
		if (!isExcluded(beanName) && !isBeanDefinitionAbstract(this.beanFactory, beanName)) {
			try {
				Class<?> beanClass = this.beanFactory.getType(beanName);
				if (beanClass != null && callback.include(beanClass, beanName)) {
					boolean lazyInit = isBeanDefinitionLazyInit(this.beanFactory, beanName);
					Object beanInstance = null;
					if (!lazyInit) {
						beanInstance = this.beanFactory.getBean(beanName);
						if (!beanClass.isInstance(beanInstance)) {
							continue;
						}
					}
					if (!ScopedProxyUtils.isScopedTarget(beanName) && !beans.containsValue(beanName) &&
							(beanInstance == null ||
									!CollectionUtils.containsInstance(beans.values(), beanInstance))) {
						// Not already registered for JMX exposure.
						beans.put(beanName, (beanInstance != null ? beanInstance : beanName));
						if (logger.isDebugEnabled()) {
							logger.debug("Bean with name '" + beanName + "' has been autodetected for JMX exposure");
						}
					}
					else {
						if (logger.isTraceEnabled()) {
							logger.trace("Bean with name '" + beanName + "' is already registered for JMX exposure");
						}
					}
				}
			}
			catch (CannotLoadBeanClassException ex) {
				if (this.allowEagerInit) {
					throw ex;
				}
				// otherwise ignore beans where the class is not resolvable
			}
		}
	}
}