Spring AOP : interception de totalite de la methode


Processs clef


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

In Spring AOP, 4 type of advices are supported :

1. Before advice – Run before the method execution

2. After returning advice – Run after the method returns a result

3. After throwing advice – Run after the method throws an exception

4. Around advice – Run around the method execution, combine all three advices above.

  
  PRARTIQUES
  
  dabns Spring-Customer.xml

1. Creer la class de advice Before par exeample  (code) #

import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
 
public class HijackBeforeMethod implements MethodBeforeAdvice
{
	@Override
	public void before(Method method, Object[] args, Object target)
		throws Throwable {
	        System.out.println("HijackBeforeMethod : Before method hijacked!");
	}
}


2. creer  un bean ObjectASurveillerServiceProxy (customerServiceProxy)
    2.1 'target' : definit kel beans on veut choper
    2.2 'interceptorName' : definit kel class (advice) on applique a cet object proxy/ target
    
    

 

 <bean id="customerServiceProxy" 
                 class="org.springframework.aop.framework.ProxyFactoryBean">
 
		<property name="target" ref="customerService" />
 
		<property name="interceptorNames">
			<list>
				<value>hijackBeforeMethodBean</value>
			</list>
		</property>
	</bean>
	
	
	
	
	



3. Creer le beans associer a cettte clase 

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
	<bean id="customerService" class="com.mkyong.customer.services.CustomerService">
		<property name="name" value="Yong Mook Kim" />
		<property name="url" value="http://www.mkyong.com" />
	</bean>
 
	<bean id="hijackBeforeMethodBean" class="com.mkyong.aop.HijackBeforeMethod" />
 
	<bean id="customerServiceProxy" 
                 class="org.springframework.aop.framework.ProxyFactoryBean">
 
		<property name="target" ref="customerService" />
 
		<property name="interceptorNames">
			<list>
				<value>hijackBeforeMethodBean</value>
			</list>
		</property>
	</bean>
</beans>



4. S'assurer que le pom a cglib
   <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mir.core</groupId>
    <artifactId>SpringAOPexample</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>SpringAOPexample</name>
    <url>http://maven.apache.org</url>
    <dependencies>
    <dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>3.8.1</version>
    			<scope>test</scope>
    		</dependency>

    		<!-- Spring framework -->
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring</artifactId>
    			<version>2.5.6</version>
    		</dependency>

    		<!-- Proxy class need this library -->
    		<dependency>
    			<groupId>cglib</groupId>
    			<artifactId>cglib</artifactId>
    			<version>2.2.2</version>
    		</dependency>
    </dependencies>
</project>
	
	
5. Run 

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mkyong.customer.services.CustomerService;
 

public class App {
	public static void main(String[] args) {
		ApplicationContext appContext = new ClassPathXmlApplicationContext(
				new String[] { "Spring-Customer.xml" });
 
		CustomerService cust = 
                                (CustomerService) appContext.getBean("customerServiceProxy");
 
		System.out.println("*************************");
		cust.printName();
		System.out.println("*************************");
		cust.printURL();
		System.out.println("*************************");
		try {
			cust.printThrowException();
		} catch (Exception e) {
 
		}
 
	}
}