Java Code Examples for org.springframework.aop.scope.ScopedProxyUtils

The following examples show how to use org.springframework.aop.scope.ScopedProxyUtils. 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
@Override
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) {
	boolean proxyTargetClass = true;
	if (node instanceof Element) {
		Element ele = (Element) node;
		if (ele.hasAttribute(PROXY_TARGET_CLASS)) {
			proxyTargetClass = Boolean.valueOf(ele.getAttribute(PROXY_TARGET_CLASS));
		}
	}

	// Register the original bean definition as it will be referenced by the scoped proxy
	// and is relevant for tooling (validation, navigation).
	BeanDefinitionHolder holder =
			ScopedProxyUtils.createScopedProxy(definition, parserContext.getRegistry(), proxyTargetClass);
	String targetBeanName = ScopedProxyUtils.getTargetBeanName(definition.getBeanName());
	parserContext.getReaderContext().fireComponentRegistered(
			new BeanComponentDefinition(definition.getBeanDefinition(), targetBeanName));
	return holder;
}
 
Example 2
@Test
public void cronTaskWithScopedProxy() {
	BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
	context.registerBeanDefinition("postProcessor", processorDefinition);
	new AnnotatedBeanDefinitionReader(context).register(ProxiedCronTestBean.class, ProxiedCronTestBeanDependent.class);
	context.refresh();

	ScheduledTaskHolder postProcessor = context.getBean("postProcessor", ScheduledTaskHolder.class);
	assertEquals(1, postProcessor.getScheduledTasks().size());

	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<CronTask> cronTasks = (List<CronTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("cronTasks");
	assertEquals(1, cronTasks.size());
	CronTask task = cronTasks.get(0);
	ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
	Object targetObject = runnable.getTarget();
	Method targetMethod = runnable.getMethod();
	assertEquals(context.getBean(ScopedProxyUtils.getTargetBeanName("target")), targetObject);
	assertEquals("cron", targetMethod.getName());
	assertEquals("*/7 * * * * ?", task.getExpression());
}
 
Example 3
@Test
public void autowiredMethodParameterWithStaticallyQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs = new ConstructorArgumentValues();
	cavs.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person = new RootBeanDefinition(QualifiedPerson.class, cavs, null);
	context.registerBeanDefinition(JUERGEN,
			ScopedProxyUtils.createScopedProxy(new BeanDefinitionHolder(person, JUERGEN), context, true).getBeanDefinition());
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedMethodParameterTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	context.refresh();
	QualifiedMethodParameterTestBean bean =
			(QualifiedMethodParameterTestBean) context.getBean("autowired");
	assertEquals(JUERGEN, bean.getPerson().getName());
}
 
Example 4
@Test
public void testAutowiredMethodParameterWithStaticallyQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs = new ConstructorArgumentValues();
	cavs.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person = new RootBeanDefinition(QualifiedPerson.class, cavs, null);
	context.registerBeanDefinition(JUERGEN,
			ScopedProxyUtils.createScopedProxy(new BeanDefinitionHolder(person, JUERGEN), context, true).getBeanDefinition());
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedMethodParameterTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	context.refresh();
	QualifiedMethodParameterTestBean bean =
			(QualifiedMethodParameterTestBean) context.getBean("autowired");
	assertEquals(JUERGEN, bean.getPerson().getName());
}
 
Example 5
@Override
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) {
	boolean proxyTargetClass = true;
	if (node instanceof Element) {
		Element ele = (Element) node;
		if (ele.hasAttribute(PROXY_TARGET_CLASS)) {
			proxyTargetClass = Boolean.valueOf(ele.getAttribute(PROXY_TARGET_CLASS));
		}
	}

	// Register the original bean definition as it will be referenced by the scoped proxy
	// and is relevant for tooling (validation, navigation).
	BeanDefinitionHolder holder =
			ScopedProxyUtils.createScopedProxy(definition, parserContext.getRegistry(), proxyTargetClass);
	String targetBeanName = ScopedProxyUtils.getTargetBeanName(definition.getBeanName());
	parserContext.getReaderContext().fireComponentRegistered(
			new BeanComponentDefinition(definition.getBeanDefinition(), targetBeanName));
	return holder;
}
 
