由于最近的服务项目提供接口有一个需求,所有操作都必须检查操作的服务可用,所以感觉Aop特别适合实施。完成学习的小例子。

关于spring-Aop原理:http://m.oschina.net/blog/174838这篇文章写的非常好。

个人觉着可能上线的时候配置文件更方便一下。所以样例主要是配置文件方式

Demo文件下载地址:

http://download.csdn.net/detail/ruishenh/7261121

Spring配置文件

/idle-service-impl/src/main/resources/spring/app-config.xml

<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-3.2.xsd"> <context:component-scanbase-package="com.ruishenh.business.impl" />
<aop:aspectj-autoproxyexpose-proxy="true" proxy-target-class="true"/>
<context:annotation-config/>
<!-- -->
<beanid="springFactoryUtil"class="com.ruishenh.utils.SpringFactoryUtil" />
<!-- -->
<importresource="aop-advisor.xml" />
</beans>

导入的aop-advisor.xml文件

/idle-service-impl/src/main/resources/spring/aop-advisor.xml

<?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"
xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-3.1.xsd"> <bean id="genericAdvisor" class="com.ruishenh.aop.aspect.advisor.GenericAdvisor"/>
<aop:config>
<!-- 定义切面 -->
<aop:aspect ref="genericAdvisor" order="0">
<!-- 定义连接点 -->
<aop:pointcut id="businessService" expression="execution(*com.ruishenh.business.impl..*.*(..))" />
<!-- 定义前置 -->
<aop:before method="before" pointcut-ref="businessService"/>
<!-- 定义围绕 -->
<aop:around method="heartbeat" pointcut-ref="businessService"/>
<!-- 定义后置 -->
<aop:after method="after" pointcut-ref="businessService"/>
<!-- 定义Target处理后普通结果增强 -->
<aop:after-returning method="afterReturning" pointcut-ref="businessService" returning="obj"/>
<!-- 定义Target处理后异常增强 -->
<aop:after-throwing method="handlerException" pointcut-ref="businessService" throwing="e"/> </aop:aspect>
</aop:config>
</beans>

关于这个类

com.ruishenh.aop.aspect.advisor.GenericAdvisor中方法有

 

1.  before  相应Target运行之前

2.  heartbeat这个是围绕的一个方法

3.  after  相应target运行之后

4.  afterReturning 相应在target处理后结果返回增强处理

5.        handlerException 相应在target运行异常时增强处理

/idle-service-impl/src/main/java/com/ruishenh/aop/aspect/advisor/GenericAdvisor.java源代码

package com.ruishenh.aop.aspect.advisor;

import org.aspectj.lang.JoinPoint;
importorg.aspectj.lang.ProceedingJoinPoint; import com.ruishenh.domain.account.AccountBank; public class GenericAdvisor { /**
* 对于要增强的方法。运行围绕处理检查心跳是否正常
* @param joinPoint
* @return
* @throws Throwable
*/
Objectheartbeat(ProceedingJoinPoint joinPoint) throws Throwable{ // if(checkHeartbeat()) {
//
// }
System.out.println("2joinPoint.Signature.name:"+joinPoint.getSignature().getName());
//记得在调用运行方法的时候异常不要try-catch,要thrwos出去,不然可能事务层没法起效,或者增强异常处理也无法起效
Objectobj=joinPoint.proceed();
//下边的參入參数能够改动。可是类型和方法的个数要和原来一致,不然原方法无法运行
// Objectobj=joinPoint.proceed(joinPoint.getArgs()); //对于返回后对象,有可能要做处理,对返回參数的某一些值处理,
//能够通过org.springframework.beans.BeanUtils.copyProperties把一些值赋值
if(obj==null) {
returnnew AccountBank();
}
returnobj;
}
/**
* 对于要增强的方法,在方法之前运行
* @param joinPoint 连接点信息
*/
voidbefore(JoinPoint joinPoint){
Object[] objs=joinPoint.getArgs();
System.out.println("1objs:"+objs[0]);
System.out.println("1joinPoint:"+joinPoint);
}; /**
* 对于要增强的方法,在方法之后运行
* @param joinPoint 连接点信息
*/
voidafter(JoinPoint joinPoint){
Object[] objs=joinPoint.getArgs();
System.out.println("4objs:"+objs[0]);
System.out.println("4joinPoint:"+joinPoint);
};
/**
* 对于要添加的方法,方法返回结果后。对结果进行记录或者分析
* 方法
* @param obj target运行后返回的结果
*/
voidafterReturning(Object obj){
System.out.println("3obj:"+obj);
};
/**
* 对于要增强的方法,方法抛出异常后。对异常的增强处理,比方记录异常。或者依据异常分析数据什么的
* @param e target运行后抛出的异常
*/
voidhandlerException(Throwable e){
System.out.println("handingException......");
e.printStackTrace();
} booleancheckHeartbeat(){
returntrue;
}
}

Junit測试

/idle-service-impl/src/test/java/com/ruishenh/business/impl/account/AccountServiceImplTest.java

package com.ruishenh.business.impl.account;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
importorg.springframework.test.context.ContextConfiguration;
importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner; importcom.ruishenh.domain.account.AccountBank;
importcom.ruishenh.domain.account.AccountBankParam;
import com.ruishenh.utils.SpringFactoryUtil; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:/spring/app-config.xml"})
public class AccountServiceImplTest { @Before
publicvoid setUp() throws Exception {
} @Test
publicvoid testStoreIn() throws Exception {
AccountServiceImplimpl= SpringFactoryUtil.getBean(AccountServiceImpl.class);
AccountBankParamparam= new AccountBankParam();
param.setId(100);
AccountBankab=impl.storeIn(param);
System.out.println(ab.toString());
} }

