一个简单的Spring的AOP例子 (成功运行,转载)

      经过这段日子的学习和使用Spring,慢慢地体会到Spring的优妙之处,正在深入地吸收Spring的精华,呵呵。现在写的这个只是个简单AOP例子,包括前置通知,后置通知,环绕通知,和目标对象。写这个例子的主要目标只是想让想学AOP的能更快地入门,了解一下如何去配置AOP里面的东东。
目标对象的接口:IStudent.java

package com.test.logging;

public interface IStudent {

 public void addStudent(String name);

}

目标类:StudentImpl.java

package com.test.logging;

public class StudentImpl implements IStudent {

 public void addStudent(String name) {
  System.out.println(" 欢迎  " + name + "  你加入Spring家庭! ");
 }
}
前置通知:BeforeAdvice.java

package com.test.logging;

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方法. ");
 }
}

后置通知:AfterAdvice.java

package com.test.logging;

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方法.");

 }
}

环绕通知:CompareInterceptor.java

package com.test.logging;

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()[0].toString();
  if (stu_name.equals("dragon")) {
   result = invocation.proceed();
  } else {
   System.out.println("此学生是" + stu_name + "而不是dragon,不批准其加入.");
  }

  return result;
 }
}

配置文件applicationContext.xml

 <bean id="beforeAdvice" class="com.test.logging.beforeAdvice"></bean>
 <bean id="afterAdvice" class="com.test.logging.afterAdvice"></bean>
 <bean id="compareInterceptor" class="com.test.logging.compareInterceptor"></bean>
 <bean id="studenttarget" class="com.test.logging.StudentImpl"></bean>

 <bean id="student" class="org.springframework.aop.framework.ProxyFactoryBean">
  <property name="proxyInterfaces">
   <value>com.test.logging.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>
  现在开始写测试类,Test.java

package com.test.logging;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class test {

 public static void main(String[] args) {
  ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
  IStudent person = (IStudent) ctx.getBean("student");
  person.addStudent("dragon");
 }
}


 

原文地址:http://www.blogjava.net/javadragon/archive/2006/12/03/85115.html

好的,以下是一个使用Spring注解实现AOP例子: 首先,在你的Spring配置文件中,需要开启AOP支持: ``` <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" /> ``` 然后,创建一个切面类,使用@Aspect注解标注: ``` @Aspect public class LoggingAspect { @Before("execution(* com.example.service.*.*(..))") public void logBefore(JoinPoint joinPoint) { System.out.println("Before executing " + joinPoint.getSignature().getName()); } @AfterReturning(pointcut = "execution(* com.example.service.*.*(..))", returning = "result") public void logAfterReturning(JoinPoint joinPoint, Object result) { System.out.println("After executing " + joinPoint.getSignature().getName() + ", result is " + result); } @AfterThrowing(pointcut = "execution(* com.example.service.*.*(..))", throwing = "exception") public void logAfterThrowing(JoinPoint joinPoint, Exception exception) { System.out.println("Exception thrown from " + joinPoint.getSignature().getName() + ", exception is " + exception); } } ``` 在上面的例子中,我们定义了三个通知,分别在方法执行前、执行后和抛出异常时执行。我们使用@Pointcut注解定义了一个切点,它匹配com.example.service包中的所有方法。然后,在通知方法上使用@Before、@AfterReturning和@AfterThrowing注解,分别表示在方法执行前、执行后和抛出异常时执行。 最后,在你的服务类中,使用@Loggable注解标注需要记录日志的方法: ``` @Service public class MyService { @Loggable public void doSomething() { // ... } } ``` 这样,在执行doSomething方法时,LoggingAspect中定义的通知方法就会被自动调用,从而实现了AOP的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值