org.springframework.aop.support.DefaultPointcutAdvisor Java Examples
The following examples show how to use
org.springframework.aop.support.DefaultPointcutAdvisor.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProxyFactoryTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testRemoveAdvisorByReference() { TestBean target = new TestBean(); ProxyFactory pf = new ProxyFactory(target); NopInterceptor nop = new NopInterceptor(); CountingBeforeAdvice cba = new CountingBeforeAdvice(); Advisor advisor = new DefaultPointcutAdvisor(cba); pf.addAdvice(nop); pf.addAdvisor(advisor); ITestBean proxied = (ITestBean) pf.getProxy(); proxied.setAge(5); assertEquals(1, cba.getCalls()); assertEquals(1, nop.getCount()); assertTrue(pf.removeAdvisor(advisor)); assertEquals(5, proxied.getAge()); assertEquals(1, cba.getCalls()); assertEquals(2, nop.getCount()); assertFalse(pf.removeAdvisor(new DefaultPointcutAdvisor(null))); }
Example #2
Source File: TrickyAspectJPointcutExpressionTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testManualProxyJavaWithStaticPointcutAndTwoClassLoaders() throws Exception { LogUserAdvice logAdvice = new LogUserAdvice(); AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); pointcut.setExpression(String.format("execution(* %s.TestService.*(..))", getClass().getName())); // Test with default class loader first... testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, new TestServiceImpl(), "TestServiceImpl"); // Then try again with a different class loader on the target... SimpleThrowawayClassLoader loader = new SimpleThrowawayClassLoader(new TestServiceImpl().getClass().getClassLoader()); // Make sure the interface is loaded from the parent class loader loader.excludeClass(TestService.class.getName()); loader.excludeClass(TestException.class.getName()); TestService other = (TestService) loader.loadClass(TestServiceImpl.class.getName()).newInstance(); testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, other, "TestServiceImpl"); }
Example #3
Source File: ServletAnnotationControllerHandlerMethodTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void proxiedFormController() throws Exception { initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() { @Override public void initialize(GenericWebApplicationContext wac) { wac.registerBeanDefinition("viewResolver", new RootBeanDefinition(TestViewResolver.class)); DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator(); autoProxyCreator.setBeanFactory(wac.getBeanFactory()); wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator); wac.getBeanFactory() .registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor())); } }, MyFormController.class); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do"); request.addParameter("name", "name1"); request.addParameter("age", "value2"); MockHttpServletResponse response = new MockHttpServletResponse(); getServlet().service(request, response); assertEquals("myView-name1-typeMismatch-tb1-myValue", response.getContentAsString()); }
Example #4
Source File: JdkProxyControllerTests.java From java-technology-stack with MIT License | 6 votes |
@SuppressWarnings("serial") private void initServlet(final Class<?> controllerclass) throws ServletException { servlet = new DispatcherServlet() { @Override protected WebApplicationContext createWebApplicationContext(@Nullable WebApplicationContext parent) { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.registerBeanDefinition("controller", new RootBeanDefinition(controllerclass)); DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator(); autoProxyCreator.setBeanFactory(wac.getBeanFactory()); wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator); wac.getBeanFactory().registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor(true))); wac.refresh(); return wac; } }; servlet.init(new MockServletConfig()); }
Example #5
Source File: ConfigurationClassPostProcessorTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void genericsBasedInjectionWithEarlyGenericsMatchingOnCglibProxy() { beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RepositoryConfiguration.class)); new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory); DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator(); autoProxyCreator.setProxyTargetClass(true); autoProxyCreator.setBeanFactory(beanFactory); beanFactory.addBeanPostProcessor(autoProxyCreator); beanFactory.registerSingleton("traceInterceptor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor())); String[] beanNames = beanFactory.getBeanNamesForType(Repository.class); assertTrue(ObjectUtils.containsElement(beanNames, "stringRepo")); beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class)); assertEquals(1, beanNames.length); assertEquals("stringRepo", beanNames[0]); beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class)); assertEquals(1, beanNames.length); assertEquals("stringRepo", beanNames[0]); assertTrue(AopUtils.isCglibProxy(beanFactory.getBean("stringRepo"))); }
Example #6
Source File: TrickyAspectJPointcutExpressionTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testManualProxyJavaWithStaticPointcutAndTwoClassLoaders() throws Exception { LogUserAdvice logAdvice = new LogUserAdvice(); AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); pointcut.setExpression(String.format("execution(* %s.TestService.*(..))", getClass().getName())); // Test with default class loader first... testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, new TestServiceImpl(), "TestServiceImpl"); // Then try again with a different class loader on the target... SimpleThrowawayClassLoader loader = new SimpleThrowawayClassLoader(new TestServiceImpl().getClass().getClassLoader()); // Make sure the interface is loaded from the parent class loader loader.excludeClass(TestService.class.getName()); loader.excludeClass(TestException.class.getName()); TestService other = (TestService) loader.loadClass(TestServiceImpl.class.getName()).newInstance(); testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, other, "TestServiceImpl"); }
Example #7
Source File: ConfigurationClassPostProcessorTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void genericsBasedInjectionWithEarlyGenericsMatchingOnJdkProxy() { beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RepositoryConfiguration.class)); new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory); DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator(); autoProxyCreator.setBeanFactory(beanFactory); beanFactory.addBeanPostProcessor(autoProxyCreator); beanFactory.registerSingleton("traceInterceptor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor())); String[] beanNames = beanFactory.getBeanNamesForType(RepositoryInterface.class); assertTrue(ObjectUtils.containsElement(beanNames, "stringRepo")); beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class)); assertEquals(1, beanNames.length); assertEquals("stringRepo", beanNames[0]); beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class)); assertEquals(1, beanNames.length); assertEquals("stringRepo", beanNames[0]); assertTrue(AopUtils.isJdkDynamicProxy(beanFactory.getBean("stringRepo"))); }
Example #8
Source File: ConfigurationClassPostProcessorTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void genericsBasedInjectionWithLateGenericsMatchingOnJdkProxyAndRawFactoryMethod() { beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RawFactoryMethodRepositoryConfiguration.class)); new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory); DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator(); autoProxyCreator.setBeanFactory(beanFactory); beanFactory.addBeanPostProcessor(autoProxyCreator); beanFactory.registerSingleton("traceInterceptor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor())); beanFactory.preInstantiateSingletons(); String[] beanNames = beanFactory.getBeanNamesForType(RepositoryInterface.class); assertTrue(ObjectUtils.containsElement(beanNames, "stringRepo")); beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class)); assertEquals(1, beanNames.length); assertEquals("stringRepo", beanNames[0]); beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class)); assertEquals(1, beanNames.length); assertEquals("stringRepo", beanNames[0]); assertTrue(AopUtils.isJdkDynamicProxy(beanFactory.getBean("stringRepo"))); }
Example #9
Source File: CgLibProxyServletAnnotationTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@SuppressWarnings("serial") private void initServlet(final Class<?> controllerclass) throws ServletException { servlet = new DispatcherServlet() { @Override protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) throws BeansException { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.registerBeanDefinition("controller", new RootBeanDefinition(controllerclass)); DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator(); autoProxyCreator.setProxyTargetClass(true); autoProxyCreator.setBeanFactory(wac.getBeanFactory()); wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator); wac.getBeanFactory().registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor(true))); wac.refresh(); return wac; } }; servlet.init(new MockServletConfig()); }
Example #10
Source File: AbstractAopProxyTests.java From spring-analysis-note with MIT License | 6 votes |
/** * Simple test that if we set values we can get them out again. */ @Test public void testValuesStick() { int age1 = 33; int age2 = 37; String name = "tony"; TestBean target1 = new TestBean(); target1.setAge(age1); ProxyFactory pf1 = new ProxyFactory(target1); pf1.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor())); pf1.addAdvisor(new DefaultPointcutAdvisor(new TimestampIntroductionInterceptor())); ITestBean tb = (ITestBean) pf1.getProxy(); assertEquals(age1, tb.getAge()); tb.setAge(age2); assertEquals(age2, tb.getAge()); assertNull(tb.getName()); tb.setName(name); assertEquals(name, tb.getName()); }
Example #11
Source File: AbstractAopProxyTests.java From spring-analysis-note with MIT License | 6 votes |
/** * Check that casting to Advised can't get around advice freeze. */ @Test public void testCannotAddAdvisorWhenFrozenUsingCast() throws Throwable { TestBean target = new TestBean(); target.setAge(21); ProxyFactory pc = new ProxyFactory(target); assertFalse(pc.isFrozen()); pc.addAdvice(new NopInterceptor()); ITestBean proxied = (ITestBean) createProxy(pc); pc.setFrozen(true); Advised advised = (Advised) proxied; assertTrue(pc.isFrozen()); try { advised.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor())); fail("Shouldn't be able to add Advisor when frozen"); } catch (AopConfigException ex) { assertTrue(ex.getMessage().contains("frozen")); } // Check it still works: proxy factory state shouldn't have been corrupted assertEquals(target.getAge(), proxied.getAge()); assertEquals(1, advised.getAdvisors().length); }
Example #12
Source File: AbstractAopProxyTests.java From spring-analysis-note with MIT License | 6 votes |
/** * Check that the string is informative. */ @Test public void testProxyConfigString() { TestBean target = new TestBean(); ProxyFactory pc = new ProxyFactory(target); pc.setInterfaces(ITestBean.class); pc.addAdvice(new NopInterceptor()); MethodBeforeAdvice mba = new CountingBeforeAdvice(); Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut(), mba); pc.addAdvisor(advisor); ITestBean proxied = (ITestBean) createProxy(pc); String proxyConfigString = ((Advised) proxied).toProxyConfigString(); assertTrue(proxyConfigString.contains(advisor.toString())); assertTrue(proxyConfigString.contains("1 interface")); }
Example #13
Source File: JdkProxyControllerTests.java From spring-analysis-note with MIT License | 6 votes |
@SuppressWarnings("serial") private void initServlet(final Class<?> controllerclass) throws ServletException { servlet = new DispatcherServlet() { @Override protected WebApplicationContext createWebApplicationContext(@Nullable WebApplicationContext parent) { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.registerBeanDefinition("controller", new RootBeanDefinition(controllerclass)); DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator(); autoProxyCreator.setBeanFactory(wac.getBeanFactory()); wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator); wac.getBeanFactory().registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor(true))); wac.refresh(); return wac; } }; servlet.init(new MockServletConfig()); }
Example #14
Source File: CglibProxyControllerTests.java From spring-analysis-note with MIT License | 6 votes |
@SuppressWarnings("serial") private void initServlet(final Class<?> controllerClass) throws ServletException { servlet = new DispatcherServlet() { @Override protected WebApplicationContext createWebApplicationContext(@Nullable WebApplicationContext parent) { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.registerBeanDefinition("controller", new RootBeanDefinition(controllerClass)); DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator(); autoProxyCreator.setProxyTargetClass(true); autoProxyCreator.setBeanFactory(wac.getBeanFactory()); wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator); Pointcut pointcut = new AnnotationMatchingPointcut(Controller.class); DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(pointcut, new SimpleTraceInterceptor(true)); wac.getBeanFactory().registerSingleton("advisor", advisor); wac.refresh(); return wac; } }; servlet.init(new MockServletConfig()); }
Example #15
Source File: GridifySpringEnhancer.java From ignite with Apache License 2.0 | 6 votes |
/** * Enhances the object on load. * * @param <T> Type of the object to enhance. * @param obj Object to augment/enhance. * @return Enhanced object. */ @SuppressWarnings({"unchecked"}) public static <T> T enhance(T obj) { ProxyFactory proxyFac = new ProxyFactory(obj); proxyFac.addAdvice(dfltAsp); proxyFac.addAdvice(setToValAsp); proxyFac.addAdvice(setToSetAsp); while (proxyFac.getAdvisors().length > 0) proxyFac.removeAdvisor(0); proxyFac.addAdvisor(new DefaultPointcutAdvisor( new GridifySpringPointcut(GridifySpringPointcut.GridifySpringPointcutType.DFLT), dfltAsp)); proxyFac.addAdvisor(new DefaultPointcutAdvisor( new GridifySpringPointcut(GridifySpringPointcut.GridifySpringPointcutType.SET_TO_VALUE), setToValAsp)); proxyFac.addAdvisor(new DefaultPointcutAdvisor( new GridifySpringPointcut(GridifySpringPointcut.GridifySpringPointcutType.SET_TO_SET), setToSetAsp)); return (T)proxyFac.getProxy(); }
Example #16
Source File: ServletAnnotationControllerHandlerMethodTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void proxiedFormController() throws Exception { initServlet(wac -> { wac.registerBeanDefinition("viewResolver", new RootBeanDefinition(TestViewResolver.class)); DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator(); autoProxyCreator.setBeanFactory(wac.getBeanFactory()); wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator); wac.getBeanFactory() .registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor())); }, MyFormController.class); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do"); request.addParameter("name", "name1"); request.addParameter("age", "value2"); MockHttpServletResponse response = new MockHttpServletResponse(); getServlet().service(request, response); assertEquals("myView-name1-typeMismatch-tb1-myValue", response.getContentAsString()); }
Example #17
Source File: ServletAnnotationControllerTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings("serial") public void proxiedStandardHandleMethod() throws Exception { DispatcherServlet servlet = new DispatcherServlet() { @Override protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.registerBeanDefinition("controller", new RootBeanDefinition(MyController.class)); DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator(); autoProxyCreator.setBeanFactory(wac.getBeanFactory()); wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator); wac.getBeanFactory().registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor())); wac.refresh(); return wac; } }; servlet.init(new MockServletConfig()); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do"); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.service(request, response); assertEquals("test", response.getContentAsString()); }
Example #18
Source File: JdkProxyServletAnnotationTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@SuppressWarnings("serial") private void initServlet(final Class<?> controllerclass) throws ServletException { servlet = new DispatcherServlet() { @Override protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) throws BeansException { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.registerBeanDefinition("controller", new RootBeanDefinition(controllerclass)); DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator(); autoProxyCreator.setBeanFactory(wac.getBeanFactory()); wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator); wac.getBeanFactory().registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor(true))); wac.refresh(); return wac; } }; servlet.init(new MockServletConfig()); }
Example #19
Source File: TrickyAspectJPointcutExpressionTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testManualProxyJavaWithStaticPointcutAndTwoClassLoaders() throws Exception { LogUserAdvice logAdvice = new LogUserAdvice(); AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); pointcut.setExpression(String.format("execution(* %s.TestService.*(..))", getClass().getName())); // Test with default class loader first... testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, new TestServiceImpl(), "TestServiceImpl"); // Then try again with a different class loader on the target... SimpleThrowawayClassLoader loader = new SimpleThrowawayClassLoader(new TestServiceImpl().getClass().getClassLoader()); // Make sure the interface is loaded from the parent class loader loader.excludeClass(TestService.class.getName()); loader.excludeClass(TestException.class.getName()); TestService other = (TestService) loader.loadClass(TestServiceImpl.class.getName()).newInstance(); testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, other, "TestServiceImpl"); }
Example #20
Source File: AbstractAopProxyTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testCanPreventCastToAdvisedUsingOpaque() { TestBean target = new TestBean(); ProxyFactory pc = new ProxyFactory(target); pc.setInterfaces(new Class<?>[] {ITestBean.class}); pc.addAdvice(new NopInterceptor()); CountingBeforeAdvice mba = new CountingBeforeAdvice(); Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut().addMethodName("setAge"), mba); pc.addAdvisor(advisor); assertFalse("Opaque defaults to false", pc.isOpaque()); pc.setOpaque(true); assertTrue("Opaque now true for this config", pc.isOpaque()); ITestBean proxied = (ITestBean) createProxy(pc); proxied.setAge(10); assertEquals(10, proxied.getAge()); assertEquals(1, mba.getCalls()); assertFalse("Cannot be cast to Advised", proxied instanceof Advised); }
Example #21
Source File: ConfigurationClassPostProcessorTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void genericsBasedInjectionWithEarlyGenericsMatchingOnCglibProxy() { beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RepositoryConfiguration.class)); new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory); DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator(); autoProxyCreator.setProxyTargetClass(true); autoProxyCreator.setBeanFactory(beanFactory); beanFactory.addBeanPostProcessor(autoProxyCreator); beanFactory.registerSingleton("traceInterceptor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor())); String[] beanNames = beanFactory.getBeanNamesForType(Repository.class); assertTrue(ObjectUtils.containsElement(beanNames, "stringRepo")); beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class)); assertEquals(1, beanNames.length); assertEquals("stringRepo", beanNames[0]); beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class)); assertEquals(1, beanNames.length); assertEquals("stringRepo", beanNames[0]); assertTrue(AopUtils.isCglibProxy(beanFactory.getBean("stringRepo"))); }
Example #22
Source File: ConfigurationClassPostProcessorTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void genericsBasedInjectionWithLateGenericsMatchingOnCglibProxy() { beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RepositoryConfiguration.class)); new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory); DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator(); autoProxyCreator.setProxyTargetClass(true); autoProxyCreator.setBeanFactory(beanFactory); beanFactory.addBeanPostProcessor(autoProxyCreator); beanFactory.registerSingleton("traceInterceptor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor())); beanFactory.preInstantiateSingletons(); String[] beanNames = beanFactory.getBeanNamesForType(Repository.class); assertTrue(ObjectUtils.containsElement(beanNames, "stringRepo")); beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class)); assertEquals(1, beanNames.length); assertEquals("stringRepo", beanNames[0]); beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class)); assertEquals(1, beanNames.length); assertEquals("stringRepo", beanNames[0]); assertTrue(AopUtils.isCglibProxy(beanFactory.getBean("stringRepo"))); }
Example #23
Source File: ConfigurationClassPostProcessorTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void genericsBasedInjectionWithLateGenericsMatchingOnCglibProxyAndRawInstance() { beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RawInstanceRepositoryConfiguration.class)); new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory); DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator(); autoProxyCreator.setProxyTargetClass(true); autoProxyCreator.setBeanFactory(beanFactory); beanFactory.addBeanPostProcessor(autoProxyCreator); beanFactory.registerSingleton("traceInterceptor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor())); beanFactory.preInstantiateSingletons(); String[] beanNames = beanFactory.getBeanNamesForType(Repository.class); assertTrue(ObjectUtils.containsElement(beanNames, "stringRepo")); beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class)); assertEquals(1, beanNames.length); assertEquals("stringRepo", beanNames[0]); beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class)); assertEquals(1, beanNames.length); assertEquals("stringRepo", beanNames[0]); assertTrue(AopUtils.isCglibProxy(beanFactory.getBean("stringRepo"))); }
Example #24
Source File: AdvisedSupport.java From lams with GNU General Public License v2.0 | 6 votes |
/** * Cannot add introductions this way unless the advice implements IntroductionInfo. */ @Override public void addAdvice(int pos, Advice advice) throws AopConfigException { Assert.notNull(advice, "Advice must not be null"); if (advice instanceof IntroductionInfo) { // We don't need an IntroductionAdvisor for this kind of introduction: // It's fully self-describing. addAdvisor(pos, new DefaultIntroductionAdvisor(advice, (IntroductionInfo) advice)); } else if (advice instanceof DynamicIntroductionAdvice) { // We need an IntroductionAdvisor for this kind of introduction. throw new AopConfigException("DynamicIntroductionAdvice may only be added as part of IntroductionAdvisor"); } else { addAdvisor(pos, new DefaultPointcutAdvisor(advice)); } }
Example #25
Source File: ConfigurationClassPostProcessorTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void genericsBasedInjectionWithLateGenericsMatchingOnJdkProxy() { beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RepositoryConfiguration.class)); new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory); DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator(); autoProxyCreator.setBeanFactory(beanFactory); beanFactory.addBeanPostProcessor(autoProxyCreator); beanFactory.registerSingleton("traceInterceptor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor())); beanFactory.preInstantiateSingletons(); String[] beanNames = beanFactory.getBeanNamesForType(RepositoryInterface.class); assertTrue(ObjectUtils.containsElement(beanNames, "stringRepo")); beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class)); assertEquals(1, beanNames.length); assertEquals("stringRepo", beanNames[0]); beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class)); assertEquals(1, beanNames.length); assertEquals("stringRepo", beanNames[0]); assertTrue(AopUtils.isJdkDynamicProxy(beanFactory.getBean("stringRepo"))); }
Example #26
Source File: ConfigurationClassPostProcessorTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void genericsBasedInjectionWithLateGenericsMatchingOnJdkProxyAndRawInstance() { beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RawInstanceRepositoryConfiguration.class)); new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory); DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator(); autoProxyCreator.setBeanFactory(beanFactory); beanFactory.addBeanPostProcessor(autoProxyCreator); beanFactory.registerSingleton("traceInterceptor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor())); beanFactory.preInstantiateSingletons(); String[] beanNames = beanFactory.getBeanNamesForType(RepositoryInterface.class); assertTrue(ObjectUtils.containsElement(beanNames, "stringRepo")); beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class)); assertEquals(1, beanNames.length); assertEquals("stringRepo", beanNames[0]); beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class)); assertEquals(1, beanNames.length); assertEquals("stringRepo", beanNames[0]); assertTrue(AopUtils.isJdkDynamicProxy(beanFactory.getBean("stringRepo"))); }
Example #27
Source File: AbstractAopProxyTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * Simple test that if we set values we can get them out again. */ @Test public void testValuesStick() { int age1 = 33; int age2 = 37; String name = "tony"; TestBean target1 = new TestBean(); target1.setAge(age1); ProxyFactory pf1 = new ProxyFactory(target1); pf1.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor())); pf1.addAdvisor(new DefaultPointcutAdvisor(new TimestampIntroductionInterceptor())); ITestBean tb = (ITestBean) pf1.getProxy(); assertEquals(age1, tb.getAge()); tb.setAge(age2); assertEquals(age2, tb.getAge()); assertNull(tb.getName()); tb.setName(name); assertEquals(name, tb.getName()); }
Example #28
Source File: AbstractAopProxyTests.java From java-technology-stack with MIT License | 6 votes |
/** * Simple test that if we set values we can get them out again. */ @Test public void testValuesStick() { int age1 = 33; int age2 = 37; String name = "tony"; TestBean target1 = new TestBean(); target1.setAge(age1); ProxyFactory pf1 = new ProxyFactory(target1); pf1.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor())); pf1.addAdvisor(new DefaultPointcutAdvisor(new TimestampIntroductionInterceptor())); ITestBean tb = (ITestBean) pf1.getProxy(); assertEquals(age1, tb.getAge()); tb.setAge(age2); assertEquals(age2, tb.getAge()); assertNull(tb.getName()); tb.setName(name); assertEquals(name, tb.getName()); }
Example #29
Source File: AbstractAopProxyTests.java From java-technology-stack with MIT License | 6 votes |
/** * Check that the string is informative. */ @Test public void testProxyConfigString() { TestBean target = new TestBean(); ProxyFactory pc = new ProxyFactory(target); pc.setInterfaces(ITestBean.class); pc.addAdvice(new NopInterceptor()); MethodBeforeAdvice mba = new CountingBeforeAdvice(); Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut(), mba); pc.addAdvisor(advisor); ITestBean proxied = (ITestBean) createProxy(pc); String proxyConfigString = ((Advised) proxied).toProxyConfigString(); assertTrue(proxyConfigString.contains(advisor.toString())); assertTrue(proxyConfigString.contains("1 interface")); }
Example #30
Source File: AbstractAopProxyTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testCanPreventCastToAdvisedUsingOpaque() { TestBean target = new TestBean(); ProxyFactory pc = new ProxyFactory(target); pc.setInterfaces(ITestBean.class); pc.addAdvice(new NopInterceptor()); CountingBeforeAdvice mba = new CountingBeforeAdvice(); Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut().addMethodName("setAge"), mba); pc.addAdvisor(advisor); assertFalse("Opaque defaults to false", pc.isOpaque()); pc.setOpaque(true); assertTrue("Opaque now true for this config", pc.isOpaque()); ITestBean proxied = (ITestBean) createProxy(pc); proxied.setAge(10); assertEquals(10, proxied.getAge()); assertEquals(1, mba.getCalls()); assertFalse("Cannot be cast to Advised", proxied instanceof Advised); }