Search in sources :

Example 1 with DefaultPointcutAdvisor

use of org.springframework.aop.support.DefaultPointcutAdvisor in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method sessionAttributeExposureWithInterface.

@SuppressWarnings("rawtypes")
@PathPatternsParameterizedTest
void sessionAttributeExposureWithInterface(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(MySessionAttributesControllerImpl.class, usePathPatterns, wac -> {
        wac.registerBeanDefinition("viewResolver", new RootBeanDefinition(ModelExposingViewResolver.class));
        DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        autoProxyCreator.setBeanFactory(wac.getBeanFactory());
        wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
        wac.getBeanFactory().registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor()));
    });
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPage");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(request.getAttribute("viewName")).isEqualTo("page1");
    HttpSession session = request.getSession();
    assertThat(session).isNotNull();
    assertThat(session.getAttribute("object1") != null).isTrue();
    assertThat(session.getAttribute("object2") != null).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("object1")).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("object2")).isTrue();
    request = new MockHttpServletRequest("POST", "/myPage");
    request.setSession(session);
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(request.getAttribute("viewName")).isEqualTo("page2");
    assertThat(session.getAttribute("object1") != null).isTrue();
    assertThat(session.getAttribute("object2") != null).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("object1")).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("object2")).isTrue();
}
Also used : DefaultAdvisorAutoProxyCreator(org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator) SimpleTraceInterceptor(org.springframework.aop.interceptor.SimpleTraceInterceptor) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpSession(jakarta.servlet.http.HttpSession) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) ModelMap(org.springframework.ui.ModelMap) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 2 with DefaultPointcutAdvisor

use of org.springframework.aop.support.DefaultPointcutAdvisor in project spring-framework by spring-projects.

the class CglibProxyControllerTests method initServlet.

@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());
}
Also used : Pointcut(org.springframework.aop.Pointcut) AnnotationMatchingPointcut(org.springframework.aop.support.annotation.AnnotationMatchingPointcut) AnnotationMatchingPointcut(org.springframework.aop.support.annotation.AnnotationMatchingPointcut) DefaultAdvisorAutoProxyCreator(org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator) SimpleTraceInterceptor(org.springframework.aop.interceptor.SimpleTraceInterceptor) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Controller(org.springframework.stereotype.Controller) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext)

Example 3 with DefaultPointcutAdvisor

use of org.springframework.aop.support.DefaultPointcutAdvisor in project spring-framework by spring-projects.

the class JdkProxyControllerTests method initServlet.

@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());
}
Also used : DefaultAdvisorAutoProxyCreator(org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator) SimpleTraceInterceptor(org.springframework.aop.interceptor.SimpleTraceInterceptor) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext)

Example 4 with DefaultPointcutAdvisor

use of org.springframework.aop.support.DefaultPointcutAdvisor in project spring-framework by spring-projects.

the class TrickyAspectJPointcutExpressionTests method testManualProxyJavaWithDynamicPointcut.

@Test
public void testManualProxyJavaWithDynamicPointcut() throws Exception {
    TestService target = new TestServiceImpl();
    LogUserAdvice logAdvice = new LogUserAdvice();
    AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
    pointcut.setExpression(String.format("@within(%s.Log)", getClass().getName()));
    testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, target, "TestServiceImpl");
}
Also used : DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultPointcutAdvisor

use of org.springframework.aop.support.DefaultPointcutAdvisor in project spring-framework by spring-projects.

the class TrickyAspectJPointcutExpressionTests method testManualProxyJavaWithStaticPointcutAndTwoClassLoaders.

@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(TestServiceImpl.class.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()).getDeclaredConstructor().newInstance();
    testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, other, "TestServiceImpl");
}
Also used : DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)47 Test (org.junit.jupiter.api.Test)29 DefaultAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator)16 SimpleTraceInterceptor (org.springframework.aop.interceptor.SimpleTraceInterceptor)16 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)14 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)12 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)11 TestBean (org.springframework.beans.testfixture.beans.TestBean)11 Advisor (org.springframework.aop.Advisor)8 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)7 CountingBeforeAdvice (org.springframework.aop.testfixture.advice.CountingBeforeAdvice)7 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)7 AspectJExpressionPointcut (org.springframework.aop.aspectj.AspectJExpressionPointcut)6 Pointcut (org.springframework.aop.Pointcut)5 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)5 ClassPathResource (org.springframework.core.io.ClassPathResource)4 ResourceScriptSource (org.springframework.scripting.support.ResourceScriptSource)4 Test (org.junit.Test)3 NameMatchMethodPointcut (org.springframework.aop.support.NameMatchMethodPointcut)3 StaticMethodMatcherPointcutAdvisor (org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor)3