Example 6
@Test
public void cronTaskWithScopedProxy() {
	BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
	context.registerBeanDefinition("postProcessor", processorDefinition);
	new AnnotatedBeanDefinitionReader(context).register(ProxiedCronTestBean.class, ProxiedCronTestBeanDependent.class);
	context.refresh();

	ScheduledTaskHolder postProcessor = context.getBean("postProcessor", ScheduledTaskHolder.class);
	assertEquals(1, postProcessor.getScheduledTasks().size());

	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<CronTask> cronTasks = (List<CronTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("cronTasks");
	assertEquals(1, cronTasks.size());
	CronTask task = cronTasks.get(0);
	ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
	Object targetObject = runnable.getTarget();
	Method targetMethod = runnable.getMethod();
	assertEquals(context.getBean(ScopedProxyUtils.getTargetBeanName("target")), targetObject);
	assertEquals("cron", targetMethod.getName());
	assertEquals("*/7 * * * * ?", task.getExpression());
}
 
Example 7
@Test
public void autowiredMethodParameterWithStaticallyQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs = new ConstructorArgumentValues();
	cavs.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person = new RootBeanDefinition(QualifiedPerson.class, cavs, null);
	context.registerBeanDefinition(JUERGEN,
			ScopedProxyUtils.createScopedProxy(new BeanDefinitionHolder(person, JUERGEN), context, true).getBeanDefinition());
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedMethodParameterTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	context.refresh();
	QualifiedMethodParameterTestBean bean =
			(QualifiedMethodParameterTestBean) context.getBean("autowired");
	assertEquals(JUERGEN, bean.getPerson().getName());
}
 
Example 8
@Test
public void testAutowiredMethodParameterWithStaticallyQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs = new ConstructorArgumentValues();
	cavs.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person = new RootBeanDefinition(QualifiedPerson.class, cavs, null);
	context.registerBeanDefinition(JUERGEN,
			ScopedProxyUtils.createScopedProxy(new BeanDefinitionHolder(person, JUERGEN), context, true).getBeanDefinition());
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedMethodParameterTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	context.refresh();
	QualifiedMethodParameterTestBean bean =
			(QualifiedMethodParameterTestBean) context.getBean("autowired");
	assertEquals(JUERGEN, bean.getPerson().getName());
}
 
Example 9
@Override
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) {
	boolean proxyTargetClass = true;
	if (node instanceof Element) {
		Element ele = (Element) node;
		if (ele.hasAttribute(PROXY_TARGET_CLASS)) {
			proxyTargetClass = Boolean.valueOf(ele.getAttribute(PROXY_TARGET_CLASS));
		}
	}

	// Register the original bean definition as it will be referenced by the scoped proxy
	// and is relevant for tooling (validation, navigation).
	BeanDefinitionHolder holder =
			ScopedProxyUtils.createScopedProxy(definition, parserContext.getRegistry(), proxyTargetClass);
	String targetBeanName = ScopedProxyUtils.getTargetBeanName(definition.getBeanName());
	parserContext.getReaderContext().fireComponentRegistered(
			new BeanComponentDefinition(definition.getBeanDefinition(), targetBeanName));
	return holder;
}
 
Example 10
@Override
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) {
	boolean proxyTargetClass = true;
	if (node instanceof Element) {
		Element ele = (Element) node;
		if (ele.hasAttribute(PROXY_TARGET_CLASS)) {
			proxyTargetClass = Boolean.valueOf(ele.getAttribute(PROXY_TARGET_CLASS));
		}
	}

	// Register the original bean definition as it will be referenced by the scoped proxy
	// and is relevant for tooling (validation, navigation).
	BeanDefinitionHolder holder =
			ScopedProxyUtils.createScopedProxy(definition, parserContext.getRegistry(), proxyTargetClass);
	String targetBeanName = ScopedProxyUtils.getTargetBeanName(definition.getBeanName());
	parserContext.getReaderContext().fireComponentRegistered(
			new BeanComponentDefinition(definition.getBeanDefinition(), targetBeanName));
	return holder;
}
 
