使用pojo+xml开发aop案例

定义一个接口
public interface EmployeeService {
 public void work();
 public void work(String name,String pass);
}
新建一个实体类实现上述接口
public class EmployeeServiceBean implements EmployeeService{
	@Override
	public void work() {
		//int i=1/0;
		System.out.println("++++++++++++++++++工作中+++++++++++++++++");
	}
	@Override
	public void work(String name,String pass) {
		//int i=1/0;
		System.out.println("++++++++++++++++++带参数的工作中+++++++++++++++++");
	}
}
定义一个切面类
public class AdviceUtil {
	//具体通知-------进行安全检测
	public void checkLogin(String name,String pass){
		System.out.println("检查用户登录的用户名和密码。。。。。。。。。。。。");
	}
	//通知
	public void goHome(){
		System.out.println("...............回家................");
	}
	//通知
	public void getUp(){
		System.out.println("...............起床................");
	}
	//通知
	public Object punchCard(ProceedingJoinPoint jp) throws Throwable{
		System.out.println("===============上班打卡================");
		Object obj=jp.proceed();
		System.out.println("===============下班打卡================");
		return obj;
	}
	//通知
	public void back(){
		System.out.println("===============back方法================");
	}
	//通知
	public void unWell(Exception ex){
		System.out.println("===============unWell方法================"+ex.getMessage());
	}
}
在xml文件中配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
		xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" >
<!-- 切面的bean -->
<bean id="adviceUtil" class="cn.csdn.advice.AdviceUtil"></bean>
<!-- 声明业务bean -->
<bean id="employeeServiceBean" class="cn.csdn.service.EmployeeServiceBean"></bean>
<!-- aop的配置     包括通知和切面-->
<aop:config>
	<!-- 声明一个切入点 -->
	<aop:pointcut expression="execution(* *.*work(..))" id="myPointcut"/>
	<aop:pointcut expression="execution(* *.*work(..)) and args(name,pass)" id="myPointcut1"/>
<!--配置切面   引入的是具体实现的bean       order是优先级 -->
	<aop:aspect ref="adviceUtil">
		<aop:before method="getUp" pointcut-ref="myPointcut"/>	
		<aop:before method="checkLogin" pointcut-ref="myPointcut1"/>	
		<aop:after method="goHome" pointcut-ref="myPointcut"/>
		<aop:around method="punchCard" pointcut-ref="myPointcut"/>
		<aop:after-returning method="back"  pointcut-ref="myPointcut"/>
		<aop:after-throwing method="unWell"  pointcut-ref="myPointcut" throwing="ex"/>	
		<!-- 引入通知 -->
		<aop:declare-parents types-matching="*..EmployeeServiceBean" 
		implement-interface="cn.csdn.advice.Auditable" default-impl="cn.csdn.advice.AuditableAdvice"/>
		
	</aop:aspect>
</aop:config>
</beans>

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值