七叶笔记 » golang编程 » golang的自动装载功能-boot4go

golang的自动装载功能-boot4go

Spring boot的应用里,强大的Spring提供的IOC容器,提供了很方便的自动装载Service和Component的功能

使用

@Autowire

或者

@resource

等方式,可以很方便简单的对组件里的字段进行自动装载

在Golang里,由于天生语言的特点,在面向对象的代码实现上没有 java 用的那么方便,特别是其中都是以 struct 的方式来封装对象,结合着func的定义来实现接口的继承,写起代码来总觉得没有java里写springApplication来的那么方便。

找到boot4go这个小项目,其中提供了简单的自动装载功能和 yaml 文件的配置管理功能。

项目地址

Autoconfiguration

 apiVersion: policy/v1beta1
kind:  Pod SecurityPolicy
metadata:
  name: mysql-snapshot
  version: v1.0.1
  major: 1
tag:
  hello: TAG-HELLO-A
  hello2: TAG-HELLO-B
spec:
  privileged: false
  allowPrivilegeEscalation: false
  volumes:
    - "*"
    - "*.json"
  hostNetwork: false
  hostIPC: false
  hostPID: false
  runAsUser:
    rule1: RunAsAny1
    rule2: RunAsAny2
    rule3: RunAsAny3
   seLinux :
    rule: RunAsAny
   supplemental Groups:
    rule: RunAsAny
  fsGroup:
    rule: RunAsAny  

 func TestContextConfiguration(t *testing.T) {
	logger := log4go.LoggerManager.GetLogger("test")
	
	logger.Info("YAML %v", ConfigurationContext.ToMap())

	logger.Info("YAML %v", ConfigurationContext.GetValue("${metadata.name}"))
	logger.Info("YAML %v", ConfigurationContext.GetValue("${spec.volumes[0]}"))

	time.Sleep(10 * time.Second)
}  