Example 11
@Test
public void testAutowiredMethodParameterWithStaticallyQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs = new ConstructorArgumentValues();
	cavs.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person = new RootBeanDefinition(QualifiedPerson.class, cavs, null);
	context.registerBeanDefinition(JUERGEN,
			ScopedProxyUtils.createScopedProxy(new BeanDefinitionHolder(person, JUERGEN), context, true).getBeanDefinition());
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedMethodParameterTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	context.refresh();
	QualifiedMethodParameterTestBean bean =
			(QualifiedMethodParameterTestBean) context.getBean("autowired");
	assertEquals(JUERGEN, bean.getPerson().getName());
}
 
Example 12
@Test
public void testAutowiredMethodParameterWithStaticallyQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs = new ConstructorArgumentValues();
	cavs.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person = new RootBeanDefinition(QualifiedPerson.class, cavs, null);
	context.registerBeanDefinition(JUERGEN,
			ScopedProxyUtils.createScopedProxy(new BeanDefinitionHolder(person, JUERGEN), context, true).getBeanDefinition());
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedMethodParameterTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	context.refresh();
	QualifiedMethodParameterTestBean bean =
			(QualifiedMethodParameterTestBean) context.getBean("autowired");
	assertEquals(JUERGEN, bean.getPerson().getName());
}
 
Example 13
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)
		throws BeansException {
	bindEnvironmentIfNeeded(registry);
	for (String name : registry.getBeanDefinitionNames()) {
		BeanDefinition definition = registry.getBeanDefinition(name);
		if (isApplicable(registry, name, definition)) {
			BeanDefinitionHolder holder = new BeanDefinitionHolder(definition,
					name);
			BeanDefinitionHolder proxy = ScopedProxyUtils
					.createScopedProxy(holder, registry, true);
			definition.setScope("refresh");
			if (registry.containsBeanDefinition(proxy.getBeanName())) {
				registry.removeBeanDefinition(proxy.getBeanName());
			}
			registry.registerBeanDefinition(proxy.getBeanName(),
					proxy.getBeanDefinition());
		}
	}
}
 
Example 14
@Test
public void postProcessorDoesNotOverrideRegularBeanDefinitionsEvenWithScopedProxy() {
	RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
	rbd.setResource(new DescriptiveResource("XML or something"));
	BeanDefinitionHolder proxied = ScopedProxyUtils.createScopedProxy(
			new BeanDefinitionHolder(rbd, "bar"), beanFactory, true);
	beanFactory.registerBeanDefinition("bar", proxied.getBeanDefinition());
	beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);
	beanFactory.getBean("foo", Foo.class);
	beanFactory.getBean("bar", TestBean.class);
}
 
Example 15
@Test
public void postProcessorDoesNotOverrideRegularBeanDefinitionsEvenWithScopedProxy() {
	RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
	rbd.setResource(new DescriptiveResource("XML or something"));
	BeanDefinitionHolder proxied = ScopedProxyUtils.createScopedProxy(
			new BeanDefinitionHolder(rbd, "bar"), beanFactory, true);
	beanFactory.registerBeanDefinition("bar", proxied.getBeanDefinition());
	beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);
	beanFactory.getBean("foo", Foo.class);
	beanFactory.getBean("bar", TestBean.class);
}
 
Example 16
@Override
public void afterSingletonsInstantiated() {
    Map<String, Object> beans = this.applicationContext.getBeansWithAnnotation(ExtRocketMQTemplateConfiguration.class)
        .entrySet().stream().filter(entry -> !ScopedProxyUtils.isScopedTarget(entry.getKey()))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

    beans.forEach(this::registerTemplate);
}
 
