@Test public void testExtendedResourceInjectionWithOverriding() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor(); bpp.setBeanFactory(bf); bf.addBeanPostProcessor(bpp); bf.registerResolvableDependency(BeanFactory.class, bf); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties props = new Properties(); props.setProperty("tb", "testBean3"); ppc.setProperties(props); ppc.postProcessBeanFactory(bf); RootBeanDefinition annotatedBd = new RootBeanDefinition(ExtendedResourceInjectionBean.class); TestBean tb5 = new TestBean(); annotatedBd.getPropertyValues().add("testBean2", tb5); bf.registerBeanDefinition("annotatedBean", annotatedBd); bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(NamedResourceInjectionBean.class)); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); TestBean tb2 = new TestBean(); bf.registerSingleton("testBean2", tb2); TestBean tb3 = new TestBean(); bf.registerSingleton("testBean3", tb3); TestBean tb4 = new TestBean(); bf.registerSingleton("testBean4", tb4); NestedTestBean tb6 = new NestedTestBean(); bf.registerSingleton("xy", tb6); ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean"); assertTrue(bean.initCalled); assertTrue(bean.init2Called); assertSame(tb, bean.getTestBean()); assertSame(tb5, bean.getTestBean2()); assertSame(tb4, bean.getTestBean3()); assertSame(tb3, bean.getTestBean4()); assertSame(tb6, bean.testBean5); assertSame(tb6, bean.testBean6); assertSame(bf, bean.beanFactory); try { bf.getBean("annotatedBean2"); } catch (BeanCreationException ex) { assertTrue(ex.getRootCause() instanceof NoSuchBeanDefinitionException); NoSuchBeanDefinitionException innerEx = (NoSuchBeanDefinitionException) ex.getRootCause(); assertEquals("testBean9", innerEx.getBeanName()); } bf.destroySingletons(); assertTrue(bean.destroyCalled); assertTrue(bean.destroy2Called); }
/** * 获取一个SpringBean服务 * * @param pBeanId Spring配置文件名中配置的SpringID号 * @return Object 返回的SpringBean实例 */ public static Object getSpringBean(String pBeanId) { Object springBean = null; try { springBean = applicationContext.getBean(pBeanId); } catch (NoSuchBeanDefinitionException e) { log.error( G4Constants.Exception_Head + "Spring配置文件中没有匹配到ID号为:[" + pBeanId + "]的SpringBean组件,请检查!"); log.error(e.getMessage()); } return springBean; }
@Test public void missingAuthenticationManagerGivesSensibleErrorMessage() { try { setContext("<http auto-config='true' />"); } catch (BeanCreationException e) { Throwable cause = ultimateCause(e); assertTrue(cause instanceof NoSuchBeanDefinitionException); NoSuchBeanDefinitionException nsbe = (NoSuchBeanDefinitionException) cause; assertEquals(BeanIds.AUTHENTICATION_MANAGER, nsbe.getBeanName()); assertTrue( nsbe.getMessage().endsWith(AuthenticationManagerFactoryBean.MISSING_BEAN_ERROR_MESSAGE)); } }
@Override public Object postProcess( Object bean, String beanName, Method method, List<Annotation> annotations) { if (this.beanAnnotationAware() && AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) { try { resolveTargetBeanFromMethodWithBeanAnnotation(method); } catch (NoSuchBeanDefinitionException e) { if (this.logger.isDebugEnabled()) { this.logger.debug( "Skipping endpoint creation; " + e.getMessage() + "; perhaps due to some '@Conditional' annotation."); } return null; } } List<Advice> adviceChain = extractAdviceChain(beanName, annotations); MessageHandler handler = createHandler(bean, method, annotations); if (!CollectionUtils.isEmpty(adviceChain) && handler instanceof AbstractReplyProducingMessageHandler) { ((AbstractReplyProducingMessageHandler) handler).setAdviceChain(adviceChain); } if (handler instanceof Orderable) { Order orderAnnotation = AnnotationUtils.findAnnotation(method, Order.class); if (orderAnnotation != null) { ((Orderable) handler).setOrder(orderAnnotation.value()); } } if (handler instanceof AbstractMessageProducingHandler || handler instanceof AbstractMessageRouter) { String sendTimeout = MessagingAnnotationUtils.resolveAttribute(annotations, "sendTimeout", String.class); if (sendTimeout != null) { Long value = Long.valueOf(this.beanFactory.resolveEmbeddedValue(sendTimeout)); if (handler instanceof AbstractMessageProducingHandler) { ((AbstractMessageProducingHandler) handler).setSendTimeout(value); } else { ((AbstractMessageRouter) handler).setSendTimeout(value); } } } boolean handlerExists = false; if (this.beanAnnotationAware() && AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) { Object handlerBean = this.resolveTargetBeanFromMethodWithBeanAnnotation(method); handlerExists = handlerBean != null && handler == handlerBean; } if (!handlerExists) { String handlerBeanName = generateHandlerBeanName(beanName, method); this.beanFactory.registerSingleton(handlerBeanName, handler); handler = (MessageHandler) this.beanFactory.initializeBean(handler, handlerBeanName); } if (AnnotatedElementUtils.isAnnotated(method, IdempotentReceiver.class.getName()) && !AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) { String[] interceptors = AnnotationUtils.getAnnotation(method, IdempotentReceiver.class).value(); for (String interceptor : interceptors) { DefaultBeanFactoryPointcutAdvisor advisor = new DefaultBeanFactoryPointcutAdvisor(); advisor.setAdviceBeanName(interceptor); NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut(); pointcut.setMappedName("handleMessage"); advisor.setPointcut(pointcut); advisor.setBeanFactory(this.beanFactory); if (handler instanceof Advised) { ((Advised) handler).addAdvisor(advisor); } else { ProxyFactory proxyFactory = new ProxyFactory(handler); proxyFactory.addAdvisor(advisor); handler = (MessageHandler) proxyFactory.getProxy(this.beanFactory.getBeanClassLoader()); } } } if (!CollectionUtils.isEmpty(adviceChain)) { for (Advice advice : adviceChain) { if (advice instanceof HandleMessageAdvice) { NameMatchMethodPointcutAdvisor handlerAdvice = new NameMatchMethodPointcutAdvisor(advice); handlerAdvice.addMethodName("handleMessage"); if (handler instanceof Advised) { ((Advised) handler).addAdvisor(handlerAdvice); } else { ProxyFactory proxyFactory = new ProxyFactory(handler); proxyFactory.addAdvisor(handlerAdvice); handler = (MessageHandler) proxyFactory.getProxy(this.beanFactory.getBeanClassLoader()); } } } } AbstractEndpoint endpoint = createEndpoint(handler, method, annotations); if (endpoint != null) { return endpoint; } return handler; }