2016년 2월 21일 일요일

Spring methodInterceptor

There are some cases that are needed to add a method before or after executing method in service layer.
At that time, you can use methodInterceptor.

- structure






















- TestMethodInterceptor.java

package org.blog.test.service.component;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.stereotype.Component;

@Component
public class TestMethodInterceptor implements MethodInterceptor {

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("before method");
        Object retVal = invocation.proceed();
        System.out.println("after method");
        return retVal;
    }

}

- TestMethodConfig.java

package org.blog.test.service.config;

import org.blog.test.service.TestService;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class TestMethodConfig {

    @Autowired
    private TestService testService;

    @Bean
    @Primary
    public ProxyFactoryBean testProxyFactoryBean() {
        ProxyFactoryBean testProxyFactoryBean = new ProxyFactoryBean();
        testProxyFactoryBean.setTarget(testService);
        testProxyFactoryBean.setInterceptorNames("testMethodInterceptor");
        return testProxyFactoryBean;
    }
}

- TestServiceImpl.java

package org.blog.test.service.impl;

import org.blog.test.service.TestService;
import org.springframework.stereotype.Service;

@Service
public class TestServiceImpl implements TestService {

    @Override
    public void testMethod() {
        System.out.println("testService String");
    }

}

- TestService.java

package org.blog.test.service;

public interface TestService {

    void testMethod();
}

- TestApplication.java

package org.blog.test;

import org.blog.test.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestApplication implements CommandLineRunner {

    @Autowired
    private TestService testService;

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

    @Override
    public void run(String... arg0) throws Exception {
        testService.testMethod();
    }
}


- pom.xml


 4.0.0
 org.blog.test
 method-interceptor-project
 0.1.0

 
  org.springframework.boot
  spring-boot-starter-parent
  1.3.2.RELEASE
 
 
  
   org.springframework.boot
   spring-boot-starter-web
  
  
   org.projectlombok
   lombok
   1.16.6
  
 

 
  
   
    org.springframework.boot
    spring-boot-maven-plugin
   
  
 


- Execution Result

You can find the log that interceptor method was executed.

2016-02-21 23:23:06.283  INFO 9808 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
before method
testService String
after method
2016-02-21 23:23:06.295  INFO 9808 --- [           main] org.blog.test.TestApplication            : Started TestApplication in 3.972 seconds (JVM running for 4.402)



original source : http://www.mkyong.com/spring/spring-aop-examples-advice/

댓글 2개 :

  1. Is it possible to register the interceptor and the class/method being intercepted without annotations/xml?

    답글삭제
    답글
    1. Is there any reason to make the interceptor without annotation/xml setting?? I think it is the easiest way to register the interceptor for the service class.

      삭제