spring-aop(基于aspectj,xml文件)

导入包:aop联盟包,aspectj包,springaop包,spring-aspects

导入约束:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->

</beans>

基于注解配置

目标类:

package aop;

public class Target {
	/**
	 * 存储方法
	 */
	public void save() {
		System.out.println("save方法执行");
	}
	/**
	 * 修改方法
	 */
	public void modify() {
		System.out.println("modify方法执行");
	}

	/**
	 * 删除方法
	 */
	public void delet() {
		System.out.println("delet方法执行");
	}
	
}

切面类:

package aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class Myaspectj {
	/**
	 * 前置通知
	 * 
	 * @param joinPoint:获取切入点信息
	 */
	public void beforeMethod(JoinPoint joinPoint) {
		System.out.println("前置通知*********************");
	}

	/**
	 * 后置通知,获取返回值
	 * 
	 * @param afterResult
	 */
	public void afterRunMethod(Object afterResult) {
		System.out.println("后置通知*********************");
	}

	/**
	 * 环绕通知
	 * 
	 * @throws Throwable
	 */
	public Object aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("环绕通知前*********************");
		Object tagert = pjp.proceed();
		System.out.println("环绕通知后*********************");
		return tagert;
	}

	/**
	 * 异常通知:获取异常类型
	 * 
	 * @param exresult
	 */
	public void exMehtod(Throwable exresult) {
		System.out.println("异常通知*********************");
	}

	/**
	 * 最终通知
	 */
	public void afterMehtod() {
		System.out.println("最终通知*********************");
	}
}

applicationContext.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
	<!-- 将将要增强的类添加进入容器 -->
	<bean id="daoImpl" class="aop.Target" />
	<!--切面类  -->
	<bean id="myaspectj" class="aop.Myaspectj" />
	<!--通过aop生成代理  -->
	<aop:config>
		<!-- 配置切点 -->
		<aop:pointcut expression="execution(* aop.Target.save(..))" id="pc" />
		<aop:pointcut expression="execution(* aop.Target.modify(..))" id="pc1" />
		<aop:pointcut expression="execution(* aop.Target.delet(..))" id="pc2" />
		<!-- 配置切面 -->
		<aop:aspect ref="myaspectj">
			<aop:before method="beforeMethod" pointcut-ref="pc"/>
			<aop:after-returning method="afterRunMethod" pointcut-ref="pc1" returning="afterResult"/>
			<aop:around method="aroundMethod" pointcut-ref="pc2"/>
			<aop:after-throwing method="exMehtod" pointcut-ref="pc1" throwing="exresult"/>
			<aop:after method="afterMehtod" pointcut-ref="pc2"/>
		</aop:aspect>
	</aop:config>
</beans>

测试类:

package aop;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:applicationContext.xml")
public class test {
	@Resource(name="daoImpl")
	Target dao;
	
	@Test
	public void saveTest() {		
		dao.save();
		dao.delet();
		dao.modify();
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值