项目学生:使用AOP简化代码

这是Project Student的一部分。

许多人坚信方法应适合您的编辑器窗口(例如20行),而有些人则认为方法应小于此范围。 这个想法是一种方法应该做一件事,而只能做一件事。 如果它做的还不止于此,则应将其分解为多种方法,而旧方法的“一件事”就是协调新方法。

这并不意味着在任意数量的行之后拆分一种方法。 有时方法自然会更大。 仍然是一个很好的问题。

那么,如何识别不只一件事的代码? 一个好的试金石是代码是否在多种方法中重复。 典型的例子是持久性类中的事务管理。 每个持久性类都需要它,并且代码始终看起来相同。

另一个示例是Resource类中未处理的异常处理程序。 每个面向REST的方法都需要处理此问题,并且代码始终看起来相同。

那是理论。 在实践中,代码可能很丑陋并且收益不大。 幸运的是,有一个解决方案:面向方面的编程(AOP)。 这使我们可以在方法调用之前或之后透明地编织代码。 这通常使我们可以大大简化我们的方法。

设计决策

AspectJ –我正在通过Spring注入来使用AspectJ。

局限性

使用CRUD方法,AspectJ切入点表达式相对简单。 当添加了更复杂的功能时,情况可能并非如此。

资源方法中未处理的异常

我们首先关心的是资源方法中未处理的异常。 不管怎样,Jersey都会返回SERVER INTERNAL ERROR(服务器内部错误)(500)消息,但是它可能包含堆栈跟踪信息和我们不希望攻击者知道的其他内容。 如果我们自己发送它,我们可以控制它包含的内容。 我们可以在所有方法中添加一个“ catch”块,但可以将其复制到AOP方法中。 这将使我们所有的Resource方法更加苗条和易于阅读。

此类还检查“找不到对象”异常。 在单个Resource类中将很容易处理,但会使代码混乱。 将异常处理程序放在此处可使我们的方法专注于快乐路径并保证响应的一致性。

该类有两个优化。 首先,它显式检查UnitTestException并在这种情况下跳过详细的日志记录。 我最大的烦恼之一是测试,当一切都按预期方式运行时,将堆栈跟踪信息充斥日志。 这使得不可能针对明显的问题浏览日志。 单个更改可以使问题更容易发现。

其次,它使用与目标类(例如CourseResource)关联的记录器,而不是与AOP类关联的记录器。 除了更清晰之外,这还使我们可以有选择地更改单个Resource(而不是全部)的日志记录级别。

另一个技巧是在处理程序中调用ExceptionService 。 该服务可以对异常做一些有用的事情,例如,它可以创建或更新Jira票证。 这还没有实现,所以我只是发表评论以说明它的去向。

