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

推荐订阅源

T
Threatpost
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Security Affairs
N
News and Events Feed by Topic
T
Tenable Blog
P
Proofpoint News Feed
W
WeLiveSecurity
Simon Willison's Weblog
Simon Willison's Weblog
Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
Help Net Security
Help Net Security
I
Intezer
T
Threat Research - Cisco Blogs
S
Secure Thoughts
C
Cyber Attacks, Cyber Crime and Cyber Security
L
Lohrmann on Cybersecurity
AWS News Blog
AWS News Blog
Google Online Security Blog
Google Online Security Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
Project Zero
Project Zero
The Hacker News
The Hacker News
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Tor Project blog
N
News | PayPal Newsroom
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
A
Arctic Wolf
Forbes - Security
Forbes - Security
O
OpenAI News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Security Latest
Security Latest
P
Palo Alto Networks Blog
S
Schneier on Security
S
Securelist
C
Cybersecurity and Infrastructure Security Agency CISA
H
Heimdal Security Blog
V
Vulnerabilities – Threatpost
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园_首页
T
Troy Hunt's Blog
Latest news
Latest news
Recent Announcements
Recent Announcements
MyScale Blog
MyScale Blog
人人都是产品经理
人人都是产品经理
L
LINUX DO - 热门话题
M
MIT News - Artificial intelligence
N
Netflix TechBlog - Medium
V
Visual Studio Blog
H
Hacker News: Front Page

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)
}