这个只是个简单AOP例子,包括前置通知,后置通知,环绕通知,和目标对象。写这个例子的主要目标只是想让想学AOP的能更快地入门,了解一下如何去配置AOP里面的东东。
目标对象的接口:IStudent.java

 1 /** */ /**
 2  * 
 3   */
 4 package  com.dragon.study;
 5
 6 /** */ /**
 7  *  @author  dragon
 8  *
 9   */
10 public   interface  IStudent  {
11     
12      public   void  addStudent(String name);
13 }
14

目标类:StudentImpl.java

 1 /** */ /**
 2  * 
 3   */
 4 package  com.dragon.study.Impl;
 5
 6 import  com.dragon.study.IStudent;
 7
 8 /** */ /**
 9  *  @author  dragon
10  *
11   */
12 public   class  StudentImpl  implements  IStudent {
13
14       public   void  addStudent(String name) {
15          System.out.println( " 欢迎  " + name + "  你加入Spring家庭! " );
16      }
17 }
18

前置通知:BeforeAdvice.java

 1 /** */ /**
 2  * 
 3   */
 4 package  com.dragon.Advice;
 5
 6 import  java.lang.reflect.Method;
 7
 8 import  org.springframework.aop.MethodBeforeAdvice;
 9
10 /** */ /**
11  *  @author  dragon
12  *
13   */
14 public   class  BeforeAdvice  implements  MethodBeforeAdvice {
15
16        public   void  before(Method method,Object[] args, Object target)
17                 throws  Throwable {
18           
19           System.out.println( " 这是BeforeAdvice类的before方法. " );
20           
21       }
22 }
23

后置通知:AfterAdvice.java

 1/** *//**
 2 * 
 3 */
 4package com.dragon.Advice;
 5
 6import java.lang.reflect.Method;
 7
 8import org.springframework.aop.AfterReturningAdvice;
 9
10/** *//**
11 * @author dragon
12 *
13 */
14public class AfterAdvice implements AfterReturningAdvice{
15    
16    public void afterReturning(Object returnValue ,Method method,
17                   Object[] args,Object target) throws Throwable{
18        System.out.println("这是AfterAdvice类的afterReturning方法.");
19    }
20      
21
22}
23

环绕通知:CompareInterceptor.java

 1/** *//**
 2 * 
 3 */
 4package com.dragon.Advice;
 5
 6import org.aopalliance.intercept.MethodInterceptor;
 7import org.aopalliance.intercept.MethodInvocation;
 8
 9
10/** *//**
11 * @author dragon
12 *
13 */
14public class CompareInterceptor implements MethodInterceptor{
15
16      public Object invoke(MethodInvocation invocation) throws Throwable{
17          Object result = null;
18         String stu_name = invocation.getArguments()[0].toString();
19         if ( stu_name.equals("dragon")){
20             //如果学生是dragon时,执行目标方法,
21              result= invocation.proceed();
22              
23         } else{
24             System.out.println("此学生是"+stu_name+"而不是dragon,不批准其加入.");
25         }
26        
27          return result;
28      }
29}
30

配置文件applicationContext.xml

 1<?xml version="1.0" encoding="UTF-8"?>
 2<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
 3
 4<beans>
 5
 6<bean id="beforeAdvice" class="com.dragon.Advice.BeforeAdvice"></bean>
 7<bean id="afterAdvice" class="com.dragon.Advice.AfterAdvice"></bean>
 8<bean id="compareInterceptor" class="com.dragon.Advice.CompareInterceptor"></bean>
 9<bean id="studenttarget" class="com.dragon.study.Impl.StudentImpl"></bean>
10
11<bean id="student" class="org.springframework.aop.framework.ProxyFactoryBean">
12  <property name="proxyInterfaces">
13    <value>com.dragon.study.IStudent</value>
14  </property>
15  <property name="interceptorNames">
16    <list>
17     <value>beforeAdvice</value>
18     <value>afterAdvice</value>
19    <value>compareInterceptor</value>  
20    </list>
21  </property>
22  <property name="target">
23    <ref bean="studenttarget"/>
24  </property>
25
26</bean>
27
28
29
30
31</beans>

现在开始写测试类,Test.java

 1/** *//**
 2 * 
 3 */
 4package com;
 5
 6import org.springframework.context.ApplicationContext;
 7import org.springframework.context.support.FileSystemXmlApplicationContext;
 8
 9import com.dragon.study.IStudent;
10
11/** *//**
12 * @author dragon
13 *
14 */
15public class Test {
16
17    /** *//**
18     * @param args
19     */
20    public static void main(String[] args) {
21        // TODO Auto-generated method stub
22      ApplicationContext ctx = 
23          new FileSystemXmlApplicationContext("/com/dragon/applicationContext.xml");
24      
25      IStudent person = (IStudent)ctx.getBean("student");
26      person.addStudent("dragon");
27      
28//      person.addStudent("javadragon");
29    }
30
31}
32

最新文章

  1. MIS系统开发利器,实施、维护人员自定义报表的福音,AgileEAS.NET SOA平台动态报表指南
  2. JQuery执行DOM批量克隆并插入的提效方法
  3. CentOS下搭建SVN服务器
  4. iOS LaunchScreen启动图设置
  5. 柯南君 :Oracle 分区技术 之 怎样支撑大数据操作?
  6. struts1标签-&lt;html:radio&gt; 使用注意事项
  7. 可以伸缩的查询面板 (searchBar)
  8. UVAlive3415 Guardian of Decency(最大独立集)
  9. java 配置及安装Eclipse
  10. JavaBean编程的基本思路-逻辑业务层
  11. Java 之 web.xml(Java之负基础实战)
  12. freemarker报错之十一
  13. mysql group by 过滤字段 只能在SELECT 后面出现,不能写其他字段 报错解决 关键字 sql_mode=only_full_group_by
  14. corefx 源码追踪:找到引起 SqlDataReader.ReadAsync 执行延迟的那行代码
  15. Spark2.X管理与开发
  16. Android开发 android沉浸式状态栏的适配(包含刘海屏)转载
  17. Redhat 6.7 x64升级SSH到OpenSSH_7.4p1完整文档
  18. Java对象序列化和返序列化
  19. 【array】数组复习
  20. from组件补充

热门文章

  1. PTA 7-1 还原二叉树 (25分)
  2. silky微服务业务主机简介
  3. 全球首发-基于.NET 6长线支持Zoomla!逐浪CMS v8.6.0正式发布
  4. 菜鸡的Java笔记 - java 线程的同步与死锁 (同步 synchronization,死锁 deadlock)
  5. [hdu7074]Little prince and the garden of roses
  6. 跟着老猫来搞GO,&quot;面向对象&quot;
  7. char *p、char p[]、字符串的几个题目
  8. 『学了就忘』Linux文件系统管理 — 61、使用parted命令进行分区
  9. C++ and OO Num. Comp. Sci. Eng. - Part 4.
  10. 【Meta】16s rRNA和16s rDNA的区别