Aop简单应用实例,注解模式
2011-05-19 11:04
423 查看
Service接口: package snail.service; public interface PersonService { public void save(String name); public void update(String name, Integer id); public String getName(Integer id); } 接口实现: package snail.serviceimp; import snail.service.PersonService; public class PersonServiceimp implements PersonService{ @Override public void save(String name) { System.out.println("woshi save()"); } @Override public void update(String name, Integer id) { System.out.println("woshi update()"); } @Override public String getName(Integer id) { System.out.println("woshi getName()"); return id+""; } } 切面类: package snail.service; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Aspect public class Myintercepter { @Pointcut("execution (* snail.serviceimp.PersonServiceimp.*(..))") public void anyMethod(){} @Before("anyMethod() && args(userName)")//定义前置通知,anyMethod()是必须的方法,//args(username)不是必须的,是接收方法的参数 public void doAccessCheck(String userName) { System.out.println("前置通知"+userName); } @AfterReturning(pointcut="anyMethod()",returning="revalue")//定义后置通知 returning参数是接收被拦截的方法的返回值 public void doReturnCheck(String revalue) { System.out.println("后置通知"+revalue); } @AfterThrowing(pointcut="anyMethod()", throwing="ex")//定义例外通知 public void doExceptionAction(Exception ex) { System.out.println("例外通知"+ex); } @After("anyMethod()")//定义最终通知 public void doReleaseAction() { System.out.println("最终通知"); } @Around("anyMethod()")//环绕通知 public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { System.out.println("环绕通知"); return pjp.proceed(); } }
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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <bean id="aop" class="snail.service.Myintercepter"></bean> <bean id="pp" class="snail.serviceimp.PersonServiceimp"></bean> </beans>
测试类:
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import snail.serviceimp.PersonServiceimp; public class test { public static void main(String[] args) { ApplicationContextatx=new ClassPathXmlApplicationContext("applicationContext.xml"); PersonService b=(PersonService) atx.getBean("pp"); //Myintercepter ss=(Myintercepter)atx.getBean("aop"); b.save("保存snail"); b.getName(1); b.update("gengxin", 5); } }
相关文章推荐
- Silverlight实用窍门系列:33.OOB模式下WebBrowser的简单应用--模拟浏览器和百度地图【附带源码实例】
- Spring---AOP基本概念以及Advice5种类型的通知注解应用实例
- 为Windows应用创建简单的异步调用模式(附实例代码)
- 05 Spring Aop实例(AOP 如此简单)@Aspect、@Around 注解方式配置
- 05 Spring Aop实例(AOP 如此简单)@Aspect、@Around 注解方式配置
- 05 Spring Aop实例(AOP 如此简单)@Aspect、@Around 注解方式配置
- 简单工厂模式——23种设计模式综合实例应用
- 基于注解配置的Spring MVC 简单的HelloWorld实例应用
- WebSphere MQ Java 应用开发简单实例(下篇:绑定模式开发)(client mode和binding mode)
- Silverlight实用窍门系列:33.OOB模式下WebBrowser的简单应用--模拟浏览器和百度地图【附带源码实例】
- Java经典编程300例之实例064 策略模式的简单应用
- 05 Spring Aop实例(AOP 如此简单)@Aspect、@Around 注解方式配置
- spring 4.0 AOP (使用AspectJ的注解方式 的aop实现)简单实例
- spring 4.0 AOP (使用AspectJ的注解方式 的aop实现)简单实例
- 05 Spring Aop实例(AOP 如此简单)@Aspect、@Around 注解方式配置
- WebSphere MQ Java 应用开发简单实例(上篇:客户端模式开发)(client mode和binding mode)
- Android AOP 注解详解及简单使用实例(三)
- 05 Spring Aop实例(AOP 如此简单)@Aspect、@Around 注解方式配置
- spring 4.0 AOP (使用AspectJ的注解方式 的aop实现)简单实例
- 05 Spring Aop实例(AOP 如此简单)@Aspect、@Around 注解方式配置