惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Commits to openclaw:main
Recent Commits to openclaw:main
N
News | PayPal Newsroom
TaoSecurity Blog
TaoSecurity Blog
Google Online Security Blog
Google Online Security Blog
NISL@THU
NISL@THU
T
Threatpost
C
CXSECURITY Database RSS Feed - CXSecurity.com
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Engineering at Meta
Engineering at Meta
AWS News Blog
AWS News Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Privacy International News Feed
B
Blog
PCI Perspectives
PCI Perspectives
Martin Fowler
Martin Fowler
Spread Privacy
Spread Privacy
P
Proofpoint News Feed
T
Tenable Blog
F
Fortinet All Blogs
G
GRAHAM CLULEY
V2EX - 技术
V2EX - 技术
C
Check Point Blog
Project Zero
Project Zero
P
Palo Alto Networks Blog
J
Java Code Geeks
W
WeLiveSecurity
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
The Exploit Database - CXSecurity.com
博客园 - 司徒正美
P
Privacy & Cybersecurity Law Blog
S
SegmentFault 最新的问题
Last Week in AI
Last Week in AI
Forbes - Security
Forbes - Security
C
Cybersecurity and Infrastructure Security Agency CISA
Security Latest
Security Latest
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Recent Announcements
Recent Announcements
博客园 - Franky
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Recorded Future
Recorded Future
The Last Watchdog
The Last Watchdog
MongoDB | Blog
MongoDB | Blog
人人都是产品经理
人人都是产品经理
酷 壳 – CoolShell
酷 壳 – CoolShell
Cisco Talos Blog
Cisco Talos Blog
量子位
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

ControlNet Blog

异星工厂中的高品质产率分析 比之前更进一步的终极看番工具栈 探索数据潜力:预训练模型与Masked Autoencoder的表征学习之旅 异星工厂原版原创蓝图分享 学习AI绘画,从Diffusion和CLIP开始 怎么通信比较快?Python跨进程通信测试 Python编程基础08:单元测试和异常处理 Python编程基础07:标准库和第三方库 Python编程基础06:Python的面对对象编程 Windows最佳动画观看环境配置指南(MPC-HC, madvr, SVP, Anime4K) 计算机视觉中的Transformer续 Python编程基础05:Python的过程分解和文件IO 解锁网易云变灰曲目PC版(Windows, MacOS) Python编程基础04:Python高级集合类型和一些杂项 Aria2傻瓜安装部署指南 Python编程基础03:数据结构和Python集合类型 在UEFI Secure Boot下的Linux安装N卡驱动 计算机视觉中的Transformer 一些邮箱客户端的使用对比
LightIoC: Scala原生基于注解的依赖注入库
ControlNet · 2022-02-18 · via ControlNet Blog

LightIoC是一个通过依赖注入的Scala轻量化控制反转工具,发布在GitHub上,项目地址

添加依赖

Scala

这个库针对Scala 2.13.x, 2.12.x, 2.11.x进行编译,在Java8, 11, 16中通过测试。目前最新版本是0.3.0,请持续关注GitHub页面

SBT:

1
libraryDependencies += "space.controlnet" %% "lightioc" % "0.3.0"

Gradle:

1
implementation group: "space.controlnet", name: "lightioc_<scala_version>", version: "0.3.0"

Java

这个库提供了一个Java友好的API,可以在Java中使用LightIoC。详情可以看Java API。

Gradle:

1
implementation group: "space.controlnet", name: "lightioc-api_2.13", version: "0.3.0"

快速开始

静态注册

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
import space.controlnet.lightioc.Container

@Provider
class TopLevelClass

@Singleton
class TopLevelSingletonClass

@Provider(isObject = true)
object TopLevelObject

@Provider(stringId = "IdForThisClass")
class NamedProviderClass

@Provider(isObject = true)
object NestedClass {

@Provider
class InnerClass

@Provider(isObject = true)
object InnerObject
}

object Main extends App {
Container.init("<package name>")
}

动态注册

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
import space.controlnet.lightioc.Container
import space.controlnet.lightioc.Util._

class Foo
class Bar {
var x: Int = _
var y: Int = _
}
object Baz
class Qux(foo: Foo)

object Main extends App {

Container.register[Foo].toSelf.inTransientScope()

Container.register[Bar].toSelf.inSingletonScope()

Container.register[Baz.type].toSelf.inSingletonScope()

Container.register[Qux].toConstructor(classOf[Foo]).inTransientScope()

Container.register("A Number").toValue(123).inSingletonScope()

val barX = 1
val barY = 2
Container.register[Int]("Bar.x").toValue(barX).inSingletonScope()
Container.register[Int]("Bar.y").toValue(barY).inSingletonScope()
val factory: Factory[Bar] = Container => {

val bar = new Bar
bar.x = Container.resolve[Int]("Bar.x")
bar.y = Container.resolve[Int]("Bar.y")
bar
}
Container.register[Bar].toFactory(factory).inTransientScope()

Container.register[Foo].toSelf.inTransientScope()
Container.register("AnotherFoo").toService[Foo]
}

当然,也可以使用Scala特色的自定义运算符。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import space.controlnet.lightioc.Container
import space.controlnet.lightioc.BindingSetter.{ New, Self }

object Main extends App {

Container.register("AString") -> "Hello IOC"

Container.register[Foo] -> classOf[Foo]

Container.register[Bar] := classOf[Bar]

Container.register[Baz.type] -> Self

Container.register[Qux] -> New(classOf[Foo])

Container.register[Bar] ~> factory

Container.register("AnotherString") >> "AString"
}

动态获取

1
2
3
4
5
6
7
8
9
10
import space.controlnet.lightioc.Container

object Main extends App {

val foo: Foo = Container.resolve[Foo]

val str: String = Container.resolve[String]("AString")

val bar: Bar = Container.resolve[Bar]
}

自动装配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import space.controlnet.lightioc.annotation.{ Autowired, Provider }

@Provider
class Foo {
@Autowired
val bar: Bar = null
}

@Provider
class Bar

@Provider
object Baz {
@Autowired
var x: Int = 0
}

Java API

一个使用Kotlin的例子。

1
2
3
4
5
6
7
8
9
10
11
12
13
import space.controlnet.lightioc.annotation.Singleton
import space.controlnet.lightioc.api.Container

@Singleton
class A {
val x: Int = 100
}

fun main() {
Container.init("<package name>")
val a = Container.resolve(A::class.java)
println(a.x)
}