@Aspect
@Component
public class UnexpectedResourceExceptionHandler {@Around("target(com.invariantproperties.sandbox.student.webservice.server.rest.AbstractResource)")public Object checkForUnhandledException(ProceedingJoinPoint pjp) throws Throwable {Object results = null;Logger log = Logger.getLogger(pjp.getSignature().getClass());try {results = pjp.proceed(pjp.getArgs());} catch (ObjectNotFoundException e) {// this is safe to log since we know that we've passed filtering.String args = Arrays.toString(pjp.getArgs());results = Response.status(Status.NOT_FOUND).entity("object not found: " + args).build();if (log.isDebugEnabled()) {log.debug("object not found: " + args);}} catch (Exception e) {// find the method we called. We can't cache this since the method// may be overloadedMethod method = findMethod(pjp); if ((method != null) && Response.class.isAssignableFrom(method.getReturnType())) {// if the method returns a response we can return a 500 message.if (!(e instanceof UnitTestException)) {if (log.isInfoEnabled()) {log.info(String.format("%s(): unhandled exception: %s", pjp.getSignature().getName(),e.getMessage()), e);}} else if (log.isTraceEnabled()) {log.info("unit test exception: " + e.getMessage());}results = Response.status(Status.INTERNAL_SERVER_ERROR).build();} else {// DO NOT LOG THE EXCEPTION. That just clutters the log - let// the final handler log it.throw e;}}return results;}/*** Find method called via reflection.*/Method findMethod(ProceedingJoinPoint pjp) {Class[] argtypes = new Class[pjp.getArgs().length];for (int i = 0; i < argtypes.length; i++) {argtypes[i] = pjp.getArgs()[i].getClass();}Method method = null;try {// @SuppressWarnings("unchecked")method = pjp.getSignature().getDeclaringType().getMethod(pjp.getSignature().getName(), argtypes);} catch (Exception e) {Logger.getLogger(UnexpectedResourceExceptionHandler.class).info(String.format("could not find method for %s.%s", pjp.getSignature().getDeclaringType().getName(),pjp.getSignature().getName()));}return method;}
}

REST发布值检查

我们的Resource方法也有很多样板代码来检查REST参数。 它们是否为非空,电子邮件地址的格式是否正确,等等。同样,很容易将大部分代码移入AOP方法并简化Resource方法。

我们首先定义一个接口,该接口指示可以验证REST传输对象。 第一个版本使我们可以简单地接受或拒绝,改进的版本可以使我们有办法告诉客户具体问题是什么。

public interface Validatable {boolean validate();
}

现在,我们扩展了先前的REST传输对象,以添加一种验证方法。

两个笔记。 首先,名称和电子邮件地址接受Unicode字母,而不仅仅是标准ASCII字母。 随着我们的世界国际化,这一点很重要。

其次,我添加了一个toString()方法,但是由于它使用了未经处理的值,因此这是不安全的。 我将在稍后处理消毒。

@XmlRootElement
public class NameAndEmailAddressRTO implements Validatable {// names must be alphabetic, an apostrophe, a dash or a space. (Anne-Marie,// O'Brien). This pattern should accept non-Latin characters.// digits and colon are added to aid testing. Unlikely but possible in real// names.private static final Pattern NAME_PATTERN = Pattern.compile("^[\\p{L}\\p{Digit}' :-]+$");// email address must be well-formed. This pattern should accept non-Latin// characters.private static final Pattern EMAIL_PATTERN = Pattern.compile("^[^@]+@([\\p{L}\\p{Digit}-]+\\.)?[\\p{L}]+");private String name;private String emailAddress;private String testUuid;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getEmailAddress() {return emailAddress;}public void setEmailAddress(String emailAddress) {this.emailAddress = emailAddress;}public String getTestUuid() {return testUuid;}public void setTestUuid(String testUuid) {this.testUuid = testUuid;}/*** Validate values.*/@Overridepublic boolean validate() {if ((name == null) || !NAME_PATTERN.matcher(name).matches()) {return false;}if ((emailAddress == null) || !EMAIL_PATTERN.matcher(emailAddress).matches()) {return false;}if ((testUuid != null) && !StudentUtil.isPossibleUuid(testUuid)) {return false;}return true;}@Overridepublic String toString() {// FIXME: this is unsafe!return String.format("NameAndEmailAddress('%s', '%s', %s)", name, emailAddress, testUuid);}
}

我们对其他REST传输对象进行了类似的更改。

现在,我们可以编写AOP方法来检查CRUD操作的参数。 和以前一样,使用与资源关联的记录器而不是AOP类来写入日志。

这些方法还记录Resource方法的条目。 同样,它是样板,在此进行简化了Resource方法。 记录该方法的退出和运行时间也很简单,但是在这种情况下,我们应该使用一个股票记录器AOP类。

@Aspect
@Component
public class CheckPostValues {/*** Check post values on create method.* * @param pjp* @return* @throws Throwable*/@Around("target(com.invariantproperties.sandbox.student.webservice.server.rest.AbstractResource) && args(validatable,..)")public Object checkParametersCreate(ProceedingJoinPoint pjp, Validatable rto) throws Throwable {final Logger log = Logger.getLogger(pjp.getSignature().getDeclaringType());final String name = pjp.getSignature().getName();Object results = null;if (rto.validate()) {// this should be safe since parameters have been validated.if (log.isDebugEnabled()) {log.debug(String.format("%s(%s): entry", name, Arrays.toString(pjp.getArgs())));}results = pjp.proceed(pjp.getArgs());} else {// FIXME: this is unsafeif (log.isInfoEnabled()) {log.info(String.format("%s(%s): bad arguments", name, Arrays.toString(pjp.getArgs())));}// TODO: tell caller what the problems wereresults = Response.status(Status.BAD_REQUEST).build();}return results;}/*** Check post values on update method.* * @param pjp* @return* @throws Throwable*/@Around("target(com.invariantproperties.sandbox.student.webservice.server.rest.AbstractResource) && args(uuid,validatable,..)")public Object checkParametersUpdate(ProceedingJoinPoint pjp, String uuid, Validatable rto) throws Throwable {final Logger log = Logger.getLogger(pjp.getSignature().getDeclaringType());final String name = pjp.getSignature().getName();Object results = null;if (!StudentUtil.isPossibleUuid(uuid)) {// this is a possible attack.if (log.isInfoEnabled()) {log.info(String.format("%s(): uuid", name));}results = Response.status(Status.BAD_REQUEST).build();} else if (rto.validate()) {// this should be safe since parameters have been validated.if (log.isDebugEnabled()) {log.debug(String.format("%s(%s): entry", name, Arrays.toString(pjp.getArgs())));}results = pjp.proceed(pjp.getArgs());} else {// FIXME: this is unsafeif (log.isInfoEnabled()) {log.info(String.format("%s(%s): bad arguments", name, Arrays.toString(pjp.getArgs())));}// TODO: tell caller what the problems wereresults = Response.status(Status.BAD_REQUEST).build();}return results;}/*** Check post values on delete method. This is actually a no-op but it* allows us to log method entry.* * @param pjp* @return* @throws Throwable*/@Around("target(com.invariantproperties.sandbox.student.webservice.server.rest.AbstractResource) && args(uuid,version) && execution(* *.delete*(..))")public Object checkParametersDelete(ProceedingJoinPoint pjp, String uuid, Integer version) throws Throwable {final Logger log = Logger.getLogger(pjp.getSignature().getDeclaringType());final String name = pjp.getSignature().getName();Object results = null;if (!StudentUtil.isPossibleUuid(uuid)) {// this is a possible attack.if (log.isInfoEnabled()) {log.info(String.format("%s(): uuid", name));}results = Response.status(Status.BAD_REQUEST).build();} else {// this should be safe since parameters have been validated.if (log.isDebugEnabled()) {log.debug(String.format("%s(%s): entry", name, Arrays.toString(pjp.getArgs())));}results = pjp.proceed(pjp.getArgs());}return results;}/*** Check post values on find methods. This is actually a no-op but it allows* us to log method entry.* * @param pjp* @return* @throws Throwable*/@Around("target(com.invariantproperties.sandbox.student.webservice.server.rest.AbstractResource) && execution(* *.find*(..))")public Object checkParametersFind(ProceedingJoinPoint pjp) throws Throwable {final Logger log = Logger.getLogger(pjp.getSignature().getDeclaringType());if (log.isDebugEnabled()) {log.debug(String.format("%s(%s): entry", pjp.getSignature().getName(), Arrays.toString(pjp.getArgs())));}final Object results = pjp.proceed(pjp.getArgs());return results;}
}

更新了Spring配置

我们必须告诉Spring搜索AOP类。 这是对我们的配置文件的单行更改。

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><aop:aspectj-autoproxy/>
</beans>

更新资源

现在,我们可以简化资源类。 仅有几种方法可以简化为幸福道路。

@Service
@Path("/course")
public class CourseResource extends AbstractResource {private static final Logger LOG = Logger.getLogger(CourseResource.class);private static final Course[] EMPTY_COURSE_ARRAY = new Course[0];@Resourceprivate CourseFinderService finder;@Resourceprivate CourseManagerService manager;@Resourceprivate TestRunService testRunService;/*** Default constructor.*/public CourseResource() {}/*** Set values used in unit tests. (Required due to AOP)* * @param finder* @param manager* @param testService*/void setServices(CourseFinderService finder, CourseManagerService manager, TestRunService testRunService) {this.finder = finder;this.manager = manager;this.testRunService = testRunService;}/*** Get all Courses.* * @return*/@GET@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })public Response findAllCourses() {final List courses = finder.findAllCourses();final List results = new ArrayList(courses.size());for (Course course : courses) {results.add(scrubCourse(course));}final Response response = Response.ok(results.toArray(EMPTY_COURSE_ARRAY)).build();return response;}/*** Create a Course.* * FIXME: what about uniqueness violations?* * @param req* @return*/@POST@Consumes({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })public Response createCourse(CourseInfo req) {final String code = req.getCode();final String name = req.getName();Response response = null;Course course = null;if (req.getTestUuid() != null) {TestRun testRun = testRunService.findTestRunByUuid(req.getTestUuid());if (testRun != null) {course = manager.createCourseForTesting(code, name, req.getSummary(), req.getDescription(),req.getCreditHours(), testRun);} else {response = Response.status(Status.BAD_REQUEST).entity("unknown test UUID").build();}} else {course = manager.createCourse(code, name, req.getSummary(), req.getDescription(), req.getCreditHours());}if (course == null) {response = Response.status(Status.INTERNAL_SERVER_ERROR).build();} else {response = Response.created(URI.create(course.getUuid())).entity(scrubCourse(course)).build();}return response;}/*** Get a specific Course.* * @param uuid* @return*/@Path("/{courseId}")@GET@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })public Response getCourse(@PathParam("courseId") String id) {// 'object not found' handled by AOPCourse course = finder.findCourseByUuid(id);final Response response = Response.ok(scrubCourse(course)).build();return response;}/*** Update a Course.* * FIXME: what about uniqueness violations?* * @param id* @param req* @return*/@Path("/{courseId}")@POST@Consumes({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })public Response updateCourse(@PathParam("courseId") String id, CourseInfo req) {final String name = req.getName();// 'object not found' handled by AOPfinal Course course = finder.findCourseByUuid(id);final Course updatedCourse = manager.updateCourse(course, name, req.getSummary(), req.getDescription(),req.getCreditHours());final Response response = Response.ok(scrubCourse(updatedCourse)).build();return response;}/*** Delete a Course.* * @param id* @return*/@Path("/{courseId}")@DELETEpublic Response deleteCourse(@PathParam("courseId") String id, @PathParam("version") Integer version) {// we don't use AOP handler since it's okay for there to be no matchtry {manager.deleteCourse(id, version);} catch (ObjectNotFoundException exception) {LOG.debug("course not found: " + id);}final Response response = Response.noContent().build();return response;}
}

单元测试

单元测试需要对每个测试进行更改,因为我们不能简单地实例化被测试的对象–我们必须使用Spring,以便正确编织AOP类。 幸运的是,这实际上是唯一的更改–我们检索资源并通过package-private方法而不是package-private构造函数设置服务。

我们还需要为服务bean创建Spring值。 配置器类负责此工作。

@Configuration
@ComponentScan(basePackages = { "com.invariantproperties.sandbox.student.webservice.server.rest" })
@ImportResource({ "classpath:applicationContext-rest.xml" })
// @PropertySource("classpath:application.properties")
public class TestRestApplicationContext1 {@Beanpublic CourseFinderService courseFinderService() {return null;}@Beanpublic CourseManagerService courseManagerService() {return null;}....

整合测试

集成测试不需要任何更改。

源代码

