spring中的AOP实验(二)

时间:2022-11-08 21:53:32

软件151  王帅

1、目标对象的接口:IStudent.java

  package  com.dragon.study;
  public   interface  IStudent  {
     public   void  addStudent(String name);

2、目标类:StudentImpl.java

 package  com.dragon.study.Impl;
 import  com.dragon.study.IStudent;
public   class  StudentImpl  implements  IStudent {
  public   void  addStudent(String name) {
 System.out.println( " 欢迎  " + name + "  你加入Spring家庭! " );
     } 

3、前置通知:BeforeAdvice.java

 package  com.dragon.Advice;
  import  java.lang.reflect.Method;
  import  org.springframework.aop.MethodBeforeAdvice;
 public   class  BeforeAdvice  implements  MethodBeforeAdvice {
public   void  before(Method method,Object[] args, Object target)
 throws  Throwable {  
System.out.println( " 这是BeforeAdvice类的before方法. " );   
 } 

4、后置通知:AfterAdvice.java

package com.dragon.Advice;
 import java.lang.reflect.Method;
 import org.springframework.aop.AfterReturningAdvice;
public class AfterAdvice implements AfterReturningAdvice{
 public void afterReturning(Object returnValue ,Method method,
Object[] args,Object target) throws Throwable{
 System.out.println("这是AfterAdvice类的afterReturning方法.");
}
}

5、环绕通知:CompareInterceptor.java

package com.dragon.Advice;
 import org.aopalliance.intercept.MethodInterceptor;
 import org.aopalliance.intercept.MethodInvocation;
public class CompareInterceptor implements MethodInterceptor{
 public Object invoke(MethodInvocation invocation) throws Throwable{
 Object result = null;
String stu_name = invocation.getArguments()[].toString();
if ( stu_name.equals("dragon")){
   //如果学生是dragon时,执行目标方法,
 result= invocation.proceed();       
} else{
 System.out.println("此学生是"+stu_name+"而不是dragon,不批准其加入.");
}
  return result;
   }
}

6、配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
"http://www.springframework.org/dtd/spring-beans.dtd">
 <beans>
 <bean id="beforeAdvice" class="com.dragon.Advice.BeforeAdvice"></bean>
 <bean id="afterAdvice" class="com.dragon.Advice.AfterAdvice"></bean>
 <bean id="compareInterceptor" class="com.dragon.Advice.CompareInterceptor">
</bean>
 <bean id="studenttarget" class="com.dragon.study.Impl.StudentImpl"></bean>
<bean id="student" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.dragon.study.IStudent</value>
 </property>
<property name="interceptorNames">
<list>
<value>beforeAdvice</value>
<value>afterAdvice</value>
<value>compareInterceptor</value>  
</list>
</property>
<property name="target">
<ref bean="studenttarget"/>
 </property>
</bean>
</beans>

7、测试:Test.java

package com;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.FileSystemXmlApplicationContext;
 import com.dragon.study.IStudent;
public class Test {
 public static void main(String[] args) {
     ApplicationContext ctx = 
new FileSystemXmlApplicationContext("/com/dragon/applicationContext.xml");    
    IStudent person = (IStudent)ctx.getBean("student");
   person.addStudent("dragon");
  }
}