1.已有的接口和类
package aop.day2;
public interface IUserService {
void saveUser();
void deleteById(Long id);
Integer update();
}
package aop.day2;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements IUserService{
@Override
public void saveUser() {
System.out.println("saveUser成功!");
}
@Override
public void deleteById(Long id) {
System.out.println("delete成功!");
}
@Override
public Integer update() {
System.out.println("update成功!");
double r = Math.random();
System.out.println("random: "+r);
if(r<0.8) {
throw new RuntimeException("update error...");
}
return (int)(r*100);
}
}
2.创建日志输出的类
package aop.day2;
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.Component;
@Component
@Aspect
public class LogAspect {
@Pointcut("execution(* aop.day2.IUserService.*(..))")
public void pt() {}
@Before("execution(* aop.day2.IUserService.*(..))")
public void before(JoinPoint jp) {
System.out.println("log before ..."+jp.getSignature().getName());
}
@After("pt()")
public void after(JoinPoint jp) {
System.out.println("log after ..."+jp.getSignature().getName());
}
@AfterReturning(pointcut = "pt()",returning = "reObj")
public void afterReturn(JoinPoint jp,Object reObj) {
System.out.println("log afterReturn ..."+
jp.getSignature().getName()+"....返回结果为:"+reObj);
}
@AfterThrowing(pointcut = "pt()",throwing = "ex")
public void afterThrow(JoinPoint jp,Throwable ex) {
System.out.println("log exception ...."+jp.getSignature().getName()
+"....异常信息:"+ex);
}
/**
* 环绕增强,需要自己定义,哪些在之前执行,哪些在之后执行等。。。
* 因此方法必须接收ProceedingJoinPoint作为参数,以确保方法被拦截后还可以继续向下运行。
* pjp.proceed();
* 1. 方法体,应该将异常继续抛出
* 2. 方法必须有返回值。
* @param pjp
* @return
* @throws Throwable
*/
@Around("pt()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("around before....");
try {
Object result = pjp.proceed();
System.out.println("around after....");
return result;
}catch (Exception e) {
System.out.println("around throw exception");
throw new RuntimeException(e);
}finally {
System.out.println("around finally");
}
}
}
3.创建一个类来代替xml 扫描包
package aop.day2;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan(basePackages = "aop.day2")
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class AppConfig {
}
4.创建测试类
package spring_jd2007;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import aop.day2.AppConfig;
import aop.day2.IUserService;
@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(locations = "classpath:aop/day2/anno.xml")
@ContextConfiguration(classes = AppConfig.class)
public class Aop_day2 {
@Autowired
IUserService service;
@Test
public void test_aop2() {
service.deleteById(2L);
service.update();
}
@Test
public void aop_javaConfig_anno() {
AnnotationConfigApplicationContext ac =
new AnnotationConfigApplicationContext(AppConfig.class);
IUserService service =
ac.getBean("target",IUserService.class);
service.deleteById(1L);
service.saveUser();
service.update();
}
@Test
public void test_aop_schema() {
ClassPathXmlApplicationContext ac =
new ClassPathXmlApplicationContext(
"aop/day2/aop.xml");
IUserService service =
ac.getBean("target",IUserService.class);
service.deleteById(1L);
service.saveUser();
service.update();
}
}