- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章详解使用Spring AOP和自定义注解进行参数检查由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
引言 。
使用springmvc作为controller层进行web开发时,经常会需要对controller中的方法进行参数检查。本来springmvc自带@valid和@validated两个注解可用来检查参数,但只能检查参数是bean的情况,对于参数是string或者long类型的就不适用了,而且有时候这两个注解又突然失效了(没有仔细去调查过原因),对此,可以利用spring的aop和自定义注解,自己写一个参数校验的功能.
代码示例 。
注意:本节代码只是一个演示,给出一个可行的思路,并非完整的解决方案.
本项目是一个简单web项目,使用到了:spring、springmvc、maven、jdk1.8 。
项目结构:
自定义注解:
validparam.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package
com.lzumetal.ssm.paramcheck.annotation;
import
java.lang.annotation.*;
/**
* 标注在参数bean上,表示需要对该参数校验
*/
@target
({elementtype.parameter})
@retention
(retentionpolicy.runtime)
@documented
public
@interface
validparam {
}
|
notnull.java:
1
2
3
4
5
6
7
8
9
10
11
12
|
package
com.lzumetal.ssm.paramcheck.annotation;
import
java.lang.annotation.*;
@target
({elementtype.field, elementtype.parameter})
@retention
(retentionpolicy.runtime)
@documented
public
@interface
notnull {
string msg()
default
"字段不能为空"
;
}
|
notempty.java:
1
2
3
4
5
6
7
8
9
10
11
12
|
package
com.lzumetal.ssm.paramcheck.annotation;
import
java.lang.annotation.*;
@target
({elementtype.field, elementtype.parameter})
@retention
(retentionpolicy.runtime)
@documented
public
@interface
notempty {
string msg()
default
"字段不能为空"
;
}
|
切面类 。
paramcheckaspect.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
package
com.lzumetal.ssm.paramcheck.aspect;
import
com.lzumetal.ssm.paramcheck.annotation.notempty;
import
com.lzumetal.ssm.paramcheck.annotation.notnull;
import
com.lzumetal.ssm.paramcheck.annotation.validparam;
import
org.apache.commons.lang3.stringutils;
import
org.aspectj.lang.joinpoint;
import
org.aspectj.lang.annotation.aspect;
import
org.aspectj.lang.annotation.before;
import
org.aspectj.lang.reflect.methodsignature;
import
org.springframework.stereotype.component;
import
javax.servlet.http.httpservletrequest;
import
javax.servlet.http.httpservletresponse;
import
javax.servlet.http.httpsession;
import
java.lang.reflect.field;
import
java.lang.reflect.parameter;
import
java.util.arrays;
/**
* 参数检查切面类
*/
@aspect
@component
public
class
paramcheckaspect {
@before
(
"execution(* com.lzumetal.ssm.paramcheck.controller.*.*(..))"
)
public
void
paramcheck(joinpoint joinpoint)
throws
exception {
//获取参数对象
object[] args = joinpoint.getargs();
//获取方法参数
methodsignature signature = (methodsignature) joinpoint.getsignature();
parameter[] parameters = signature.getmethod().getparameters();
for
(
int
i =
0
; i < parameters.length; i++) {
parameter parameter = parameters[i];
//java自带基本类型的参数(例如integer、string)的处理方式
if
(isprimite(parameter.gettype())) {
notnull notnull = parameter.getannotation(notnull.
class
);
if
(notnull !=
null
&& args[i] ==
null
) {
throw
new
runtimeexception(parameter.tostring() + notnull.msg());
}
//todo
continue
;
}
/*
* 没有标注@validparam注解,或者是httpservletrequest、httpservletresponse、httpsession时,都不做处理
*/
if (parameter.gettype().isassignablefrom(httpservletrequest.class) || parameter.gettype().isassignablefrom(httpsession.class) ||
parameter.gettype().isassignablefrom(httpservletresponse.class) || parameter.getannotation(validparam.class) == null) {
continue;
}
class<?> paramclazz = parameter.gettype();
//获取类型所对应的参数对象,实际项目中controller中的接口不会传两个相同的自定义类型的参数,所以此处直接使用findfirst()
object arg = arrays.stream(args).filter(ar -> paramclazz.isassignablefrom(ar.getclass())).findfirst().get();
//得到参数的所有成员变量
field[] declaredfields = paramclazz.getdeclaredfields();
for (field field : declaredfields) {
field.setaccessible(true);
//校验标有@notnull注解的字段
notnull notnull = field.getannotation(notnull.class);
if (notnull != null) {
object fieldvalue = field.get(arg);
if (fieldvalue == null) {
throw new runtimeexception(field.getname() + notnull.msg());
}
}
//校验标有@notempty注解的字段,notempty只用在string类型上
notempty notempty = field.getannotation(notempty.class);
if (notempty != null) {
if (!string.class.isassignablefrom(field.gettype())) {
throw new runtimeexception("notempty annotation using in a wrong field class");
}
string fieldstr = (string) field.get(arg);
if (stringutils.isblank(fieldstr)) {
throw new runtimeexception(field.getname() + notempty.msg());
}
}
}
}
}
/**
* 判断是否为基本类型:包括string
* @param clazz clazz
* @return true:是; false:不是
*/
private
boolean
isprimite(
class
<?> clazz){
return
clazz.isprimitive() || clazz == string.
class
;
}
}
|
参数javabean 。
studentparam.java:
1
2
3
4
5
6
7
8
9
10
11
|
package
com.lzumetal.ssm.paramcheck.requestparam;
import
com.lzumetal.ssm.paramcheck.annotation.notempty;
import
com.lzumetal.ssm.paramcheck.annotation.notnull;
public
class
studentparam {
@notnull
private
integer id;
private
integer age;
@notempty
private
string name;
//get、set方法省略...
}
|
验证参数校验的controller 。
testcontroller.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package
com.lzumetal.ssm.paramcheck.controller;
import
com.google.gson.gson;
import
com.lzumetal.ssm.paramcheck.annotation.notnull;
import
com.lzumetal.ssm.paramcheck.annotation.validparam;
import
com.lzumetal.ssm.paramcheck.requestparam.studentparam;
import
org.springframework.stereotype.controller;
import
org.springframework.web.bind.annotation.requestmapping;
import
org.springframework.web.bind.annotation.requestmethod;
import
org.springframework.web.bind.annotation.responsebody;
@controller
public
class
testcontroller {
private
static
gson gson =
new
gson();
@responsebody
@requestmapping
(value =
"/test"
, method = requestmethod.post)
public
studentparam checkparam(
@validparam
studentparam param,
@notnull
integer limit) {
system.out.println(gson.tojson(param));
system.out.println(limit);
return
param;
}
}
|
本节示例代码已上传至github:https://github.com/liaosilzu2007/ssm-parent.git 。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:https://segmentfault.com/a/1190000014454607 。
最后此篇关于详解使用Spring AOP和自定义注解进行参数检查的文章就讲到这里了,如果你想了解更多关于详解使用Spring AOP和自定义注解进行参数检查的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我在同一类中的方法之间进行方法调用并应用事务建议时遇到问题。 Spring Framework .NET 文档声明它支持基于组合和继承的代理,并且您可以强制 Spring 创建要实例化的基于继承的代理
我理解这些原则,但我很难看到实际应用程序在少数几个之外。请赐教;) 最佳答案 问任何支持人员:日志记录不是 AOP 的一个好的应用程序。他们不在乎应用程序内部调用了什么方法。他们关心应用程序正在执行的
我知道以前有人问过这个问题,但这是一年半前的事了,尽管我认为现在可能是重新提问的时候了。我也认识到它可能被视为主观的,但我想有一些客观的原因支持/反对 AOP。 我会对 感兴趣谁在使用 AOP 在软件
我想这个问题以前有人问过,但我无法立即找到相关的 SO 问题或其他地方的相关文章。 令我震惊的是,AOP 中的某些术语相当奇怪。看来我不是唯一一个-这个article ,例如,指出“不幸的是,AOP
面向切面编程可能的和严重的缺点是什么? 例如:新手的神秘调试(可读性影响) 最佳答案 工具链支持不佳 - 调试器、分析器等可能不了解 AOP,因此可能会在代码上工作,就好像所有方面都已被过程代码替换
这两种AOP框架的优缺点是什么?我使用 Unity 作为我的 aop 框架,但我猜想编译时 aop 框架(例如 postsharp)可能比运行时 aop 框架具有更好的性能?看起来运行时 aop 框架
我现在正在学习 spring aop,我不知道将上下文参数传递给建议。 请注意,我指的是 context 参数,而不是 normal 参数。 传递普通参数很简单,例如: a join point: p
来自类路径资源 [ApplicationContextAOP.xml] 的 XML 文档中的第 13 行无效;嵌套异常是 org.xml.sax.SAXParseException: cvc-comp
我使用 spring boot 2 和 spring security。 使用 aop,我搜索以获取调用该方法的用户。 @Aspect @Component public class LogAspec
我最近一直在一个非常简单的应用程序上尝试 Spring 的 AOP 功能,并且我坚持在适当的时间运行该方法,这意味着该部分中定义的方法应该在 中定义的方法之后运行 在我的代码中,这两个方法都在主方法中
我试图在网上找到如何通过 Ninject 使用 AOP 的例子。有人可以确认 AOP 在 Ninject 2 中是否可用而不使用外部库(即 CaSTLe Windsor?)。 如果可以的话,您能否发布
Aop配置已经在我的项目中完成了。为此添加了以下配置。问题是当下面的代码没有注释时,不会调用 formService 中的方法。因此我得到空指针异常。知道问题出在哪里吗?我附上了下面的代码.. AOP
我是 AOP 的新手。我遇到了这样的问题。 package org.suman.Aspect; import org.aspectj.lang.annotation.Aspect; import or
在我们的企业应用程序中,我们希望将日志记录、度量等横切关注点作为方面。我们已经准备好了 aspectj 建议(来自我们现有的 java 应用程序),但我没有找到将 aspectj 与 Grails 集
我正在向外部系统编写 Web 服务。 我的服务包装类有许多方法可以调用Web服务的所有soap接口(interface)。该调用可能会引发异常,然后该异常会自动触发重新连接到 Web 服务。 为了处理
已结束。此问题不符合 Stack Overflow guidelines .它目前不接受答案。 我们不允许提出有关书籍、工具、软件库等方面的建议的问题。您可以编辑问题,以便用事实和引用来回答它。 关闭
我是 spring 框架的新手,正在尝试一些示例来理解 AOP,这是我到目前为止所做的,但它不起作用。 问题是我一添加 对于 spring.xml,我的构建失败说无法创建具有空指针异常的 bean。但
下面是我要创建的方面。我想将两个切入点表达式合并为一个。我已经看到这可以使用带注释的切入点来完成,但是 xml 中的相同语法失败了。谁能帮帮我? 提前致谢 最佳答案
我对 Spring 事务管理感到困惑。在我的应用程序中,我在服务类中使用 @Transactional 实现了事务管理。我配置的 spring.xml 如下:
我知道围绕 Controller 方法编写 AOP 建议的标准方法,并且如果在 Controller 方法中声明,您可以访问 HttpServletRequest arg。 但我的情况是我有一个翻译服
我是一名优秀的程序员,十分优秀!