现在我们要讲的是第四种AOP实现之注入式AspectJ切面
通过简单的配置就可以实现AOP了。
源码结构:
1、首先我们新建一个接口,love 谈恋爱接口。
package com.spring.aop;
/**
* 谈恋爱接口
*
* @author Administrator
*
*/
public interface Love
{
/*
* 谈恋爱方法
*/
void fallInLove();
}
2、我们写一个Person类实现Love接口
package com.spring.aop;
/**
* 人对象
* @author luwenbin006@163.com
*
*/
public class Person implements Love
{
/*
* 重写谈恋爱方法
* @see com.spring.aop.Love#fallInLove()
*/
public void fallInLove()
{
System.out.println("谈恋爱了...");
}
}
3、下面我们来写aop 注解通知类 分别是配置中的 执行前
执行后 执行前后 执行抛出异常
package com.spring.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
/**
* 注解方式 aop通知类
* @author luwenbin006@163.com
*
*/
public class LoveHelper
{
//在调用方法之前执行 执行拦截包com.spring.aop.*下所有的方法
public void doBefore(JoinPoint point) throws Throwable
{
System.out.println("谈恋爱之前必须要彼此了解!");
}
//在调用方法前后执行
public Object doAround(ProceedingJoinPoint point) throws Throwable
{
if(point.getArgs().length>0)
{
return point.proceed(point.getArgs());
}else
{
return point.proceed();
}
}
//在调用方法之后执行
public void doAfter(JoinPoint point) throws Throwable
{
System.out.println("我们已经谈了5年了,最终还是分手了!");
//System.out.println("我们已经谈了5年了,最终步入了结婚的殿堂!");
}
//当抛出异常时被调用
public void doThrowing(JoinPoint point, Throwable ex)
{
System.out.println("doThrowing::method "
+ point.getTarget().getClass().getName() + "."
+ point.getSignature().getName() + " throw exception");
System.out.println(ex.getMessage());
}
}
4、配置好application.xml 配置好AOP注入模式和相应的bean aop通知类。
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 配置bean -->
<bean id="person" class="com.spring.aop.Person">
</bean>
<!-- 配置通知方法类 -->
<bean id="loveHelper" class="com.spring.aop.LoveHelper">
</bean>
<aop:config>
<aop:aspect id="configAspect" ref="loveHelper">
<!--配置com.spring.aop包下所有类或接口的所有方法 -->
<aop:pointcut id="loveServices" expression="execution(* com.spring.aop.*..*(..))" />
<aop:before pointcut-ref="loveServices" method="doBefore" />
<aop:after pointcut-ref="loveServices" method="doAfter" />
<aop:around pointcut-ref="loveServices" method="doAround" />
<aop:after-throwing pointcut-ref="loveServices"
method="doThrowing" throwing="ex" />
</aop:aspect>
</aop:config>
</beans>
5、通过测试类测试下我们的代码~~~
package com.spring.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.aop.Love;
/**
* aop测试方法类
* @author luwenbin006@163.com
*
*/
public class LoveTest
{
public static void main(String[] args)
{
//加载spring配置文件
ApplicationContext appCtx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
//得到person对象
Love love = (Love) appCtx.getBean("person");
//调用谈恋爱方法
love.fallInLove();
}
}
6、看控制台输出结果 调用了我们定义的aop拦截方法~~~ ok了
7、源码下载地址:源码下载