Spring Aop Schema实现
编程技术  /  houtizong 发布于 3年前   68
本例使用的是Spring2.5
1.Aop配置文件spring-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: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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="helloWorldServiceImpl" class="com.spring.bijian.aop.HelloWorldServiceImpl"> </bean> <bean id="aspect" class="com.spring.bijian.aop.HelloWorldAspect"> </bean> <aop:config> <aop:pointcut id="pointcut" expression="execution(* com.spring..*.sayHello(..)) and args(user)"/> <aop:aspect ref="aspect"> <aop:before pointcut-ref="pointcut" method="beforeAdvice(java.lang.String)" arg-names="user"/> <aop:after pointcut="execution(* com.spring..*.sayHello(..))" method="afterAdvice"/> <aop:after-returning pointcut="execution(* com.spring..*.sayAfterReturning(..))" method="afterReturningAdvice" arg-names="value" returning="value"/> <aop:after-throwing pointcut="execution(* com.spring..*.sayAfterThrowing(..))" method="afterThrowingAdvice" arg-names="e" throwing="e"/> <aop:around pointcut="execution(* com.spring..*.sayAround(..))" method="aroundAdvice"/> </aop:aspect> </aop:config> </beans>
2.HelloWorldService接口
package com.spring.bijian.aop;public interface HelloWorldService { public void sayHello(String username); public boolean sayAfterReturning(); public void sayAfterThrowing(); public int sayAround(String username,String password); }
3.HelloWorldServiceImpl类
package com.spring.bijian.aop;import java.lang.RuntimeException;public class HelloWorldServiceImpl implements HelloWorldService {public void sayHello(String username) {System.out.println("My first Aop test " + username);}public boolean sayAfterReturning() {System.out.println("after returning");return true;}public void sayAfterThrowing() {System.out.println("After throwing");throw new RuntimeException();}public int sayAround(String username, String password) {System.out.println("Around username:" + username + ",password:" + password);int val = 1;return val;}}
4.具体实现AOP编程的HelloWorldAspect类
package com.spring.bijian.aop;import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; public class HelloWorldAspect { public void beforeAdvice(String user){ //System.out.println(jp.getArgs()[0].toString()); System.out.println("before advice " + user); } public void afterAdvice(JoinPoint jp){ System.out.println("after advice "+jp.getArgs()[0].toString()); } public void afterReturningAdvice(Object value){ System.out.println("afterReturning advice "+value); } public void afterThrowingAdvice(Exception e){ System.out.println("after throwing advice exception:" + e); } public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable{ /**System.out.println(pjp.toLongString()); System.out.println(pjp.getSignature().toString()); System.out.println(pjp.getKind().toString()); System.out.println(pjp.getArgs()[0].toString()); System.out.println("name+psd");*/ Object [] obj = pjp.getArgs(); String username = (String) obj[0]; String password = (String) obj[1]; System.out.println(username+","+password); Object retVal; if(username.equals("bijian") && password.equals("color")){ retVal = pjp.proceed(); }else{ retVal = pjp.proceed(new Object[]{"ideal","*******"}); } System.out.println("name+password"); if(retVal == null) System.out.println("false"); else{ System.out.println(retVal.toString()); } return 2; } }
5.测试类
package com.spring.bijian.aop;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class AopTest {/** * @param args */public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("com/spring/bijian/aop/spring-aop.xml");HelloWorldService hello = (HelloWorldService) context.getBean("helloWorldServiceImpl");hello.sayHello("bijian");//System.out.println("===================================");//hello.sayAfterReturning();//System.out.println("===================================");//hello.sayAfterThrowing();System.out.println("===================================");Object val = (Object) hello.sayAround("bijian", "colo");System.out.println(val.toString());}}
运行结果:
before advice bijianMy first Aop test bijianafter advice bijian===================================bijian,coloAround username:ideal,password:*******name+password12
6.AOP Around(环绕通知)可以控制返回对象,即你可以返回一个与目标对象完全不同的返回值,虽然这很危险,但是你却可以办到。目标方法的返回值 就是 环绕通知的返回值。proceed() 方法可以在通知体内调用一次、多次 或根本不用调用;还可以控制执行方法的参数值。具体见aroundAdvice方法的实现。
这里使用AOP Around就可以用来进行一个身份验证,通过面向切面编程(AOP)将验证部分独立出来,使得代码的耦合度降低,如果使用面向对象思想(OOP)就应该是独立写一个身份验证的方法,然后当需要使用该方法再去new 一个身份验证方法的类,然后传递参数通过验证类中的方法去验证,这样代码的耦合度就变强了。
文章来源:http://blog.csdn.net/acceptedxukai/article/details/7784986
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接