- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章浅谈自定义注解在Spring中的应用由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
1.Java自定义注解与Spring 。
Java注解作为程序元素(类、成员变量、成员方法等)的一种元数据信息,对程序本身的执行不会产生影响。通过自定义注解,可以给程序元素添加特殊的声明.
Spring作为构建企业级应用的平台,提供了丰富的功能。将Java的自定义注解与Spring结合,在特定场景下实现注解的解析、处理,可以降低应用的耦合度,提高程序的可扩展性.
2.应用场景 。
下面总结几种应用场景,仅说明大致思路(ps:并非所有场景都在项目中实践过) 。
2.1登陆、权限拦截 。
在web项目中,登陆拦截和权限拦截是一个老生常谈的功能。通过自定义登陆注解或权限注解,在自定义拦截器中解析注解,实现登陆和权限的拦截功能.
这种使用方式,配置简单,灵活度高,代码耦合度低.
2.2定时任务管理 。
在系统构建过程中,会有各种定时任务的需求,而定时任务的集中管理,可以更高效维护系统的运行.
通过Java注解官方文档RepeatingAnnotations章节中的自定义的定时任务注解,可以实现业务方法的定时任务声明。结合Spring的容器后处理器BeanPostProcessor(ps:Spring容器后处理器下篇再说),解析自定义注解。解析后的注解信息再使用QuartzAPI构建运行时定时任务,即可完成定时任务的运行时创建和集中管理.
这种方式能避免定义Quartz定时任务的配置,提高系统扩展性.
2.3多数据源路由的数据源指定 。
Spring提供的AbstractRoutingDataSource实现多数据源的动态路由,可应用在主从分离的架构下。通过对不同的方法指定不同的数据源,实现数据源的动态路由(例如:读方法走从库数据源,写方法走主库数据源)。而如何标识不同的方法对应的数据源类型,则可使用自定义注解实现。通过解析方法上声明的自定义注解对应的数据源类型,实现数据源的路由功能.
这种方式避免了对方法的模式匹配解析(例如:select开头、update开头等),声明更加灵活.
自定义注解 。
先看一个最简单的例子,在使用SpringWeb应用中的过程中,大家免不了会使用@Controller,@Service,@Repository等注解来定义JavaBean。那么怎么自己定义一个注解,Spring可以自动加载呢。所以就有了第一个例子.
1
2
3
4
5
6
7
|
@Target
({ ElementType.TYPE })
@Retention
(RetentionPolicy.RUNTIME)
@Documented
@Component
public
@interface
MyComponent {
String value()
default
""
;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@Configuration
public
class
ComponentAnnotationTest {
public
static
void
main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext =
new
AnnotationConfigApplicationContext();
annotationConfigApplicationContext.register(ComponentAnnotationTest.
class
);
annotationConfigApplicationContext.refresh();
InjectClass injectClass = annotationConfigApplicationContext.getBean(InjectClass.
class
);
injectClass.print();
}
@MyComponent
public
static
class
InjectClass {
public
void
print() {
System.out.println(
"hello world"
);
}
}
}
|
运行这个例子,就会发现,@MyComponent 注解的类,也被Spring加载进来了,而且可以当成普通的JavaBean正常的使用。查看Spring的源码会发现,Spring是使用ClassPathScanningCandidateComponentProvider扫描package,这个类有这样的注释 。
1
2
|
A component provider that scans the classpath from a base
package
.
It then applies exclude and include filters to the resulting classes to find candidates.
|
这个类的 registerDefaultFilters 方法有这样几行代码 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
protected
void
registerDefaultFilters() {
this
.includeFilters.add(
new
AnnotationTypeFilter(Component.
class
));
ClassLoader cl = ClassPathScanningCandidateComponentProvider.
class
.getClassLoader();
try
{
this
.includeFilters.add(
new
AnnotationTypeFilter(((Class<?
extends
Annotation>) ClassUtils.forName(
"javax.annotation.ManagedBean"
, cl)),
false
));
logger.debug(
"JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning"
);
}
catch
(ClassNotFoundException ex) {
// JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.
}
try
{
this
.includeFilters.add(
new
AnnotationTypeFilter(((Class<?
extends
Annotation>) ClassUtils.forName(
"javax.inject.Named"
, cl)),
false
));
logger.debug(
"JSR-330 'javax.inject.Named' annotation found and supported for component scanning"
);
}
catch
(ClassNotFoundException ex) {
// JSR-330 API not available - simply skip.
}
}
|
这里就会发现Spring在扫描类信息的使用只会判断被@Component注解的类,所以任何自定义的注解只要带上@Component(当然还要有String value() default "";的方法,因为Spring的Bean都是有beanName唯一标示的),都可以被Spring扫描到,并注入容器内.
总结 。
以上就是本文关于浅谈自定义注解在Spring中的应用的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持! 。
原文链接:http://blog.csdn.net/liuxigiant/article/details/54296489 。
最后此篇关于浅谈自定义注解在Spring中的应用的文章就讲到这里了,如果你想了解更多关于浅谈自定义注解在Spring中的应用的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
好的,所以我编辑了以下... 只需将以下内容放入我的 custom.css #rt-utility .rt-block {CODE HERE} 但是当我尝试改变... 与 #rt-sideslid
在表格 View 中,我有一个自定义单元格(在界面生成器中高度为 500)。在该单元格中,我有一个 Collection View ,我按 (10,10,10,10) 固定到边缘。但是在 tablev
对于我的无能,我很抱歉,但总的来说,我对 Cocoa、Swift 和面向对象编程还很陌生。我的主要来源是《Cocoa Programming for OS X》(第 5 版),以及 Apple 的充满
我正在使用 meta-tegra 为我的 NVIDIA Jetson Nano 构建自定义图像。我需要 PyTorch,但没有它的配方。我在设备上构建了 PyTorch,并将其打包到设备上的轮子中。现
在 jquery 中使用 $.POST 和 $.GET 时,有没有办法将自定义变量添加到 URL 并发送它们?我尝试了以下方法: $.ajax({type:"POST", url:"file.php?
Traefik 已经默认实现了很多中间件,可以满足大部分我们日常的需求,但是在实际工作中,用户仍然还是有自定义中间件的需求,为解决这个问题,官方推出了一个 Traefik Pilot[1] 的功
我想让我的 CustomTextInputLayout 将 Widget.MaterialComponents.TextInputLayout.OutlinedBox 作为默认样式,无需在 XML 中
我在 ~/.emacs 中有以下自定义函数: (defun xi-rgrep (term) (grep-compute-defaults) (interactive "sSearch Te
我有下表: 考虑到每个月的权重,我的目标是在 5 个月内分散 10,000 个单位。与 10,000 相邻的行是我最好的尝试(我在这上面花了几个小时)。黄色是我所追求的。 我试图用来计算的逻辑如下:计
我的表单中有一个字段,它是文件类型。当用户点击保存图标时,我想自然地将文件上传到服务器并将文件名保存在数据库中。我尝试通过回显文件名来测试它,但它似乎不起作用。另外,如何将文件名添加到数据库中?是在模
我有一个 python 脚本来发送电子邮件,它工作得很好,但问题是当我检查我的电子邮件收件箱时。 我希望该用户名是自定义用户名,而不是整个电子邮件地址。 最佳答案 发件人地址应该使用的格式是: You
我想减小 ggcorrplot 中标记的大小,并减少文本和绘图之间的空间。 library(ggcorrplot) data(mtcars) corr <- round(cor(mtcars), 1)
GTK+ noob 问题在这里: 是否可以自定义 GtkFileChooserButton 或 GtkFileChooserDialog 以删除“位置”部分(左侧)和顶部的“位置”输入框? 我实际上要
我正在尝试在主页上使用 ajax 在 magento 中使用 ajax 显示流行的产品列表,我可以为 5 或“N”个产品执行此操作,但我想要的是将分页工具栏与结果集一起添加. 这是我添加的以显示流行产
我正在尝试使用 PasswordResetForm 内置函数。 由于我想要自定义表单字段,因此我编写了自己的表单: class FpasswordForm(PasswordResetForm):
据我了解,新的 Angular 7 提供了拖放功能。我搜索了有关 DnD 的 Tree 组件,但没有找到与树相关的内容。 我在 Stackblitz 上找到的一个工作示例.对比drag'ndrop功能
我必须开发一个自定义选项卡控件并决定使用 WPF/XAML 创建它,因为我无论如何都打算学习它。完成后应该是这样的: 到目前为止,我取得了很好的进展,但还有两个问题: 只有第一个/最后一个标签项应该有
我要定制xtable用于导出到 LaTeX。我知道有些问题是关于 xtable在这里,但我找不到我要找的具体东西。 以下是我的表的外观示例: my.table <- data.frame(Specif
用ejs在这里显示日期 它给我结果 Tue Feb 02 2016 16:02:24 GMT+0530 (IST) 但是我需要表现为 19th January, 2016 如何在ejs中执行此操作?
我想问在 JavaFX 中使用自定义对象制作 ListView 的最佳方法,我想要一个每个项目如下所示的列表: 我搜了一下,发现大部分人都是用细胞工厂的方法来做的。有没有其他办法?例如使用客户 fxm
我是一名优秀的程序员,十分优秀!