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

SpringAOP案例(一)XML方式

2014-03-07 10:46 387 查看
第一步要导入spring架构包:一、XML方式1. TestAspect:切面类[java] viewplaincopypackage com.spring.aop;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;public class TestAspect {public void doAfter(JoinPoint jp) {System.out.println("log Ending method: " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());}public Object doAround(ProceedingJoinPoint pjp) throws Throwable {long time = System.currentTimeMillis();Object retVal = pjp.proceed();time = System.currentTimeMillis() - time;System.out.println("process time: " + time + " ms");return retVal;}public void doBefore(JoinPoint jp) {System.out.println("log Begining method: " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());}public void doThrowing(JoinPoint jp, Throwable ex) {System.out.println("method " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName() + " throw exception");System.out.println(ex.getMessage());}}2. AServiceImpl:目标对象[java] viewplaincopypackage com.spring.service;// 使用jdk动态代理public class AServiceImpl implements AService {public void barA() {System.out.println("AServiceImpl.barA()");}public void fooA(String _msg) {System.out.println("AServiceImpl.fooA(msg:" + _msg + ")");}}3. BServiceImpl:目标对象[java] viewplaincopypackage com.spring.service;// 使用cglibpublic class BServiceImpl {public void barB(String _msg, int _type) {System.out.println("BServiceImpl.barB(msg:" + _msg + " type:" + _type + ")");if (_type == 1)throw new IllegalArgumentException("测试异常");}public void fooB() {System.out.println("BServiceImpl.fooB()");}}
ApplicationContext:Spring配置文件   文件默认在src目录下[html] viewplaincopy<?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"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"><aop:config><aop:aspect id="TestAspect" ref="aspectBean"><!--配置com.spring.service包下所有类或接口的所有方法--><aop:pointcut id="businessService" expression="execution(* com.spring.service.*.*(..))" /><aop:before pointcut-ref="businessService" method="doBefore"/><aop:after pointcut-ref="businessService" method="doAfter"/><aop:around pointcut-ref="businessService" method="doAround"/><aop:after-throwing pointcut-ref="businessService" method="doThrowing" throwing="ex"/></aop:aspect></aop:config><bean id="aspectBean" class="com.spring.aop.TestAspect" /><bean id="aService" class="com.spring.service.AServiceImpl"></bean><bean id="bService" class="com.spring.service.BServiceImpl"></bean></beans>
package com.spring.main;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.spring.service.AService;//测试类public class SpringAOPTest {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");//对于实现接口的类,要得到它的接口的bean而不是接口的实现类AService aServiceImpl = (AService) ctx.getBean("aService");aServiceImpl.barA();aServiceImpl.fooA("这是springaop测试类!");// AService b=(AService) ctx.getBean("bService");// b.barA();// b.fooA("这是没有实现接口的类!");}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: