17、(知识篇)Spring使用AOP(XML方式)

/**

* Spring AOP 使用方法(xml形式)

* xml方式比较简单,首先在xml中配置好相关bean

* 然后设置切点和切面即可

* 详情参考application.xml

* @param args

*/


测试类:

package com.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.contoller.UserController;
import com.spring.vo.User;

public class Test {
	
	/**
	 * Spring AOP 使用方法(xml形式)
	 * 
	 * xml方式比较简单,首先在xml中配置好相关bean
	 * 
	 * 然后设置切点和切面即可
	 * 
	 * 详情参考application.xml
	 * 
	 * 
	 * @param args
	 */
	
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		//通过springioc获取bean
		UserController ctrl = (UserController) ctx.getBean("userController");
		//调用bean方法
		ctrl.saveUser(new User("Super Man", 20));
		//测试异常通知:ctrl.saveUser(new User("Super Man", 20));
	}
}

通知类(aop使用):

package com.spring.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;

public class LoggingTools {

	
	/**
	 * 前置通知
	 * @param joinpoint
	 */
	public void beforeLog(JoinPoint joinpoint){
		Signature sign = joinpoint.getSignature();
		System.out.println(" 前置通知 beforeAdvice ... "+sign.getName()+" params ... "+joinpoint.getArgs());
	}
	
	/**
	 * 后置通知 (即使有无异常都回执行,大致上等于java里的finally块)
	 * @param jp
	 */
	public void afterLog(JoinPoint jp){
		System.out.println(" 后置通知 afterAdvice ... "+jp.getSignature().getName());
	}
	
	/**
	 * 返回通知:相当于调用方法成功获取到返回值
	 * @param jp
	 * @param result
	 */
	public void afterReturningLog(JoinPoint jp,Object result){
		System.out.println(" 返回通知 afterReturingAdvice ... "+jp.getSignature().getName()+" the result is "+result);
	}
	
	/**
	 * 异常通知:运行异常触发一下事件,大致相当于catch块
	 * @param jp
	 * @param e
	 */
	public void afterThrowingLog(JoinPoint jp,Exception e){
		System.out.println(" 异常通知 afterThrowingAdvice ... "+jp.getSignature().getName()+" exception is ... "+e);
	}
	
	
	
	/**
	 * 环绕通知,是上面4种通知的完整版本
	 * 能够实现上面所有通知功能
	 * 参数使用proceedingJoinPoint
	 * @param pjd
	 * @return 
	 */
	/*public Object aroundLog(ProceedingJoinPoint pjd){
		Signature sign = pjd.getSignature();
		Object[] args = pjd.getArgs();
		Object result = null;
		try {
			System.out.println(" 前置通知 beforeAdvice ... "+sign.getName());
			
			//if(args[0] instanceof User){
				//return 0;
			//}
			
			result = pjd.proceed();//调用方法。。。
			System.out.println(" 返回通知 afterReturingAdvice ... "+sign.getName()+" the result is "+result);
		} catch (Throwable e) {
			// TODO: handle exception
			System.out.println(" 异常通知 afterThrowingAdvice ... "+sign.getName()+" exception is ... "+e);
			throw new RuntimeException(e);
		} finally{
			System.out.println(" 后置通知 afterAdvice ... "+sign.getName());
		}
		
		return result;
	}*/
	
	
}


package com.spring.aop;

public class JoinPointTest {

	public void customerJoinPoint() {

	}

	public void beforeJoinPoint() {
		System.out.println("before joinpoint test ... ");
	}
}

实体类(注意!bean中需要用get/set方法,xml才可以配置有效)

package com.spring.contoller;

import com.spring.dao.UserDao;
import com.spring.vo.User;

public class UserController {

	private UserDao userDao;
	
	
	public UserDao getUserDao() {
		return userDao;
	}


	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}


	public int saveUser(User user) {
		// TODO Auto-generated method stub
		return userDao.save(user);
	}

}

package com.spring.dao;

import com.spring.vo.User;


public class UserDao {


	public int save(User user) {
		// TODO Auto-generated method stub
		int money = 1000/user.getAge();
		System.out.println("save user ... "+user+"  , he has "+(1000/user.getAge())+" dollars");
		return money;
	}

}

package com.spring.vo;

public class User {
	private String name;
	private int age;
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public User(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "User [name=" + name + ", age=" + age + "]";
	}
	
}

applicationContext.xml




	 
	
	
	
		
	
	
	
	
	
		
		
			
										  
		
			
			
			
			
			
		
		
		
			
		
	
	



你可能感兴趣的:(JAVA知识类相关,spring学习)