Example 17
@Override public void afterSingletonsInstantiated() {
    Map<String, Object> beans = this.applicationContext.getBeansWithAnnotation(RocketMQTransactionListener.class)
        .entrySet().stream().filter(entry -> !ScopedProxyUtils.isScopedTarget(entry.getKey()))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

    beans.forEach(this::registerTransactionListener);
}
 
Example 18
@Override
public void afterSingletonsInstantiated() {
    Map<String, Object> beans = this.applicationContext.getBeansWithAnnotation(RocketMQMessageListener.class)
        .entrySet().stream().filter(entry -> !ScopedProxyUtils.isScopedTarget(entry.getKey()))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

    beans.forEach(this::registerContainer);
}
 
Example 19
@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 20
Source Project: lams   Source File: MBeanExporter.java    License: 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 21
Source Project: spring4-understanding   Source File: MBeanExporter.java    License: 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 22
@Test
public void postProcessorDoesNotOverrideRegularBeanDefinitionsEvenWithScopedProxy() {
	RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
	rbd.setResource(new DescriptiveResource("XML or something"));
	BeanDefinitionHolder proxied = ScopedProxyUtils.createScopedProxy(
			new BeanDefinitionHolder(rbd, "bar"), beanFactory, true);
	beanFactory.registerBeanDefinition("bar", proxied.getBeanDefinition());
	beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);
	beanFactory.getBean("foo", Foo.class);
	beanFactory.getBean("bar", TestBean.class);
}
 
Example 23
private void verifyEnvironment() {
	int configurers = this.context.getBeanNamesForType(TaskConfigurer.class).length;
	// retrieve the count of dataSources (without instantiating them) excluding
	// DataSource proxy beans
	long dataSources = Arrays
			.stream(this.context.getBeanNamesForType(DataSource.class))
			.filter((name -> !ScopedProxyUtils.isScopedTarget(name))).count();

	if (configurers == 0 && dataSources > 1) {
		throw new IllegalStateException(
				"To use the default TaskConfigurer the context must contain no more than"
						+ " one DataSource, found " + dataSources);
	}
}
 
Example 24
@Bean
public BeanDefinitionHolder proxyDataSource() {
	GenericBeanDefinition proxyBeanDefinition = new GenericBeanDefinition();
	proxyBeanDefinition.setBeanClassName("javax.sql.DataSource");
	BeanDefinitionHolder myDataSource = new BeanDefinitionHolder(
			proxyBeanDefinition, "dataSource2");
	ScopedProxyUtils.createScopedProxy(myDataSource,
			(BeanDefinitionRegistry) this.context.getBeanFactory(), true);
	return myDataSource;
}
 
Example 25
Source Project: spring-analysis-note   Source File: MBeanExporter.java    License: 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 26
Source Project: spring-analysis-note   Source File: ScopedProxyCreator.java    License: MIT License 4 votes vote down vote up
public static BeanDefinitionHolder createScopedProxy(
		BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry, boolean proxyTargetClass) {

	return ScopedProxyUtils.createScopedProxy(definitionHolder, registry, proxyTargetClass);
}
 
Example 27
Source Project: spring-analysis-note   Source File: ScopedProxyCreator.java    License: MIT License 4 votes vote down vote up
public static String getTargetBeanName(String originalBeanName) {
	return ScopedProxyUtils.getTargetBeanName(originalBeanName);
}
 
Example 28
Source Project: java-technology-stack   Source File: MBeanExporter.java    License: 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 29
Source Project: java-technology-stack   Source File: ScopedProxyCreator.java    License: MIT License 4 votes vote down vote up
public static BeanDefinitionHolder createScopedProxy(
		BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry, boolean proxyTargetClass) {

	return ScopedProxyUtils.createScopedProxy(definitionHolder, registry, proxyTargetClass);
}
 
Example 30
Source Project: java-technology-stack   Source File: ScopedProxyCreator.java    License: MIT License 4 votes vote down vote up
public static String getTargetBeanName(String originalBeanName) {
	return ScopedProxyUtils.getTargetBeanName(originalBeanName);
}