要使用AOP,首先要导入依赖包
org.aspectj aspectjweaver1.9.4
我们先写一个业务:
UserService:
package com.cyf.service;
public interface UserService {
public void add();
public void delete();
public void update();
public void select();
}
UserServiceimpl:
package com.cyf.service;
public class UserServiceImpl implements UserService{
public void add() {
System.out.println("增加一个数据");
}
public void delete() {
System.out.println("删除一个数据");
}
public void update() {
System.out.println("修改一个数据");
}
public void select() {
System.out.println("查询一个数据");
}
}
方法一、使用SpringAPI接口:
先写两个日志类:
Log类:
package com.cyf.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"方法");
}
}
AfterLog类:
package com.cyf.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AafterLog implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("方法"+method.getName()+"被执行,返回值是"+returnValue);
}
}
建一个application.xml配置文件:
测试类:
import com.cyf.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
UserService userService = (UserService) context.getBean("userserviceimpl");
userService.select();
}
}
方法二、自定义来实现AOP(切面定义):
先写一个自定义日志类:
DiyLog类:
package com.cyf.diy;
import com.cyf.service.UserService;
public class DiyLog {
public void Before(){
System.out.println("------执行前------");
}
public void After(){
System.out.println("------执行后------");
}
}
写application.xml配置文件:
测试类:
import com.cyf.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
UserService userService = (UserService) context.getBean("userserviceimpl");
userService.select();
}
}
方法三、使用注解实现:
写一个annotation类:
package com.cyf.diy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Before;
public class anntationPoint {
@Before("execution(* com.cyf.service.UserServiceImpl.*(..))")
public void Before(){
System.out.println("-----方法前------");
}
@After("execution(* com.cyf.service.UserServiceImpl.*(..))")
public void After(){
System.out.println("-----方法后------");
}
@Around("execution(* com.cyf.service.UserServiceImpl.*(..))")
public void Arround(ProceedingJoinPoint jp) throws Throwable {
System.out.println("环绕前");
Signature signature = jp.getSignature(); //获得签名
Object proceed = jp.proceed(); //执行方法
System.out.println("环绕后");
System.out.println(proceed);
}
}
写application.xml配置文件:
测试:
import com.cyf.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
UserService userService = (UserService) context.getBean("userserviceimpl");
userService.select();
}
}



