Search in sources :

Example 1 with SimpleTraceInterceptor

use of org.springframework.aop.interceptor.SimpleTraceInterceptor 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 SimpleTraceInterceptor

use of org.springframework.aop.interceptor.SimpleTraceInterceptor 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 SimpleTraceInterceptor

use of org.springframework.aop.interceptor.SimpleTraceInterceptor 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 SimpleTraceInterceptor

use of org.springframework.aop.interceptor.SimpleTraceInterceptor in project spring-framework by spring-projects.

the class ConfigurationClassPostProcessorTests method genericsBasedInjectionWithEarlyGenericsMatchingOnJdkProxy.

@Test
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);
    assertThat(beanNames).contains("stringRepo");
    beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class));
    assertThat(beanNames.length).isEqualTo(1);
    assertThat(beanNames[0]).isEqualTo("stringRepo");
    beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class));
    assertThat(beanNames.length).isEqualTo(1);
    assertThat(beanNames[0]).isEqualTo("stringRepo");
    assertThat(AopUtils.isJdkDynamicProxy(beanFactory.getBean("stringRepo"))).isTrue();
}
Also used : DefaultAdvisorAutoProxyCreator(org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator) SimpleTraceInterceptor(org.springframework.aop.interceptor.SimpleTraceInterceptor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Test(org.junit.jupiter.api.Test)

Example 5 with SimpleTraceInterceptor

use of org.springframework.aop.interceptor.SimpleTraceInterceptor in project spring-framework by spring-projects.

the class ConfigurationClassPostProcessorTests method genericsBasedInjectionWithEarlyGenericsMatchingOnCglibProxy.

@Test
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);
    assertThat(beanNames).contains("stringRepo");
    beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class));
    assertThat(beanNames.length).isEqualTo(1);
    assertThat(beanNames[0]).isEqualTo("stringRepo");
    beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class));
    assertThat(beanNames.length).isEqualTo(1);
    assertThat(beanNames[0]).isEqualTo("stringRepo");
    assertThat(AopUtils.isCglibProxy(beanFactory.getBean("stringRepo"))).isTrue();
}
Also used : DefaultAdvisorAutoProxyCreator(org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator) SimpleTraceInterceptor(org.springframework.aop.interceptor.SimpleTraceInterceptor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator)16 SimpleTraceInterceptor (org.springframework.aop.interceptor.SimpleTraceInterceptor)16 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)16 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)14 Test (org.junit.jupiter.api.Test)8 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)5 Test (org.junit.Test)3 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)3 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)3 PathPatternsParameterizedTest (org.springframework.web.servlet.handler.PathPatternsParameterizedTest)3 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)3 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)3 WebApplicationContext (org.springframework.web.context.WebApplicationContext)2 DispatcherServlet (org.springframework.web.servlet.DispatcherServlet)2 MockServletConfig (org.springframework.web.testfixture.servlet.MockServletConfig)2 HttpSession (jakarta.servlet.http.HttpSession)1 Map (java.util.Map)1 HttpSession (javax.servlet.http.HttpSession)1 Pointcut (org.springframework.aop.Pointcut)1 AnnotationMatchingPointcut (org.springframework.aop.support.annotation.AnnotationMatchingPointcut)1