List of usage examples for org.springframework.aop.target LazyInitTargetSource LazyInitTargetSource
LazyInitTargetSource
From source file:org.springframework.jmx.export.MBeanExporter.java
/** * Registers beans that are configured for lazy initialization with the * <code>MBeanServer<code> indirectly through a proxy. * @param beanName the name of the bean in the <code>BeanFactory</code> * @param beanKey the key associated with this bean in the beans map * @return the <code>ObjectName</code> under which the bean was registered * with the <code>MBeanServer</code> * @throws JMException an error in the underlying JMX infrastructure * @throws InvalidTargetObjectTypeException an error in the definition of the MBean resource *//*w ww . j a v a2 s. co m*/ private ObjectName registerLazyInit(String beanName, String beanKey) throws JMException, InvalidTargetObjectTypeException { LazyInitTargetSource targetSource = new LazyInitTargetSource(); targetSource.setTargetBeanName(beanName); targetSource.setBeanFactory(this.beanFactory); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTargetSource(targetSource); proxyFactory.setProxyTargetClass(true); proxyFactory.setFrozen(true); Object proxy = proxyFactory.getProxy(); ObjectName objectName = getObjectName(proxy, beanKey); if (logger.isDebugEnabled()) { logger.debug("Registering lazy-init MBean [" + objectName + "]"); } ModelMBean mbean = createModelMBean(); mbean.setModelMBeanInfo(getMBeanInfo(proxy, beanKey)); mbean.setManagedResource(proxy, MR_TYPE_OBJECT_REFERENCE); return doRegister(mbean, objectName); }
From source file:org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration.java
@SuppressWarnings("unchecked") private <T> T lazyBean(Class<T> interfaceName) { LazyInitTargetSource lazyTargetSource = new LazyInitTargetSource(); String[] beanNamesForType = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(applicationContext, interfaceName);/* w w w.j a v a 2s . c om*/ if (beanNamesForType.length == 0) { return null; } String beanName; if (beanNamesForType.length > 1) { List<String> primaryBeanNames = Arrays.stream(beanNamesForType) .filter(i -> applicationContext instanceof ConfigurableApplicationContext) .filter(n -> ((ConfigurableApplicationContext) applicationContext).getBeanFactory() .getBeanDefinition(n).isPrimary()) .collect(Collectors.toList()); Assert.isTrue(primaryBeanNames.size() != 0, () -> "Found " + beanNamesForType.length + " beans for type " + interfaceName + ", but none marked as primary"); Assert.isTrue(primaryBeanNames.size() == 1, () -> "Found " + primaryBeanNames.size() + " beans for type " + interfaceName + " marked as primary"); beanName = primaryBeanNames.get(0); } else { beanName = beanNamesForType[0]; } lazyTargetSource.setTargetBeanName(beanName); lazyTargetSource.setBeanFactory(applicationContext); ProxyFactoryBean proxyFactory = new ProxyFactoryBean(); proxyFactory = objectPostProcessor.postProcess(proxyFactory); proxyFactory.setTargetSource(lazyTargetSource); return (T) proxyFactory.getObject(); }