spring aop案例
配置
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
">
<bean id="dao" class="com.bokesoft.yigo.Dao"></bean>
<bean id="dealwith" class="com.bokesoft.yigo.DealWith"></bean>
<aop:config >
<!-- 这个是切入点 -->
<aop:pointcut expression="within(com.bokesoft.yigo..*)" id="sss"/>
<!--把想执行的方法直入到类前 ref为类名称-->
<aop:aspect ref="dealwith">
<!--method 为方法名称 pointcut-ref 为切入点 -->
<aop:before method="doAften" pointcut-ref="sss" />
</aop:aspect>
</aop:config>
</beans>
package com.bokesoft.yigo;
public class Dao implements DaoI {
/* (non-Javadoc)
* @see com.bokesoft.yigo.DaoI#login()
*/
@Override
public void login(){
System.out.print("王梅时间");
}
}
//这个是切入类
public class DealWith {
public void doAften(){
System.out.println("111111");
}
}
这个是测试
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext a= new ClassPathXmlApplicationContext("applicationContext.xml");
DaoI s= (DaoI) a.getBean("dao");
s.login();
}
}
// 这个是around
public Object around(ProceedingJoinPoint jp){
jp.getSignature().getName(); // 方法名称
jp.getArgs(); // 拿到参数
try {
jp.proceed();//放行
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}