SpringAOP案例(一)XML方式
来源:互联网 发布:java字符串双引号转义 编辑:程序博客网 时间:2024/05/17 05:13
第一步要导入spring架构包:
一、XML方式
1. TestAspect:切面类
- package 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:目标对象
- package 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:目标对象
- package com.spring.service;
- // 使用cglib
- public 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目录下
- <?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("这是没有实现接口的类!");}}
0 0
- SpringAOP案例(一)XML方式
- springAOP基于XML配置文件方式
- s2sh案例一 xml配置方式
- SpringAOP(一) ProxyFactoryBean
- SpringAOP配置详解带实例(基于XML配置文件方式)
- SpringAOP基本概念及实现原理(一)
- SpringAOP配置文件applicationContext.xml
- springAOP (注解方式)
- SpringAOP之Advisor方式
- SpringAOP的实现方式
- SpringAOP:简单的Aspect案例
- SpringAOP实例之简单实现(xml形式)
- SpringAOP切点配置(注解与xml配置)
- SpringAOP 处理通知中的参数(注解与xml配置)
- springAOP之aspectJ(一)
- springAOP源码解析(一)
- SpingAOP案例(一)注解(Annotation)方式
- Spring入门案例 注解方式配置(一)
- Win7上Git安装及配置过程
- org.apache.subversion.javahl.ClientException: Item is not readable 解决办法
- 用java调用oracle存储过程总结
- 编译loongson内核1b-linux-3.0-20130104问题总结
- 转自 https://www.digitalocean.com/community/articles/how-to-create-a-ssl-certificate-on-nginx-for-cent
- SpringAOP案例(一)XML方式
- 线程之ReadWriteLock用法
- java.lang.RuntimeException:Can't create handler inside trhead that has not called Looper.prepare
- 如何在visual2012里,调用(或引用)DeleteObject方法
- Linux的inode的理解
- java jsp el fn
- 【移动开发】Android中三种超实用的滑屏方式汇总(ViewPager、ViewFlipper、ViewFlow)
- 苹果公司发布iPhone 5s和iPhone 5c
- sql server 游标使用简示例