Spring AOP 两种注入方式

第一种是基于annotation方式注入

(1)定义一个Aspect对象LogBeforeAdvice

package org.aop;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class LogBeforeAdvice {
private Logger logger = Logger.getLogger(this.getClass().getName());
@Pointcut("execution(* org.aop.IHello.*(..))")
private void logging(){}

@Before("logging()")
public void before(JoinPoint joinPoint){
  logger.log(Level.INFO,"method start ..." + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
}

@AfterReturning(pointcut="logging()" ,returning="retVal")
public void afterReturning(JoinPoint joinPoint ,Object retVal){
  logger.log(Level.INFO,"method end ..." + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
}
}



(2)定义一个接口类IHello

package org.aop;

public interface IHello {
public void hello(String hello);
}
(3)定义一个实现类HelloSpeaker

package org.aop;

public class HelloSpeaker implements IHello {

public void hello(String hello) {
  System.out.println("你好! " + hello);
}

}
(4)配置Spring配置文件

<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<bean id="logBeforeAdvice" class="org.aop.LogBeforeAdvice"></bean>
<bean id="helloSpeaker" class="org.aop.HelloSpeaker"></bean>
  <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

最后编写测试类

package org.aop;

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

public class AOPTest {

/**
  * @param args
  */
public static void main(String[] args) {
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  IHello hello = (IHello) context.getBean("helloSpeaker");
  hello.hello("陈胜尊");
}

}
测试结果为

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
2011-4-26 21:32:37 org.aop.LogBeforeAdvice before
信息: method start ...org.aop.IHello.hello
你好! 陈胜尊
2011-4-26 21:32:37 org.aop.LogBeforeAdvice afterReturning
信息: method end ...org.aop.IHello.hello


第二种方式是基于XML方式注册

(1)把Aspect类修改为

package org.aop;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.aspectj.lang.JoinPoint;

public class LogBeforeAdvice {
private Logger logger = Logger.getLogger(this.getClass().getName());

public void before(JoinPoint joinPoint) {
  logger.log(Level.INFO, "method start ..."
    + joinPoint.getSignature().getDeclaringTypeName() + "."
    + joinPoint.getSignature().getName());
}

public void afterReturning(JoinPoint joinPoint) {
  logger.log(Level.INFO, "method end ..."
    + joinPoint.getSignature().getDeclaringTypeName() + "."
    + joinPoint.getSignature().getName());
}
}

(2)修改配置文件为

  <aop:config>
  <aop:aspect id="loggin" ref="logBeforeAdvice">
   <aop:pointcut id="helloLog" expression="execution(* org.aop.IHello.*(**))"/>
   <aop:before
    pointcut-ref="helloLog"
    method="before"
   />
   <aop:after-returning
   pointcut-ref="helloLog"
   method="afterReturning"
   />
  </aop:aspect>
</aop:config >

执行结果是一样的

猜你喜欢

转载自chenshengzun.iteye.com/blog/1164540