/** * Throws an IllegalArgumentException if it encounters a JDK dynamic proxy. * Metadata can only be read from target classes and CGLIB proxies! */ @Override protected void checkManagedBean(Object managedBean) throws IllegalArgumentException { if (AopUtils.isJdkDynamicProxy(managedBean)) { throw new IllegalArgumentException( "MetadataMBeanInfoAssembler does not support JDK dynamic proxies - " + "export the target beans directly or use CGLIB proxies instead"); } }
/** * Return the class to be used for the JMX descriptor field "class". * Only applied when the "exposeClassDescriptor" property is "true". * <p>The default implementation returns the first implemented interface * for a JDK proxy, and the target class else. * @param managedBean the bean instance (might be an AOP proxy) * @return the class to expose in the descriptor field "class" * @see #setExposeClassDescriptor * @see #getClassToExpose(Class) * @see org.springframework.aop.framework.AopProxyUtils#proxiedUserInterfaces(Object) */ protected Class<?> getClassForDescriptor(Object managedBean) { if (AopUtils.isJdkDynamicProxy(managedBean)) { return AopProxyUtils.proxiedUserInterfaces(managedBean)[0]; } return getClassToExpose(managedBean); }
@Test public void withJdkProxy() { ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithJdkProxy.class); aspectIsApplied(ctx); assertThat(AopUtils.isJdkDynamicProxy(ctx.getBean(FooService.class)), is(true)); }
@Test public void withExposedProxy() { ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithExposedProxy.class); aspectIsApplied(ctx); assertThat(AopUtils.isJdkDynamicProxy(ctx.getBean(FooService.class)), is(true)); }
@Test public void annotatedService_PTC_false() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(PTCFalse.class, NonAnnotatedServiceImpl.class); ctx.refresh(); AnnotatedService s = ctx.getBean(AnnotatedService.class); assertTrue("expected a jdk proxy", AopUtils.isJdkDynamicProxy(s)); assertThat(s, not(instanceOf(NonAnnotatedServiceImpl.class))); } }
@Test public void succeedsWhenJdkProxyAndScheduledMethodIsPresentOnInterface() throws InterruptedException { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(Config.class, JdkProxyTxConfig.class, RepoConfigB.class); ctx.refresh(); Thread.sleep(100); // allow @Scheduled method to be called several times MyRepositoryWithScheduledMethod repository = ctx.getBean(MyRepositoryWithScheduledMethod.class); CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class); assertThat("repository is not a proxy", AopUtils.isJdkDynamicProxy(repository), is(true)); assertThat("@Scheduled method never called", repository.getInvocationCount(), greaterThan(0)); assertThat("no transactions were committed", txManager.commits, greaterThan(0)); }
@Test public void nonAnnotatedService_PTC_false() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(PTCFalse.class, AnnotatedServiceImpl.class); ctx.refresh(); NonAnnotatedService s = ctx.getBean(NonAnnotatedService.class); assertTrue("expected a jdk proxy", AopUtils.isJdkDynamicProxy(s)); assertThat(s, not(instanceOf(AnnotatedServiceImpl.class))); }
private Foo jdkProxy(Foo foo) { ProxyFactory pf = new ProxyFactory(); pf.setTarget(foo); pf.addInterface(Foo.class); Foo proxy = (Foo) pf.getProxy(); assertTrue("Proxy is a JDK dynamic proxy", AopUtils.isJdkDynamicProxy(proxy)); assertThat(proxy, instanceOf(Foo.class)); return proxy; }
@Test public void withScopedProxyThroughAspectJPattern() throws IOException, ClassNotFoundException { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ComponentScanWithScopedProxyThroughAspectJPattern.class); ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope()); ctx.refresh(); // should cast to the interface FooService bean = (FooService) ctx.getBean("scopedProxyTestBean"); // should be dynamic proxy assertThat(AopUtils.isJdkDynamicProxy(bean), is(true)); }
@Test public void withScopedProxyThroughRegex() throws IOException, ClassNotFoundException { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ComponentScanWithScopedProxyThroughRegex.class); ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope()); ctx.refresh(); // should cast to the interface FooService bean = (FooService) ctx.getBean("scopedProxyTestBean"); // should be dynamic proxy assertThat(AopUtils.isJdkDynamicProxy(bean), is(true)); }
@Override protected Object createProxy(ProxyCreatorSupport as) { assertFalse("Not forcible CGLIB", as.isProxyTargetClass()); Object proxy = as.createAopProxy().getProxy(); assertTrue("Should be a JDK proxy: " + proxy.getClass(), AopUtils.isJdkDynamicProxy(proxy)); return proxy; }
@Test public void setFieldAndGetFieldViaJdkDynamicProxy() throws Exception { ProxyFactory pf = new ProxyFactory(this.person); pf.addInterface(Person.class); Person proxy = (Person) pf.getProxy(); assertTrue("Proxy is a JDK dynamic proxy", AopUtils.isJdkDynamicProxy(proxy)); assertSetFieldAndGetFieldBehaviorForProxy(proxy, this.person); }
@Test public void testProxyTargetClassWithInterfaceAsTarget() { ProxyFactory pf = new ProxyFactory(); pf.setTargetClass(ITestBean.class); Object proxy = pf.getProxy(); assertTrue("Proxy is a JDK proxy", AopUtils.isJdkDynamicProxy(proxy)); assertTrue(proxy instanceof ITestBean); assertEquals(ITestBean.class, AopProxyUtils.ultimateTargetClass(proxy)); ProxyFactory pf2 = new ProxyFactory(proxy); Object proxy2 = pf2.getProxy(); assertTrue("Proxy is a JDK proxy", AopUtils.isJdkDynamicProxy(proxy2)); assertTrue(proxy2 instanceof ITestBean); assertEquals(ITestBean.class, AopProxyUtils.ultimateTargetClass(proxy2)); }
@Test public void withScopedProxy() throws IOException, ClassNotFoundException { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ComponentScanWithScopedProxy.class); ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope()); ctx.refresh(); // should cast to the interface FooService bean = (FooService) ctx.getBean("scopedProxyTestBean"); // should be dynamic proxy assertThat(AopUtils.isJdkDynamicProxy(bean), is(true)); // test serializability assertThat(bean.foo(1), equalTo("bar")); FooService deserialized = (FooService) SerializationTestUtils.serializeAndDeserialize(bean); assertThat(deserialized, notNullValue()); assertThat(deserialized.foo(1), equalTo("bar")); }
@Test public void testCglibPrototypeInstance() { Object prototype = testPrototypeInstancesAreIndependent("cglibPrototype"); assertTrue("It's a cglib proxy", AopUtils.isCglibProxy(prototype)); assertFalse("It's not a dynamic proxy", AopUtils.isJdkDynamicProxy(prototype)); }
private void jdkAssertions(ITestBean tb, int nopInterceptorCount) { NopInterceptor nop = (NopInterceptor) beanFactory.getBean("nopInterceptor"); assertEquals(0, nop.getCount()); assertTrue(AopUtils.isJdkDynamicProxy(tb)); int age = 5; tb.setAge(age); assertEquals(age, tb.getAge()); assertEquals(2 * nopInterceptorCount, nop.getCount()); }
@Test public void testInterfacesScopedProxy() throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "org/springframework/context/annotation/scopedProxyInterfacesTests.xml"); context.getBeanFactory().registerScope("myScope", new SimpleMapScope()); // should cast to the interface FooService bean = (FooService) context.getBean("scopedProxyTestBean"); // should be dynamic proxy assertTrue(AopUtils.isJdkDynamicProxy(bean)); // test serializability assertEquals("bar", bean.foo(1)); FooService deserialized = (FooService) SerializationTestUtils.serializeAndDeserialize(bean); assertNotNull(deserialized); assertEquals("bar", deserialized.foo(1)); context.close(); }
@Test public void testDetectsInterfaces() throws Exception { ProxyFactoryBean fb = new ProxyFactoryBean(); fb.setTarget(new TestBean()); fb.addAdvice(new DebugInterceptor()); fb.setBeanFactory(new DefaultListableBeanFactory()); ITestBean proxy = (ITestBean) fb.getObject(); assertTrue(AopUtils.isJdkDynamicProxy(proxy)); }
@Test public void testRequestScopeWithProxiedInterfaces() { RequestContextHolder.setRequestAttributes(oldRequestAttributes); ApplicationContext context = createContext(ScopedProxyMode.INTERFACES); IScopedTestBean bean = (IScopedTestBean) context.getBean("request"); // should be dynamic proxy, implementing both interfaces assertTrue(AopUtils.isJdkDynamicProxy(bean)); assertTrue(bean instanceof AnotherScopeTestInterface); assertEquals(DEFAULT_NAME, bean.getName()); bean.setName(MODIFIED_NAME); RequestContextHolder.setRequestAttributes(newRequestAttributes); // this is a proxy so it should be reset to default assertEquals(DEFAULT_NAME, bean.getName()); RequestContextHolder.setRequestAttributes(oldRequestAttributes); assertEquals(MODIFIED_NAME, bean.getName()); }
@Test public void requestScopeWithProxiedInterfaces() { RequestContextHolder.setRequestAttributes(oldRequestAttributes); ApplicationContext context = createContext(INTERFACES); IScopedTestBean bean = (IScopedTestBean) context.getBean("request"); // should be dynamic proxy, implementing both interfaces assertTrue(AopUtils.isJdkDynamicProxy(bean)); assertTrue(bean instanceof AnotherScopeTestInterface); assertEquals(DEFAULT_NAME, bean.getName()); bean.setName(MODIFIED_NAME); RequestContextHolder.setRequestAttributes(newRequestAttributes); // this is a proxy so it should be reset to default assertEquals(DEFAULT_NAME, bean.getName()); RequestContextHolder.setRequestAttributes(oldRequestAttributes); assertEquals(MODIFIED_NAME, bean.getName()); }