spring——AOP

一、AOP 简介:百度百科;
面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),是软件开发中的一个热点,也是 Spring框架中的一个重要内容。利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
主要的功能是:日志记录,性能统计,安全控制,事务处理,异常处理等等。
二、开发步骤
1、model、service、serviceimpl、相继建立
2、切面类

public class StudentServiceAspect {
	private Logger logger = Logger.getLogger(StudentServiceAspect.class);
	
	public void doBefore(JoinPoint jp) {
		logger.info("测试 开始-----------------------------");
		logger.info(jp.getTarget().getClass().getName()+":"+jp.getSignature().getName()+":"+Arrays.toString(jp.getArgs()));
	}
	public void doAfter(JoinPoint jp) {
		logger.info("测试完毕--------------------------------------------");
	}
	public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
		logger.info("环绕开始执行------------------------");
//		方法名和返回值
		logger.info(pjp.getSignature().getName());
		Object obj = pjp.proceed();
		logger.info("返回值:"+obj);
		logger.info("环绕执行完毕---------------------------");
		return obj;
	}
	public void doReturn(JoinPoint jp) {
		logger.info("返回值是1--------------------------------------------");
	}
	public void doExcep(JoinPoint jp,Throwable ex) {
		logger.info("程序出异常了--------------------------------------------");
		logger.info(ex.getMessage());
	}
}

3、sericeimpl中的

public class StudentServiceImpl implements StudentService{

	@Override
	public void addStudent(Student stu) {
		System.out.println("添加学生信息"+stu.getName()+"---------------");
	}

	@Override
	public int updateStudent(Student stu) {
		System.out.println("更新学生信息"+stu.getName()+"----------------");
		return 1;
	}
	
}

4、resources中的配置
bean中的配置

<bean id="aspect" class="com.mao.spring.aop.aspect.StudentServiceAspect" />
	<bean id="student" class="com.mao.spring.aop.model.Student">
		<property name="id" value="11" />
		<property name="name" value="张刚" />
	</bean>
<bean id="studentService" class="com.mao.spring.aop.service.impl.StudentServiceImpl"></bean>

切面配置

<aop:config>
		<aop:aspect id="stuAspect" ref="aspect">
			<aop:pointcut expression="execution(* com.mao.spring.aop.service..*.*(..))" id="aa"/>
			<aop:before method="doBefore" pointcut-ref="aa"/>
			<aop:after method="doAfter" pointcut-ref="aa"/>
			<aop:around method="doAround" pointcut-ref="aa"/>
			<aop:after-returning method="doReturn" pointcut-ref="aa"/>
			<aop:after-throwing method="doExcep" pointcut-ref="aa" throwing="ex"/>
		</aop:aspect>
	</aop:config>

5、测试

public class StudentServiceTest {
	private Logger logger=Logger.getLogger(StudentServiceTest.class);
	private ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContent.xml");
	
	@Test
	public void testAdd() {
		Student stu = ac.getBean("student", Student.class);
		StudentService studentService = ac.getBean("studentService",StudentService.class);
		studentService.addStudent(stu);
		logger.info(" -----------------添加结束 -----------------------------");
	}
	@Test
	public void testUpdate() {
		Student stu = ac.getBean("student", Student.class);
		StudentService studentService = ac.getBean("studentService",StudentService.class);
		studentService.updateStudent(stu);
		logger.info(" -----------------更新结束 -----------------------------");
	}
}

测试结果

[main] INFO com.mao.spring.aop.aspect.StudentServiceAspect - 测试 开始-----------------------------
[main] INFO com.mao.spring.aop.aspect.StudentServiceAspect - com.mao.spring.aop.service.impl.StudentServiceImpl:addStudent:[Student [id=11, name=张刚]]
[main] INFO com.mao.spring.aop.aspect.StudentServiceAspect - 环绕开始执行------------------------
[main] INFO com.mao.spring.aop.aspect.StudentServiceAspect - addStudent
添加学生信息张刚---------------
[main] INFO com.mao.spring.aop.aspect.StudentServiceAspect - 返回值是1--------------------------------------------
[main] INFO com.mao.spring.aop.aspect.StudentServiceAspect - 返回值:null
[main] INFO com.mao.spring.aop.aspect.StudentServiceAspect - 环绕执行完毕---------------------------
[main] INFO com.mao.spring.aop.aspect.StudentServiceAspect - 测试完毕--------------------------------------------
[main] INFO com.mao.spring.aop.service.StudentServiceTest -  -----------------添加结束 -----------------------------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值