Spring的AOP配置

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>"&gt;</wbr></wbr></wbr></wbr></wbr>

<wbr> &lt;bean id="common" class="com.spring.aop.Common"/&gt;<br><wbr> &lt;bean id="check" class="com.spring.aop.Check"/&gt;<br><wbr><wbr><wbr><br><wbr><span style="color:#ff5309">&lt;aop:config&gt;<br><wbr><wbr><wbr> &lt;aop:aspect id="myAop" ref="check"&gt;<br><wbr><wbr><wbr><wbr><wbr> &lt;aop:pointcut id="target" <strong>expression="execution(* com.spring.aop.Common.execute(..))"/&gt;<br></strong><wbr><wbr><wbr><wbr><wbr> &lt;aop:before method="checkValidity" pointcut-ref="target"/&gt;<br><wbr><wbr><wbr><wbr><wbr> &lt;aop:after method="addLog" pointcut-ref="target"/&gt;<br><wbr><wbr><wbr> &lt;/aop:aspect&gt;<br><wbr> &lt;/aop:config&gt;<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span>&lt;/beans&gt;<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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值