一、基本概念
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