运行后输出结果:

1objs:com.ruishenh.domain.account.AccountBankParam@d522de2[id=100,name=<null>,account=<null>]

1joinPoint:execution(AccountBankcom.ruishenh.business.impl.account.AccountServiceImpl.storeIn(AccountBankParam))

2joinPoint.Signature.name:storeIn

==============todo=====================

4objs:com.ruishenh.domain.account.AccountBankParam@d522de2[id=100,name=<null>,account=<null>]

4joinPoint:execution(AccountBankcom.ruishenh.business.impl.account.AccountServiceImpl.storeIn(AccountBankParam))

3obj:com.ruishenh.domain.account.AccountBank@2d397e5c[id=0,name=<null>,account=<null>]

com.ruishenh.domain.account.AccountBank@2d397e5c[id=0,name=<null>,account=<null>]

版权声明:本文博主原创文章,博客,未经同意不得转载。

Spring aop 小例子demo的更多相关文章

  1. SSH框架系列:Spring AOP应用记录日志Demo

    分类: [java]2013-12-10 18:53 724人阅读 评论(0) 收藏 举报 1.简介 Spring 中的AOP为Aspect Oriented Programming的缩写,面向切面编 ...

  2. Spring AOP应用实例demo

    AOP(Aspect-Oriented Programming.面向方面编程).能够说是OOP(Object-OrientedPrograming.面向对象编程)的补充和完好.OOP引入封装.继承和多 ...

  3. Spring AOP 学习例子

    http://outofmemory.cn/code-snippet/3762/Spring-AOP-learn-example     工作忙,时间紧,不过事情再多,学习是必须的.记得以前的部门老大 ...

  4. Spring AOP实战例子与springmvc整合不起效果的解决办法

    在使用AOP之前,首先我们先了解一下什么是AOP吧.在网上很多人将AOP翻译为“面向切面编程”,什么是面向切面?与面向对象有什么区别呢? 在回答这两个问题之前,我们先要明白切面的概念. 切面由切点与增 ...

  5. spring Aop的一个demo

    面向切面是什么我就不说了. 上代码: package com.foreveross.service.weixin.test; import java.lang.annotation.Documente ...

  6. spring aop 的一个demo(未完,待完善)

    假设我们有这样的一个场景 : 对于一个类的众多方法,有些方法需要从缓存读取数据,有些则需要直接从数据库读取数据.怎样实现呢? 实现方案有多种.下面我说下常见的几种实现方案 : 1.直接采用spring ...

  7. mybatis 不整合spring 入门小例子

    先上一个搭建完的项目结构截图: 相对比较重要的配置文件有 db.properties , SqlMappingConfig.xml , mapper/User.xml , log4j.properti ...

  8. Spring AOP注解配置demo

    https://blog.csdn.net/yhl_jxy/article/details/78815636#commentBox

  9. c# spring aop的简单例子

    刚刚完成了一个c#的spring aop简单例子,是在mac下用Xamarin Studio开发的.代码如下: 接口 using System; using System.Collections.Ge ...

随机推荐

  1. Git在下搭建下环境的工具

    (本文稿来自:http://www.open-open.com/news/view/55387) Git是一个快速,可扩展的,分布式的版本控制系统.Git服务器起初是专为Linux开发,后来移植至Wi ...

  2. cocos2d-x环境搭建(win7+cocos2d-x-3.0)

    一.环境需准备的材料: 1.VS2012,下载地址:官网 2.cocos2d-x和cocostudio,下载地址:官网 3.eclispe,我用的是:adt-bundle-windows-x86_64 ...

  3. 一个使用Java jdk8中Nashorn(Java javascript引擎)设计的Web开发框架

    地址:https://github.com/iboxdb/hijk 採用给框架开发应用,简单直接.开发效率高 下载后 set PATH to /JAVA 8_HOME/bin jjs build.js ...

  4. Mega的简单使用

    Table of Contents 1 Mega画树的简单应用 2 fas格式文件的准备 3 用生成的.meg画树 4 生出树的处理 4.1 修改内容,添加标注 4.2 导出 4.3 后面随着学习的进 ...

  5. OpenCms创建网站的过程示意图——专用OpenCms人们刚开始学习

    很多人听说过OpenCms,我知道它的强大,只需下载并安装,最后,我们看到了久违OpenCms,我们看到了它的简单的界面,喜悦之后,但难免困惑.如何用这个东西,我如何用它来网站,从哪里开始,无从下手. ...

  6. 潜水 java类加载器ClassLoader

    类加载器(class loader)用于装载 Java 类到 Java 虚拟机中.一般来说.Java 虚拟机使用 Java 类的方式例如以下:Java 源程序(.java 文件)在经过 Java 编译 ...

  7. Facebook Asynchronous Layout and Rending

    Facebook Asynchronous Layout and Rending by 吴雪莹 dispatch_async(backgroundQueue, ^{ storyNode = [[FBS ...

  8. Golang 1.3 发布时间。最终找到地方下载。

    golang 1.3 已发布 但golang.org官方网站被封锁不能下载. 最终找到一个镜像站点. http://golang.so/ http://tip.golang.so/ golang中国的 ...

  9. 重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, RadioButton, CheckBox, ToggleSwitch

    原文:重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, Rad ...

  10. GIT用服务器上的版本强制覆盖掉本地的版本(转)

    以前在机子上使用git下载了linux git版本库上的源代码,由于在阅读源代码的过程中,难免会对本地的文件进行修改,这样就导致本地文件与服务器上的文件版本不一致,我想做的就是用服务器上的版本强制覆盖 ...