Spring-AOP学习

一、基本概念

a)        joinPoint:切入点

b)        Advice:特定的Jointpoint处运行的代码,对于SpringAOP 来讲,有Before advice、AfterreturningAdvice、ThrowAdvice、AroundAdvice(MethodInteceptor)等。 

c)        Pointcut:一组切入点

d)        Aspect: Pointcut+ Advice

e)        Weaving:把切面加入程序的过程,spring中位proxFactory

f)         Target:需要切面的对象

g)        Introduction:

h)        切入类型:静态切入(AspectJ)和动态切入(spring aop)


二、入门实例

应用是Spring 2.0 的APO, 它引入了一种更加简单并且更强大的方式来定义切面。

涉及Jar包

spring.jar

aspectjrt-1.2.1.jar

aspectjweaver.jar

cglib-nodep-2.2.2.jar

直接贴代码,原理就不讲了,主要也是给自己备注用的。。

 

类Male

package spring.aop;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
public class Male {
    Log log = LogFactory.getLog(Male.class);
    public void talk(){
       log.info("i'm male");
    }
}

类Female

package spring.aop;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
public class Female {
    Log log = LogFactory.getLog(Female.class);
   
    public void talk(){
       log.info("i'm femal");
    }
}

类SexAspect

package spring.aop;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
/**
 * 提供切面服务类
 * @author Administrator
 *
 */
@Aspect
public class SexAspect {
    private Log log = LogFactory.getLog(SexAspect.class);
 
    @Pointcut("execution(*spring.aop.*.talk*())")
    public void simplePointcut() {
    }
 
    @AfterReturning(pointcut = "simplePointcut()")
    public void hello() {
       log.info("this is SexAspect");
    }
 
}

运行类Run

package spring.aop;
 
import spring.util.BeanUtil;
 
/**
 * 第一个spring2的aop简单应用例子
 * @author Administrator
 *
 */
public class Run {
    public static void main(String[] args){
       //获取bean
       Female female = (Female)BeanUtil.getBean("Female");
       //执行方法
       female.talk();
       Male male = (Male)BeanUtil.getBean("Male");
       male.talk();
    }
}


配置文件

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
       http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
 
 
    <aop:aspectj-autoproxy/>
    <bean id="Female" class="spring.aop.Female"/>
 
    <bean id="Male" class="spring.aop.Male"/>
    <bean id="SexAspect" class="spring.aop.SexAspect"/>
 
</beans>

获取spring代理bean的工厂类

package spring.util;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
/**
 * 用户获取spring的ioc容器里的bean对象
 * @author Administrator
 *
 */
public class BeanUtil {
   
    private static BeanFactory beanFactory = null;
    private static final Log log = LogFactory.getLog(BeanUtil.class);
     
    static{   
       final String FILE = "spring/config/spring.xml";
       try{
          beanFactory = newClassPathXmlApplicationContext(FILE);//方法1
//        Resource resource = newInputStreamResource(new FileInputStream(FILE)));//方法2
//        beanFactory = newXmlBeanFactory(resource);
         
       }catch(Throwable ex){
           ex.printStackTrace();
            final String MSG = "导入配置文件失败:" + FILE;
            log.error(MSG);       
           throw new Error(MSG ,ex );
       }
    }
   
    private BeanUtil(){
      
    }
   
     
    /**
     * 获取Bean对象
     * @param pBeanID
     * @return
     */
    public static Object getBean(String pBeanID) {
       return beanFactory.getBean(pBeanID);
    }
   
    /**
     * Utilize the Java 5 generic type to eliminate the cast
     * @param <T>
     * @param beanID
     * @param clazz
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String beanID, Class<T> clazz){
       return (T)beanFactory.getBean(beanID);
    }
   
 
}
 


So,跑一下吧,结果如下,顺利完成aop入门~

 

2012-7-12 13:30:01spring.aop.Female talk

INFO: i'm femal

2012-7-12 13:30:01spring.aop.SexAspect hello

INFO: this is SexAspect

2012-7-12 13:30:01spring.aop.Male talk

INFO: i'm male

2012-7-12 13:30:01spring.aop.SexAspect hello

INFO: this is SexAspect




 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值