Spring练习四,AOP : 面向切面的编程,基于注解的方式配置

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();
	}
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值