自定义弹簧范围?

编程入门 行业动态 更新时间:2024-10-14 12:27:14
本文介绍了自定义弹簧范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

除了 Servlet上下文范围之外,任何人都知道其他自定义spring范围和 ThreadScope 吗?

如果您制作了一些闭源自定义范围,那么我真的也很想听听它的作用以及如何为您工作. (我想有人会在桌面应用程序中创建WindowScope吗?)

If you've made some closed-source custom scope I'd really also be interested in hearing what it does and how it worked out for you. (I'd imagine someone would make a WindowScope in a desktop app ?)

我愿意接受所有用例,希望在这里扩大视野.

I'm open to all use cases, I'm looking to expand my horizon here.

推荐答案

我们实现了自己的自定义Spring范围.我们的许多代码都在相对较低的级别(靠近数据库)下工作,并且在此基础上,我们使用自己的数据源,链接,属性等对象模型来维护概念级别.

We implemented our own custom Spring scope. A lot of our code works at a relatively low level, close to the database, and we maintain a conceptual level on top of that with its own object model of data sources, links, attributes etc.

无论如何,很多bean需要所谓的StorageDictionary(此对象图的封装)来完成其工作.当我们对对象图进行不重要的更改时,有时需要删除并重新创建字典.因此,我们为字典范围的对象实现了自定义范围,并且给定字典的部分无效包括清除此自定义范围.这使Spring可以处理这些对象的一种很好的自动缓存形式.每次您都得到一个相同的对象,直到字典失效,这时您将得到一个新的对象.

Anyway, a lot of beans require a so-called StorageDictionary (an encapsulation of this object graph) to do their work. When we make non-trivial changes to the object graph, the dictionary sometimes needs to be blown away and recreated. Consequently, we implemented a custom scope for objects that were dictionary scoped, and part of the invalidation of a given dictionary involves clearing this custom scope. This lets Spring handle a nice form of automatic caching for these objects. You get the same object back every time up until the dictionary is invalidated, at which point you get a new object.

这不仅有助于保持一致性,而且还允许对象本身将对字典中实体的引用进行高速缓存,这是安全的,因为只要它们自身可以被Spring检索,高速缓存将一直有效.反过来,这使我们可以将它们构建为不可变的对象(只要它们可以通过构造函数注入进行连接即可),无论如何,这都是一件非常好的事情.

This helps not only with consistency but also allows the objects themselves to cache references to entities within the dictionary, safe within the knowledge that the cache will be valid for as long as they themselves are retrievable by Spring. This in turn lets us build these as immutable objects (so long as they can be wired via constructor injection), which is a very good thing to do anyway wherever possible.

该技术不会在任何地方都有效,并且在很大程度上取决于软件的特性(例如,如果定期修改字典,这将是非常低效的,如果更新字典,则永远都不会是不必要的,并且其效率将比直接使用的效率稍低使用权).但是,它绝对帮助我们以概念上简单明了的方式将对生命周期的管理从Spring转移到了Spring.

This technique won't work everywhere and does depend heavily on the characteristics of the software (e.g. if the dictionary was modified regularly this would be horribly inefficient, and if it was updated never this would be unnecessary and slightly less efficient than direct access). However, it has definitely helped us pass off this management of lifecycle to Spring in a way that is conceptually straightforward and in my opinion quite elegant.

更多推荐

自定义弹簧范围?

本文发布于:2023-11-28 12:32:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642411.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   弹簧

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!