AOP面向切面编程MethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor接口的使用

    一、实现接口MethodBeforeAdvice该拦截器会在调用方法前执行

            实现接口   AfterReturningAdvice该拦截器会在调用方法后执行

            实现接口  MethodInterceptor该拦截器会在调用方法前后都执行,实现环绕结果。


package com.ly.model;

import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

public class Advice implements MethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor{

	@Override
	public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
		saveBeforeMessage();	
	}
	@Override
	public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
		saveAfterMessage();	
	}
	public void saveBeforeMessage(){
		System.out.println("调用BeforeAdvice成功");
	}
	public void saveAfterMessage(){
		System.out.println("调用AfterAdvice成功");
	}
	@Override
	public Object invoke(MethodInvocation arg0) throws Throwable {
		System.out.println("调用RoundService之前成功");
		Object result=arg0.proceed();
		System.out.println("调用RoundService之后成功");
		return result;
	}

}

以下为AOPservice的实现


package com.ly.service.impl;

import com.ly.service.AOPService;

public class AOPServiceImpl implements AOPService{
	@Override
	public void print(String message) {
		System.out.println(message);
		
	}
	@Override
	public void save() {
		System.out.println("保存信息成功");	
	}
}

以下为配置文件信息





	
	
	
	
	
	
	
	
		
		
		
	
    
      
	
		
		
			
		
		
		
        
            adviser
        
	

以下为测试类


package com.ly.control;

import java.io.IOException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;

import com.ly.service.AOPService;

public class SpringTest {
	public static void main(String[] args) throws IOException {
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext(new String[]{"springBeans.xml"});
		AOPService aopservice=  (AOPService) applicationContext.getBean("proxyService");

		//由于在springBeans.xml中只配置了 value="com\.ly\.service\.impl\.AOPServiceImpl\.print"适配print方法,没有适配save方法,故只有调用print方法时才会执行advice通知
		aopservice.print("调用print成功==========》");
		aopservice.save();
	}
}


二、在web应用中调用时需要用WebApplicationContext这个来得到Spring配置中的bean类

    

public class UserAction extends ActionSupport {
	private static final Logger log = Logger.getLogger(UserAction.class);

	private UserService userService;

	public UserService getUserService(){
		if(userService == null){
			WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getRequest().getServletContext());
			userService = (UserService) applicationContext.getBean("proxyService");
//			userService = new UserServiceImpl();
		}
		
		return userService;
	}
	




你可能感兴趣的:(spring,面向切面编程)