Spring 注解 实现 AOP (切面编程)

Spring 注解 实现 AOP (切面编程)


实现功能:AOP实现模拟日志

实体类

package top.vkiss.springaop.domain;
public class User {
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

DAO 层接口

package top.vkiss.springaop.dao;
public interface UserDao {
	public void addUser();
	public void delUser();
}

DAO 层实现类

package top.vkiss.springaop.dao;
import org.springframework.stereotype.Repository;
@Repository("ud")
public class UserDaoImpl implements UserDao {
	@Override
	public void addUser() {
		// TODO Auto-generated method stub
		int a = 10/0;
		System.out.println("     添加一名用户 !");
	}
	@Override
	public void delUser() {
		// TODO Auto-generated method stub
		System.out.println("     删除一名用户 !");
	}
}

service 层接口

package top.vkiss.springaop.service;
public interface UserService {
	public void addUser();
	public void delUser();
}

service 实现类

package top.vkiss.springaop.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.vkiss.springaop.dao.UserDao;
@Service("us")
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao ud;
	public UserDao getUd() {
		return ud;
	}
	public void setUd(UserDao ud) {
		this.ud = ud;
	}
	@Override
	public void addUser() {
		// TODO Auto-generated method stub
		ud.addUser();
	}
	@Override
	public void delUser() {
		// TODO Auto-generated method stub
		ud.delUser();
	}
}

Action 层接口

package top.vkiss.springaop.action;
public interface UserAction {
	public void addUser();
	public void delUser();
}

Action 层实现类

package top.vkiss.springaop.action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import top.vkiss.springaop.service.UserService;
@Controller("userAction")
public class UserActionImpl implements UserAction {
	@Autowired
	private UserService us;
	public UserService getUs() {
		return us;
	}
	public void setUs(UserService us) {
		this.us = us;
	}
	@Override
	public void addUser() {
		// TODO Auto-generated method stub
		us.addUser();
	}
	@Override
	public void delUser() {
		// TODO Auto-generated method stub
		us.delUser();
	}
}

日志层

package top.vkiss.springaop.log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Service;
@Service("daoLog")
@Aspect
public class DaoLog {
	@Pointcut("execution( * top.vkiss.springaop.dao.*Impl.*(..) )")
	public void pointCut_(){}
	@Before("pointCut_()")
	public void method_Before(){
		System.out.println("。日志操作...");
	}
	@After("pointCut_()")
	public void method_After(){
		System.out.println("。日志操作后。。。");
	}
	@AfterThrowing(pointcut="pointCut_()",throwing="e")
	public void afterThrows(JoinPoint joinPoint,Exception e){
		System.out.println("dao层的_"+joinPoint.getSignature().getName()+"出现了:"+e.toString()+"异常");
	}
	@AfterReturning(returning="result",pointcut="pointCut_()")
	public void method_After_returning(JoinPoint joinPoint,Object result){
		System.out.println("。return之后。。。返回值"+result);
	}
	public Object method_After_around(ProceedingJoinPoint jp){
		Object  resultObject=null;
		try {
			System.out.println("方法执行之前 。。。。");
			 resultObject=  jp.proceed() ;
			System.out.println("返回值执行的。。。。");
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("出现异常执行的代码");
		}//执行方法 
		System.out.println("方法执行之后的内容");
	     return resultObject;
	}
}

Test 测试类

package top.vkiss.springaop.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.vkiss.springaop.action.CustomerActionImpl;
import top.vkiss.springaop.action.UserActionImpl;
public class TestAOP {
	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		UserActionImpl uac = (UserActionImpl) ac.getBean("userAction");
		//uac.addUser();//异常测试
		uac.delUser();
	}
}

Spring 配置文件



	
	
	
	

项目中用到的jar包

aop 依赖包:

com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

struts 2 日志包:

commons-logging-1.1.3.jar

spring 核心包:
spring-aop-5.0.0.RELEASE.jar
spring-aspects-5.0.0.RELEASE.jar
spring-beans-5.0.0.RELEASE.jar
spring-context-5.0.0.RELEASE.jar
spring-core-5.0.0.RELEASE.jar
spring-expression-5.0.0.RELEASE.jar

注意:spring版本和jdk版本搭配(否则会出现各种异常)

           spring 3.0+  & JDK 1.7

           spring 4.0+  & JDK 1.8

 

 

你可能感兴趣的:(Spring)