/** * Find the bean name for the current invocation. Assumes that an ExposeBeanNameAdvisor * has been included in the interceptor chain, and that the invocation is exposed * with ExposeInvocationInterceptor. * @return the bean name (never {@code null}) * @throws IllegalStateException if the bean name has not been exposed */ public static String getBeanName() throws IllegalStateException { return getBeanName(ExposeInvocationInterceptor.currentInvocation()); }
@Test public void testNoIntroduction() { String beanName = "foo"; TestBean target = new RequiresBeanNameBoundTestBean(beanName); ProxyFactory pf = new ProxyFactory(target); pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR); pf.addAdvisor(ExposeBeanNameAdvisors.createAdvisorWithoutIntroduction(beanName)); ITestBean proxy = (ITestBean) pf.getProxy(); assertFalse("No introduction", proxy instanceof NamedBean); // Requires binding proxy.getAge(); }
@Test public void testWithIntroduction() { String beanName = "foo"; TestBean target = new RequiresBeanNameBoundTestBean(beanName); ProxyFactory pf = new ProxyFactory(target); pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR); pf.addAdvisor(ExposeBeanNameAdvisors.createAdvisorIntroducingNamedBean(beanName)); ITestBean proxy = (ITestBean) pf.getProxy(); assertTrue("Introduction was made", proxy instanceof NamedBean); // Requires binding proxy.getAge(); NamedBean nb = (NamedBean) proxy; assertEquals("Name returned correctly", beanName, nb.getBeanName()); }
@Override public int getAge() { assertEquals(beanName, ExposeBeanNameAdvisors.getBeanName()); return super.getAge(); } }
/** * Find the bean name for the current invocation. Assumes that an ExposeBeanNameAdvisor * has been included in the interceptor chain, and that the invocation is exposed * with ExposeInvocationInterceptor. * @return the bean name (never {@code null}) * @throws IllegalStateException if the bean name has not been exposed */ public static String getBeanName() throws IllegalStateException { return getBeanName(ExposeInvocationInterceptor.currentInvocation()); }
/** * Find the bean name for the current invocation. Assumes that an ExposeBeanNameAdvisor * has been included in the interceptor chain, and that the invocation is exposed * with ExposeInvocationInterceptor. * @return the bean name (never {@code null}) * @throws IllegalStateException if the bean name has not been exposed */ public static String getBeanName() throws IllegalStateException { return getBeanName(ExposeInvocationInterceptor.currentInvocation()); }