这里介绍spring-aop使用方式二,spring配置文件的方式来使用AOP。
spring-applicationContext.xml 部分代码配制如下:
- <!-- component scan -->
- <context:component-scan base-package="com.lw.rpc"
- use-default-filters="false">
- <context:include-filter type="annotation"
- expression="org.springframework.stereotype.Repository" />
- <context:include-filter type="annotation"
- expression="org.springframework.stereotype.Service" />
- </context:component-scan>
- <!-- 声明通知类 -->
- <bean id="aspectJAdviceAop" class="com.lw.rpc.test.AspectJAdviceAop" />
- <aop:config>
- <aop:aspect id="useAapect" ref="aspectJAdviceAop" >
- <!-- 配置切点 -->
- <aop:pointcut expression="execution(* com.lw.rpc..*.*(..))" id="point_cut"/>
- <!-- 前置通知 -->
- <aop:before method="beforeAdvice" pointcut-ref="point_cut"/>
- <!-- <aop:pointcut id="except_add" expression="execution(* com.lw.rpc..*.*(..)))" /> -->
- <!--后置通知 returning指定返回参数 ,如果不设置这个返回参数,你可以亲自试一下,看看是什么效果-->
- <!-- <aop:after-returning method="afterAdvice" pointcut-ref="point_cut" returning="result" /> -->
- <!--环绕 -->
- <!-- <aop:around method="aroundAdvice" pointcut-ref="point_cut"/> -->
- <!--throw中 -->
- <!-- <aop:after-throwing method="afterThrow" pointcut-ref="point_cut" throwing="e"/> -->
- </aop:aspect>
- </aop:config>
通知类如下,该类里面实现那四种方式的AOP拦截。
package com.lw.rpc.test;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class AspectJAdviceAop {
/**
* 前置通知
*
* @param jp
*/
public void beforeAdvice(JoinPoint jp) {
System.out.println(" 进入连接点前 +++++++++++++++ ");
System.out.println(jp.getTarget().getClass() + "对象正在用这个");
System.out.println(jp.getArgs()[0] + "test");
System.out.println(" 进入到连接点点方法 ");
}
/**
* 后置通知
*
* @param jp
*/
public void afterAdvice(JoinPoint jp, String result) {
System.out.println(" 连接点方法执行完成后 +++++++++++++++ ");
System.out.println(jp.getTarget().getClass() + "对象正在用这个");
System.out.println(jp.getSignature().getName() + "方法");
System.out.println("结果:" + result);
}
/**
* 环绕通知
*
* @param jp
* @param result
* @throws Throwable
*/
public void aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
System.out.println(" 进入到环绕通知 +++++++++++++++ ");
// 调用方法的参数
Object[] args = pjp.getArgs();
// 调用的方法名
String method = pjp.getSignature().getName();
// 获取目标对象
Object target = pjp.getTarget();
// 执行完方法的返回值:调用proceed()方法,就会触发切入点方法执行
Object result = pjp.proceed();
System.out.println("调用方法结束:之后执行!\n");
System.out.println("输出:" + args[0] + ";" + method + ";" + target + ";" + result + "\n");
}
/**
* 异常通知
*
* @param jp
* @param e
*/
public void afterThrow(JoinPoint jp, Throwable e) {
System.out.println(" 异常通知 ");
}
}
//目标类,对哪个目标进行下手。
package com.lw.rpc.service;
import com.lw.rpc.model.SpiderOrder;
/**
* 提供数据service
*
* @author
*
*/
public interface SpiderOrderService {
/**
* 生成订单信息
* @param spiderOrder
* @return
*/
public int saveSpiderOrder(SpiderOrder spiderOrder);
/**
* 修改订单信息
* @param spiderOrder
*/
public void updateSpiderOrder(SpiderOrder spiderOrder);
/**
* 删除订单信息
* @param spiderId
*/
public void deleteSpiderOrderById(Long spiderId);
}
package com.lw.rpc.service.impl;
import org.springframework.stereotype.Service;
import com.lw.rpc.model.SpiderOrder;
import com.lw.rpc.service.SpiderOrderService;
/**
* 订单实现类
*
* @author
*
*/
@Service("spiderOrderService")
public class SpiderOrderServiceImpl implements SpiderOrderService{
/**
* 生成订单
*/
public int saveSpiderOrder(SpiderOrder spiderOrder) {
//具体的业务逻辑
System.out.println("保存方法");
return 0;
}
@Override
public void updateSpiderOrder(SpiderOrder spiderOrder) {
// TODO Auto-generated method stub
}
@Override
public void deleteSpiderOrderById(Long spiderId) {
// TODO Auto-generated method stub
}
}
测试方法:
package com.lw.rpc.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lw.rpc.model.SpiderOrder;
import com.lw.rpc.service.SpiderOrderService;
public class SpringAopTest {
public static void main(String[] args) {
ApplicationContext cxt = new ClassPathXmlApplicationContext("/spring/spring-applicationContext.xml");
SpiderOrderService spiderOrderService = (SpiderOrderService) cxt.getBean("spiderOrderService");
spiderOrderService.saveSpiderOrder(new SpiderOrder());
}
}
输出如下:
[main] INFO org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org[email protected]5d6f64b1: startup date [Sun Apr 16 23:28:47 CST 2017]; root of context hierarchy
[main] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [spring/spring-applicationContext.xml]
[main] INFO org.springframework.beans.factory.config.PropertyPlaceholderConfigurer - Loading properties file from class path resource [properties/datasource/init.properties]
[main] INFO org.springframework.beans.factory.config.PropertyPlaceholderConfigurer - Loading properties file from class path resource [properties/redis/redis-conf.properties]
[main] INFO org.springframework.beans.factory.config.PropertyPlaceholderConfigurer - Loading properties file from class path resource [properties/server/server-config.properties]
进入连接点前 +++++++++++++++
class com.lw.rpc.service.impl.SpiderOrderServiceImpl对象正在用这个
com.lw.rpc.model.Sp[email protected]
进入到连接点点方法
保存方法