Spring的AOP配置
1.先写一个普通类:
package com.spring.aop;
public class Common {
<wbr>public void execute(String username,String password){<br><wbr><wbr><wbr><wbr> System.out.println("------------------普通类----------------");<br><wbr><wbr> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr>
}
2.写一个切面类,用于合法性校验和日志添加:
package com.spring.aop;
public class Check {
<wbr>public void checkValidity(){<br><wbr><wbr><wbr><wbr> System.out.println("------------------验证合法性----------------");<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr>
public void addLog(JoinPoint j){
<wbr><wbr>System.out.println("------------------添加日志----------------");<br><wbr><wbr>Object obj[] = j.getArgs();<br><wbr><wbr> for(Object o :obj){<br><wbr><wbr><wbr> System.out.println(o);<br><wbr><wbr> }<br><wbr><wbr> System.out.println("========checkSecurity=="+j.getSignature().getName());//这个是获得方法名<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
}
3.配置AOP,使用XML方式:(注意红色标志的内容)
<?xml version="1.0" encoding="UTF-8"?>
<beans
<wbr>xmlns="<a href="http://www.springframework.org/schema/beans">http://www.springframework.org/schema/beans</a>"<br><wbr>xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>"<br><wbr><span style="color:#fd2202">xmlns:aop="</span><a href="http://www.springframework.org/schema/aop"><span style="color:#fd2202">http://www.springframework.org/schema/aop</span></a><span style="color:#fd2202">"<br></span><wbr>xsi:schemaLocation="<a href="http://www.springframework.org/schema/beans">http://www.springframework.org/schema/beans</a>
<a href="http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">http://www.springframework.org/schema/beans/spring-beans-2.5.xsd</a><br><wbr><a href="http://www.springframework.org/schema/aop"><span style="color:#fa1309">http://www.springframework.org/schema/aop</span></a>
<a href="http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><span style="color:#fa1309">http://www.springframework.org/schema/aop/spring-aop-2.5.xsd</span></a>"></wbr></wbr></wbr></wbr></wbr>
<wbr> <bean id="common" class="com.spring.aop.Common"/><br><wbr> <bean id="check" class="com.spring.aop.Check"/><br><wbr><wbr><wbr><br><wbr><span style="color:#ff5309"><aop:config><br><wbr><wbr><wbr> <aop:aspect id="myAop" ref="check"><br><wbr><wbr><wbr><wbr><wbr> <aop:pointcut id="target" <strong>expression="execution(* com.spring.aop.Common.execute(..))"/><br></strong><wbr><wbr><wbr><wbr><wbr> <aop:before method="checkValidity" pointcut-ref="target"/><br><wbr><wbr><wbr><wbr><wbr> <aop:after method="addLog" pointcut-ref="target"/><br><wbr><wbr><wbr> </aop:aspect><br><wbr> </aop:config><br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></beans><br><strong>注意:</strong></wbr></wbr></wbr></wbr></wbr></wbr>
execution(* com.spring.aop.*.*(..))"/
这样写应该就可以了
这是com.aptech.jb.epet.dao.hibimpl 包下所有的类的所有方法。。
第一个*代表所有的返回值类型
第二个*代表所有的类
第三个*代表类所有方法
最后一个..代表所有的参数。
<wbr></wbr>
4.最后写一个测试:
package com.spring.aop;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationC<wbr>ontext;</wbr>
public class Client {
<wbr>public static void main(String[] args) {<br><wbr><wbr><wbr><wbr> BeanFactory factory=new ClassPathXmlApplicationC<wbr>ontext("applicationContext-aop.xml");<br><wbr><wbr><wbr><wbr> Common c=(Common) factory.getBean("common");<br><wbr><wbr><wbr><wbr> c.execute("zhengjunhua","zhengjunhua");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr> }<br> }<br></wbr></wbr>
注意:
需要添加三个包:spring-aop.jar , aspectjrt.jar ,aspectjweaver.jar,否则会报错。
<wbr></wbr>
输出结果:
------------------验证合法性----------------
------------------普通类----------------
------------------添加日志----------------
zhengjunhua
zhengjunhua
========checkSecurity==execute