@SuppressWarnings("unchecked") private <T> T lazyBean(Class<T> interfaceName) { LazyInitTargetSource lazyTargetSource = new LazyInitTargetSource(); String[] beanNamesForType = BeanFactoryUtils.beanNamesForTypeIncludingAncestors( applicationContext, interfaceName); 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(); }
@Override @Nullable public synchronized Object getTarget() throws BeansException { if (this.target == null) { this.target = getBeanFactory().getBean(getTargetBeanName()); postProcessTargetObject(this.target); } return this.target; }
@Override @Nullable public Object getTarget() { try { return super.getTarget(); } catch (RuntimeException ex) { if (logger.isInfoEnabled()) { logger.info("Failed to retrieve target for JMX-exposed bean [" + this.objectName + "]: " + ex); } throw ex; } }
private <T> T createLazyProxy(ListableBeanFactory beanFactory, Class<T> type) { ProxyFactory factory = new ProxyFactory(); LazyInitTargetSource source = new LazyInitTargetSource(); source.setTargetClass(type); source.setTargetBeanName(getBeanNameFor(beanFactory, MessageBus.class)); source.setBeanFactory(beanFactory); factory.setTargetSource(source); factory.addAdvice(new PassthruAdvice()); factory.setInterfaces(new Class<?>[] { type }); @SuppressWarnings("unchecked") T proxy = (T) factory.getProxy(); return proxy; }
@Override @Nullable protected AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource( Class<?> beanClass, String beanName) { if (getBeanFactory() instanceof ConfigurableListableBeanFactory) { BeanDefinition definition = ((ConfigurableListableBeanFactory) getBeanFactory()).getBeanDefinition(beanName); if (definition.isLazyInit()) { return new LazyInitTargetSource(); } } return null; }
private static void validateBeanCycle(Object auth, Set<String> beanNames) { if (auth != null && !beanNames.isEmpty()) { if (auth instanceof Advised) { Advised advised = (Advised) auth; TargetSource targetSource = advised.getTargetSource(); if (targetSource instanceof LazyInitTargetSource) { LazyInitTargetSource lits = (LazyInitTargetSource) targetSource; if (beanNames.contains(lits.getTargetBeanName())) { throw new FatalBeanException( "A dependency cycle was detected when trying to resolve the AuthenticationManager. Please ensure you have configured authentication."); } } } beanNames = Collections.emptySet(); } } }
@Override @Nullable protected AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource( Class<?> beanClass, String beanName) { if (getBeanFactory() instanceof ConfigurableListableBeanFactory) { BeanDefinition definition = ((ConfigurableListableBeanFactory) getBeanFactory()).getBeanDefinition(beanName); if (definition.isLazyInit()) { return new LazyInitTargetSource(); } } return null; }
private static void validateBeanCycle(Object auth, Set<String> beanNames) { if (auth != null && !beanNames.isEmpty()) { if (auth instanceof Advised) { Advised advised = (Advised) auth; TargetSource targetSource = advised.getTargetSource(); if (targetSource instanceof LazyInitTargetSource) { LazyInitTargetSource lits = (LazyInitTargetSource) targetSource; if (beanNames.contains(lits.getTargetBeanName())) { throw new FatalBeanException( "A dependency cycle was detected when trying to resolve the AuthenticationManager. Please ensure you have configured authentication."); } } } beanNames = Collections.emptySet(); } } }
LazyInitTargetSource targetSource = new LazyInitTargetSource(); targetSource.setTargetBeanName(beanName); targetSource.setBeanFactory(this.beanFactory); proxyFactory.setTargetSource(targetSource);
public synchronized Object getTarget() throws BeansException { if (this.target == null) { this.target = getBeanFactory().getBean(getTargetBeanName()); postProcessTargetObject(this.target); } return this.target; }
@Override protected AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource( Class<?> beanClass, String beanName) { if (getBeanFactory() instanceof ConfigurableListableBeanFactory) { BeanDefinition definition = ((ConfigurableListableBeanFactory) getBeanFactory()).getBeanDefinition(beanName); if (definition.isLazyInit()) { return new LazyInitTargetSource(); } } return null; }
private static void validateBeanCycle(Object auth, Set<String> beanNames) { if (auth != null && !beanNames.isEmpty()) { if (auth instanceof Advised) { Advised advised = (Advised) auth; TargetSource targetSource = advised.getTargetSource(); if (targetSource instanceof LazyInitTargetSource) { LazyInitTargetSource lits = (LazyInitTargetSource) targetSource; if (beanNames.contains(lits.getTargetBeanName())) { throw new FatalBeanException( "A dependency cycle was detected when trying to resolve the AuthenticationManager. Please ensure you have configured authentication."); } } } beanNames = Collections.emptySet(); } } }
@Override @Nullable public Object getTarget() { try { return super.getTarget(); } catch (RuntimeException ex) { if (logger.isInfoEnabled()) { logger.info("Failed to retrieve target for JMX-exposed bean [" + this.objectName + "]: " + ex); } throw ex; } }
LazyInitTargetSource targetSource = new LazyInitTargetSource(); targetSource.setTargetBeanName(beanName); targetSource.setBeanFactory(this.beanFactory); proxyFactory.setTargetSource(targetSource);
@Override public synchronized Object getTarget() throws BeansException { if (this.target == null) { this.target = getBeanFactory().getBean(getTargetBeanName()); postProcessTargetObject(this.target); } return this.target; }
protected AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource( Class beanClass, String beanName) { if (getBeanFactory() instanceof ConfigurableListableBeanFactory) { BeanDefinition definition = ((ConfigurableListableBeanFactory) getBeanFactory()).getBeanDefinition(beanName); if (definition.isLazyInit()) { return new LazyInitTargetSource(); } } return null; }
@Override @Nullable public Object getTarget() { try { return super.getTarget(); } catch (RuntimeException ex) { if (logger.isInfoEnabled()) { logger.info("Failed to retrieve target for JMX-exposed bean [" + this.objectName + "]: " + ex); } throw ex; } }
@SuppressWarnings("unchecked") private <T> T lazyBean(Class<T> interfaceName) { LazyInitTargetSource lazyTargetSource = new LazyInitTargetSource(); String[] beanNamesForType = BeanFactoryUtils.beanNamesForTypeIncludingAncestors( applicationContext, interfaceName); if (beanNamesForType.length == 0) { return null; } Assert.isTrue(beanNamesForType.length == 1, () -> "Expecting to only find a single bean for type " + interfaceName + ", but found " + Arrays.asList(beanNamesForType)); lazyTargetSource.setTargetBeanName(beanNamesForType[0]); lazyTargetSource.setBeanFactory(applicationContext); ProxyFactoryBean proxyFactory = new ProxyFactoryBean(); proxyFactory = objectPostProcessor.postProcess(proxyFactory); proxyFactory.setTargetSource(lazyTargetSource); return (T) proxyFactory.getObject(); }
@Override @Nullable public synchronized Object getTarget() throws BeansException { if (this.target == null) { this.target = getBeanFactory().getBean(getTargetBeanName()); postProcessTargetObject(this.target); } return this.target; }
@SuppressWarnings("unchecked") private <T> T lazyBean(Class<T> interfaceName) { LazyInitTargetSource lazyTargetSource = new LazyInitTargetSource(); String[] beanNamesForType = context.getBeanNamesForType(interfaceName); Assert.isTrue(beanNamesForType.length == 1 , "Expecting to only find a single bean for type " + interfaceName + ", but found " + Arrays.asList(beanNamesForType)); lazyTargetSource.setTargetBeanName(beanNamesForType[0]); lazyTargetSource.setBeanFactory(context); ProxyFactoryBean proxyFactory = new ProxyFactoryBean(); proxyFactory.setTargetSource(lazyTargetSource); proxyFactory.setInterfaces(new Class[] { interfaceName }); return (T) proxyFactory.getObject(); }