  • 源代码位于https://github.com/beargiles/project-student [github]和http://beargiles.github.io/project-student/ [github页面]。

参考: 项目学生:来自Invariant Properties博客的JCG合作伙伴 Bear Giles 使用AOP简化代码 。

翻译自: https://www.javacodegeeks.com/2014/01/project-student-simplifying-code-with-aop.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/364622.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

洛谷 P3367 【模板】并查集

嗯... 题目链接&#xff1a;https://www.luogu.org/problemnew/show/P3367 并查集可以支持的操作&#xff1a;“并”和“查”。然后这道题主要就是考察这两种操作。将每一个点的“父亲”初始化为自己&#xff0c;然后分别进行“并”和“查”。 “并”&#xff1a;用递归函数fin…

MySQL使用学习使用,mysql学习--基本使用_MySQL

一旦安装完成&#xff0c;MySQL 服务器应该自动启动。sudo start mysql #手动的话这样启动sudo stop mysql #手动停止当你修改了配置文件后&#xff0c;你需要重启 mysqld 才能使这些修改生效。要想检查 mysqld 进程是否已经开启&#xff0c;可以使用下面的命令&#xff1a;pgr…

解决@vue/cli 创建项目是安装chromedriver时失败的问题

最近在使用新版vue的命令行工具创建项目时&#xff0c;安装chromedriver老是失败&#xff0c;导致后面的步骤也没有进行。网上搜索了一下&#xff0c;全是使用 npm install chromedriver --chromedriver_cdnurlhttp://cdn.npm.taobao.org/dist/chromedriver 安装的&#xff0c…

Java 8 Friday Goodies:Lambda和排序

在Data Geekery &#xff0c;我们喜欢Java。 而且&#xff0c;由于我们真的很喜欢jOOQ的流畅的API和查询DSL &#xff0c;我们对Java 8将为我们的生态系统带来什么感到非常兴奋。 我们已经写了一些关于Java 8好东西的博客 &#xff0c;现在我们觉得是时候开始一个新的博客系列了…

npm 包下载的各种姿势

最近在写Node程序的时候&#xff0c;突然对 npm install 的-save和-save-dev 这两个参数的使用比较混乱。其实博主在这之前对这两个参数的理解也是模糊的&#xff0c;各种查资料和实践后对它们之间的异同点略有理解。遂写下这篇文章避免自己忘记&#xff0c;同时也给node猿友一…

39.数组中数值和下标相等的元素

题目描述&#xff1a; 假设一个单调递增的数组里的每个元素都是整数且是唯一的&#xff0c;请编程实现一个函数&#xff0c;找出数组中任意一个数值等于其下标的元素&#xff0c;例如&#xff0c;在数组{-3&#xff0c;-1,1,3,5}中数字3和它的下标相同。 思路分析&#xff1a; …

php读取xml标签内容,从xml php5获取内容

我正在研究一个支付解决方案,需要一些关于php的帮助。我正在做一个httprequest,作为回应,我将得到一些xml。XML可能如下所示:1description121510authurlsettleurl基本上我想做的是从标签中获取内容并将其保存为字符串。我试过这个:$order <?xml version"1.0" en…

jquery3和layui冲突导,致使用layui.layer.full弹出全屏iframe窗口时高度152px问题

项目中使用的jquery版本是jquery-3.2.1&#xff0c;在使用layui弹出全屏iframe窗口时&#xff0c;iframe窗口顶部总是出现一个152px高的滚动窗口无法实现真正全屏&#xff0c;代码如下&#xff1a; <!DOCTYPE html> <html> <head><meta charset"utf-…

ADF Faces。 立即的自定义客户端事件

在本文中&#xff0c;我将重点介绍ADF Faces Javascript API方法以从客户端触发自定义事件。 例如&#xff1a; function cliListener(actionEvent) {AdfCustomEvent.queue(actionEvent.getSource(), "servListener",null, true);}我们可以使用af&#xff1a;client…

react-native页面间传递数据的几种方式

1. 利用react-native 事件DeviceEventEmitter 监听广播 应用场景&#xff1a; - 表单提交页面&#xff0c; A页面跳转到B页面选人&#xff0c; 然后返回A页面&#xff0c; 需要将B页面选择的数据传回A页面。 - 多个多媒体来回切换播放&#xff0c;暂停后二次继续播放等问题。…

php数据库操作类的调用优化,PHP PDO优化数据库操作类 多数据库驱动类

就是做一下整理 PHP PDO类操作。简化操作流程更多内容http://git.oschina.net/youkuiyuan/yky_test/blob/master/class/pdo.class.php点击链接加入群【微信开发探讨群】&#xff1a;http://jq.qq.com/?_wv1027&kcsNcd9群号&#xff1a;330393916欢迎浏览&#xff1a;www.z…

Genymotion模拟器安装ARM架构编译应用失败解决方案

我们在安装一些应用到Genymotion模拟器会提示&#xff1a;adb: failed to install xx.apk: Failure [INSTALL_FAILED_NO_MATCHING_ABIS: Failed to extract native libraries, res-113] 原因是Genymotion采用的编译方式是x86&#xff0c;默认不支持ARM架构编译的应用&#xff0…

CentOS7.5 yum 安装与配置MySQL5.7.24

安装环境&#xff1a;CentOS7 64位 MINI版&#xff0c;安装MySQL5.7 1、配置YUM源 在MySQL官网中下载YUM源rpm安装包&#xff1a;https://dev.mysql.com/downloads/repo/yum/ 下面已经提供一个YUM源安装包,如果不需要特定版本可直接使用我提供的5.7.24版本 # 下载mysql源安装包…

5种改善服务器日志记录的技术

在最近的时间里&#xff0c;我们已经看到了许多工具可以帮助您理解日志。 诸如Scribe和LogStash之类的开源项目&#xff0c;诸如Splunk之类的本地工具以及诸如SumoLogic和PaperTrail之类的托管服务。 这些都可以帮助您将大量日志数据减少为更有意义的内容。 它们共有一个共同点…

在vue项目中引用element-ui时 让el-input 获取焦点的方法

在制作项目的时候遇到一个需求&#xff0c;点击一个按钮弹出一个input输入框&#xff0c;并让输入框获得焦点&#xff0c;项目中引用了element-ui 在网上查找了很多方法&#xff0c;但是在实际使用中发现了一个问题无论是使用$ref获取input元素然后使用focus方法还是使用饿了么…

java excel处理框架,Java三方—-excel框架之POI的使用一

Apache POI是Apache软件基金会的开放源码函式库&#xff0c;POI提供API给Java程序对Microsoft Office格式档案读和写的功能。pdf框架之IText的使用&#xff0c;参见我的博客&#xff1a;Java三方—->pdf框架之IText的使用。今天我们开始POI中Excel部分的学习。POI框架的简单…

关于background-*的一些属性

1、盒模型 盒模型从外到内一次为&#xff1a;margin-box、border-box、padding-box、content-box。 2、一些属性设置的相对位置 ⑴background-position的属性值&#xff08;top/right/bottom/left/center&#xff09;起始位置是相对于padding-box外边沿开始的&#xff0c;…

设计模式:不可变的嵌入式构建器

上周&#xff0c;我写了关于什么使图案成为反图案。 本周&#xff0c;我提出一种设计模式…或等待……也许这是一种反模式。 还是&#xff1f; 让我们看看&#xff01; 当有一个类可以构建另一个实例时&#xff0c;构建器模式是一种编程样式。 构建器模式的最初目的是将对象的…

Outlook邮件的右键菜单中添加自定义按钮

customUI代码如下&#xff1a; <customUI xmlns"http://schemas.microsoft.com/office/2009/07/customui"><contextMenus><contextMenu idMso"ContextMenuMailItem"> <button id"button1" label"修改件名"…

vue 项目的I18n国际化之路

I18n (internationalization ) ---未完善 产品国际化是产品后期维护及推广中重要的一环&#xff0c;通过国际化操作使得产品能更好适应不同语言和地区的需求 国际化重点&#xff1a;1、 语言语言本地化2、 文化颜色、习俗等3、 书写习惯日期格式、时区、数字格式、书写方向备…