日志输出

 [19:11:28 CST 2022/04/09 677] [INFO][test] (github.com/gohutool/boot4go.TestContextConfiguration:118) YAML map[ALLUSERSPROFILE:C:\ProgramData APPCODE_VM_OPTIONS:E:\BaiduNetdiskDownload\ JetBrains  2022 最 新版本全家桶激活\方式2:激活到2099年补丁\ja-netfilter-all\vmoptions\appcode.                 vmoptions APPDATA:C:\Users\NST\AppData\Roaming AR:ar CC:gcc CGO_CFLAGS:-O0 -g CGO_ENABLED:1 CLION_VM_OPTIONS:E:\BaiduNetdiskDownload\JetBrains 2022 最新版   本全家桶激活\方式2:激活到2099年补丁\ja-netfilter-all\vmoptions\clion.vmopti               ons COMPUTERNAME:NST-PC CXX:g++ ComSpec:C:\WINDOWS\system32\cmd.exe CommonProgramFiles:C:\Program Files\Common Files CommonProgramFiles(x86):C:\Program Files (x86)\Common Files CommonProgramW6432:C:\Program Files\Common Files DATAGRIP_VM_OPTIONS:E:\BaiduNetdiskDownload\JetBrains 2022 最新版本全家桶激活\方式           2:激活到2099年补丁\ja-netfilter-all\vmoptions\datagrip.vmoptions DATASPELL_V       M_OPTIONS:E:\BaiduNetdiskDownload\JetBrains 2022 最新版本全家桶激活\方式2:激活到2099年补丁\ja-netfilter-all\vmo            ptions\dataspell.vmoptions DriverData:C:\Windows\ System32 \Drivers\DriverData FPS_BROWSER_APP_PROFILE_STRING:Internet Explorer FPS_BROWSER_USER_PROFILE_STRING:Default GATEWAY_VM_OPTIONS:E:\BaiduNetdiskDownload\JetBrains 2022 最新版本     全家桶激活\方式2:激活到2099年补丁\ja-netfilter-all\vmoptions\gateway.vmopti              ons GCCGO:gccgo GO111MODULE:on GOAMD64:v1 GOARCH:amd64 GOCACHE:C:\Users\NST\AppData\Local\go-build GOENV:C:\Users\NST\AppData\Roaming\go\env GOEXE:.exe GOHOSTARCH:amd64 GOHOSTOS:windows GOLAND_VM_OPTIONS:E:\BaiduNetdiskDownload\JetBrains 2022 最新版本全家桶激活\方式2:激活到2099年补丁\ja-netfilter-all\v                  moptions\goland.vmoptions GOMODCACHE:E:\WORK\SOFT\go1.18.windows-amd64\go\pkg\mod GOOS:windows GOPATH:E:\WORK\SOFT\go1.18.windows-amd64\go GOPROXY: GOROOT:E:\WORK\SOFT\go1.18.windows-amd64\go GOSUMDB:sum.golang.org GOTOOLDIR:E:\WORK\SOFT\go1.18.windows-amd64\go\pkg\tool\windows_amd64 GOVERSION:go1.18 GoLand:E:\WORK\SOFT\JetBrains\GoLand 2021.3.3\bin; HOMEDRIVE:C: HOMEPATH:\Users\NST IDEA_INITIAL_DIRECTORY:C:\WINDOWS\System32 IDEA_VM_OPTIONS:E:\BaiduNetdiskDownload\JetBrains 2022 最新版    本全家桶激活\方式2:激活到2099年补丁\ja-netfilter-all\vmoptions\idea.vmoptio               ns JAVA_HOME:E:\WORK\SOFT\JDK8-64 JETBRAINSCLIENT_VM_OPTIONS:E:\BaiduNetdiskDownload\JetBrains 2022 最新版本全家桶激活\方式2:激活到2099年补丁\ja-netfilt                  er-all\vmoptions\jetbrainsclient.vmoptions JETBRAINS_CLIENT_VM_OPTIONS:E:\BaiduNetdiskDownload\JetBrains 2022 最新版本全家桶激活\方式2:激活到2099年补丁\j                  a-netfilter-all\vmoptions\jetbrains_client.vmoptions LOCALAPPDATA:C:\Users\NST\AppData\Local LOGONSERVER:\\NST-PC MAVEN_HOME:E:\WORK\SOFT\maven-3.6.1 MONGO_HOME:E:\WORK\SOFT\mongodb- win32 -x86_64-windows-5.0.3\ MOZ_PLUGIN_PATH:D:\Program Files (x86)\Foxit Software\Foxit PhantomPDF\plugins\ NPM_PREFIX:E:\WORK\SOFT\nodejs\node_global NUMBER_OF_PROCESSORS:8 OS:Windows_NT OneDrive:C:\Use
[21:40:01 CST 2022/04/09 423] [INFO][boot4go.context.test] (github.com/gohutool/boot4go.TestGet bean :117) mysql-snapshot
[21:40:01 CST 2022/04/09 423] [INFO][boot4go.context.test] (github.com/gohutool/boot4go.TestGetBean:120) [* *.json]  

自动装载字段属性

 type Test struct {
	age     int16          `bootable:"${metadata.major}"`
	name    string         `bootable:"${metadata.name}"`
	version string         `bootable:"${metadata.version}"`
	data    map[string]any `bootable:"${spec.runAsUser}"`
	list    []any          `bootable:"${spec.volumes}"`
}  

日志输出

 [21:40:01 CST 2022/04/09 424] [INFO][boot4go.context.test] (github.com/gohutool/boot4go.TestGetBean:129) ↦[rule1:RunAsAny1 rule2:RunAsAny2 rule3:RunAsAny3]
[21:40:01 CST 2022/04/09 424] [INFO][boot4go.context.test] (github.com/gohutool/boot4go.TestGetBean:130) &[* *.json]
[21:40:01 CST 2022/04/09 424] [INFO][boot4go.context.test] (github.com/gohutool/boot4go.TestGetBean:130) 1
[21:40:01 CST 2022/04/09 424] [INFO][boot4go.context.test] (github.com/gohutool/boot4go.TestGetBean:130) mysql-snapshot
[21:40:01 CST 2022/04/09 424] [INFO][boot4go.context.test] (github.com/gohutool/boot4go.TestGetBean:130) v1.0.1  

Autowire with nested struct(自动装载嵌套结构体)

 type Test struct {
	hello   IHello         `bootable:"aaa"`
	hello2  Hello2         `bootable`
	data    map[string]any `bootable:"${spec.runAsUser}"`
	list    []any          `bootable:"${spec.volumes}"`
}

func (t *Test) sayHello2(w string) string {
	return t.hello2.sayHello(w)
}

func (t *Test) sayHello(w string) string {
	return t.hello.sayHello(w)
}

type IHello interface {
	sayHello(t string) string
}

type Hello struct {
	tag string `bootable:"${tag.hello}"`
}

type Hello2 struct {
	tag string `bootable:"${tag.hello2}"`
}

func (h *Hello) sayHello(t string) string {
	return "Hello " + h.tag + " : " + t
}

func (h *Hello2) sayHello(t string) string {
	return "Hello2 " + h.tag + " : " + t
}  

 func init() {
	log4go.LoggerManager.InitWithDefaultConfig()
	d, _ := Context. Registry Bean("aaa", Hello{})
	logger = log4go.LoggerManager.GetLogger("boot4go.context.test")
}

func TestGetBean(t *testing.T) {
    // Autowired Bean
	bean, _ := Context.GetBean(Test{})

	t1 := bean.(*Test)
	logger.Info(t1.hello)
	logger.Info("Hello2=" + t1.sayHello2("AAA"))
	logger.Info("Hello=" + t1.sayHello("AAA"))
}  

日志输出

 === RUN   TestGetBean
[21:40:01 CST 2022/04/09 423] [INFO][boot4go.context.test] (github.com/gohutool/boot4go.TestGetBean:117) 0xc00013bbb8
[21:40:01 CST 2022/04/09 423] [INFO][boot4go.context.test] (github.com/gohutool/boot4go.TestGetBean:118) Hello2=Hello2 TAG-HELLO-B : AAA
[21:40:01 CST 2022/04/09 424] [INFO][boot4go.context.test] (github.com/gohutool/boot4go.TestGetBean:126) Hello=Hello TAG-HELLO-A : AAA
--- PASS: TestGetBean (10.00s)
PASS


Debugger finished with the exit code 0  

boot4go目前在刚刚开始, 还会持续增加新的功能。目标sprincloud -》 gocloud

相关项目

参考文章

日志功能

表达式功能

动态代理

相关文章