您的位置:首页 > 编程语言 > Java开发

Spring的AOP配置(基于xml)

2014-07-24 10:53 405 查看
一、导入相关jar包:spring-aop.jar , aspectjrt.jar ,aspectjweaver.jar,cglib-nodep-2.1_3.jar

二、创建相关类

业务类 Baction.java

aop类 Log.java

测试类 Test.java

---------------------------------------业务类 Baction.java----------------------------------------------

/**业务类

* @author yangxiaoyong

* @version 创建时间:2014年7月24日 上午10:43:51

* 参考:www.sql8.net

*/

public class Baction {

public void add(String p1,String p2){

System.out.println("------------------业务类----------------");

System.out.println("------------------添加用户----------------");

}

}

----------------------------aop类 Log.java-----------------------------------------

import org.aspectj.lang.JoinPoint;

/**日志类,aop切入类

* @author yangxiaoyong

* @version 创建时间:2014年7月24日 上午10:44:02

* 参考:www.sql8.net

*/

public class Log {

public void checkValidity(){

System.out.println("------------------验证合法性----------------");

}

public void addLog(JoinPoint j){

System.out.println("------------------添加日志----------------");

System.out.println("========方法名=="+j.getSignature().getName());//这个是获得方法名

System.out.println("------------------输入切入方法的相关参数----------------");

Object obj[] = j.getArgs();

for(Object o :obj){

System.out.println(o);

}

}

}

------------------------测试类 Test.java-----------------------------

import org.springframework.context.support.FileSystemXmlApplicationContext;

/**测试类

* @author yangxiaoyong

* @version 创建时间:2014年7月24日 上午10:44:59

* 参考:www.sql8.net

*/

public class Test {

public static void main(String[] args) {

FileSystemXmlApplicationContext factory=new FileSystemXmlApplicationContext("/WebRoot/WEB-INF/config/applicationContext-aop.xml");

Baction c=(Baction) factory.getBean("baction");

c.add("sql8.net","sql8.net");

}

}

三、applicationContext-aop.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-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
default-autowire="autodetect">

<bean id="baction" class="com.zjht.edw.test.aop.Baction"/>

<bean id="log" class="com.zjht.edw.test.aop.Log"/>

<aop:config>

<aop:aspect id="myAop" ref="log">

<aop:pointcut id="target" expression="execution(* com.zjht.edw.test.aop.Baction.add(..))"/>

<aop:before method="checkValidity" pointcut-ref="target"/>

<aop:after method="addLog" pointcut-ref="target"/>

</aop:aspect>

</aop:config>

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: