SpringBoot

package
v1.0.0-beta Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 8, 2019 License: Apache-2.0 Imports: 9 Imported by: 26

README

spring-boot

开箱即用的 Go-Spring 程序启动框架。

import (
	"fmt"
	"testing"
	"time"

	"github.com/go-spring/go-spring/boot-starter"
	"github.com/go-spring/go-spring/spring-boot"
	"github.com/go-spring/go-spring/spring-core"
)

func init() {
	SpringBoot.RegisterModule(func(ctx SpringCore.SpringContext) {
		ctx.RegisterBean(new(MyModule))
	})
}

type MyModule struct {
}

func (m *MyModule) OnStartApplication(ctx SpringBoot.ApplicationContext) {
	fmt.Println("MyModule start")

	ctx.SafeGoroutine(func() {

		defer fmt.Println("go stop")
		fmt.Println("go start")

		time.Sleep(200 * time.Millisecond)
		BootStarter.Exit()
	})
}

func (m *MyModule) OnStopApplication(ctx SpringBoot.ApplicationContext) {
	fmt.Println("MyModule stop")
}

func TestRunApplication(t *testing.T) {
	SpringBoot.RunApplication("config/")
}

Documentation

Overview

开箱即用的 Go-Spring 程序启动框架。

Index

Constants

View Source
const (
	SPRING_PROFILE = "spring.profile"
)

Variables

This section is empty.

Functions

func BindProperty

func BindProperty(name string, i interface{})

根据类型获取属性值,属性名称统一转成小写。

func CollectBeans

func CollectBeans(i interface{}) bool

收集数组或指针定义的所有符合条件的 Bean 对象,收集到返回 true,否则返回 false。

func Exit

func Exit()

退出 SpringBoot 应用。

func FindBeanByName

func FindBeanByName(beanId string) (interface{}, bool)

根据名称和类型获取单例 Bean,若多于 1 个则 panic;找到返回 true 否则返回 false。

func GetAllBeanDefinitions

func GetAllBeanDefinitions() []*SpringCore.BeanDefinition

获取所有 Bean 的定义,一般仅供调试使用。

func GetAllProperties

func GetAllProperties() map[string]interface{}

获取所有的属性值

func GetBean

func GetBean(i interface{}) bool

根据类型获取单例 Bean,若多于 1 个则 panic;找到返回 true 否则返回 false。

func GetBeanByName

func GetBeanByName(beanId string, i interface{}) bool

根据名称和类型获取单例 Bean,若多于 1 个则 panic;找到返回 true 否则返回 false。

func GetBoolProperty

func GetBoolProperty(name string) bool

获取布尔型属性值,属性名称不支持大小写。

func GetDefaultProperty

func GetDefaultProperty(name string, defaultValue interface{}) (interface{}, bool)

获取属性值,如果没有找到则使用指定的默认值,属性名称不支持大小写。

func GetFloatProperty

func GetFloatProperty(name string) float64

获取浮点型属性值,属性名称不支持大小写。

func GetIntProperty

func GetIntProperty(name string) int64

获取有符号整型属性值,属性名称不支持大小写。

func GetPrefixProperties

func GetPrefixProperties(prefix string) map[string]interface{}

获取指定前缀的属性值集合,属性名称不支持大小写。

func GetProfile

func GetProfile() string

获取运行环境

func GetProperty

func GetProperty(name string) interface{}

获取属性值,属性名称不支持大小写。

func GetStringProperty

func GetStringProperty(name string) string

获取字符串型属性值,属性名称不支持大小写。

func GetUintProperty

func GetUintProperty(name string) uint64

获取无符号整型属性值,属性名称不支持大小写。

func RegisterBean

func RegisterBean(bean interface{}) *SpringCore.Annotation

注册单例 Bean,不指定名称,重复注册会 panic。

func RegisterBeanDefinition

func RegisterBeanDefinition(beanDefinition *SpringCore.BeanDefinition) *SpringCore.Annotation

注册单例 Bean,使用 BeanDefinition 对象,重复注册会 panic。

func RegisterBeanFn

func RegisterBeanFn(fn interface{}, tags ...string) *SpringCore.Annotation

通过构造函数注册单例 Bean,不指定名称,重复注册会 panic。

func RegisterNameBean

func RegisterNameBean(name string, bean interface{}) *SpringCore.Annotation

注册单例 Bean,需指定名称,重复注册会 panic。

func RegisterNameBeanFn

func RegisterNameBeanFn(name string, fn interface{}, tags ...string) *SpringCore.Annotation

通过构造函数注册单例 Bean,需指定名称,重复注册会 panic。

func RunApplication

func RunApplication(configLocation ...string)

快速启动 SpringBoot 应用。

func SetProfile

func SetProfile(profile string)

设置运行环境

func SetProperty

func SetProperty(name string, value interface{})

设置属性值,属性名称不支持大小写。

func WireBean

func WireBean(bean interface{})

绑定外部指定的 Bean

Types

type Application

type Application struct {
	AppContext     ApplicationContext // 应用上下文
	ConfigLocation []string           // 配置文件目录
}

定义 SpringBoot 应用。

func (*Application) ShutDown

func (app *Application) ShutDown()

BootStarter.AppRunner.$ShutDown

func (*Application) Start

func (app *Application) Start()

BootStarter.AppRunner.$Start

type ApplicationContext

type ApplicationContext interface {
	// 继承 SpringContext 的功能。
	SpringCore.SpringContext

	// 安全的启动一个 goroutine。
	SafeGoroutine(fn GoFunc)

	// 等待所有 goroutine 退出。
	Wait()
}

定义 Application 上下文。

type ApplicationEvent

type ApplicationEvent interface {
	OnStartApplication(ctx ApplicationContext) // 应用启动的事件
	OnStopApplication(ctx ApplicationContext)  // 应用停止的事件
}

应用运行过程中的事件。

type CommandLineRunner

type CommandLineRunner interface {
	Run()
}

定义命令行启动器接口。

type ConfigMapPropertySource

type ConfigMapPropertySource struct {
	// contains filtered or unexported fields
}

基于 k8s ConfigMap 的属性源

func NewConfigMapPropertySource

func NewConfigMapPropertySource(filename string) *ConfigMapPropertySource

构造函数

func (*ConfigMapPropertySource) Load

func (p *ConfigMapPropertySource) Load(profile string) map[string]interface{}

func (*ConfigMapPropertySource) Name

func (p *ConfigMapPropertySource) Name() string

type DefaultApplicationContext

type DefaultApplicationContext struct {
	*SpringCore.DefaultSpringContext
	// contains filtered or unexported fields
}

定义 ApplicationContext 的默认版本。

func (*DefaultApplicationContext) SafeGoroutine

func (ctx *DefaultApplicationContext) SafeGoroutine(fn GoFunc)

安全地启动一个 goroutine。

func (*DefaultApplicationContext) Wait

func (ctx *DefaultApplicationContext) Wait()

等待所有 goroutine 安全地退出。

type DefaultPropertySource

type DefaultPropertySource struct {
	// contains filtered or unexported fields
}

基于默认配置文件的属性源

func NewDefaultPropertySource

func NewDefaultPropertySource(fileLocation string) *DefaultPropertySource

构造函数

func (*DefaultPropertySource) Load

func (p *DefaultPropertySource) Load(profile string) map[string]interface{}

func (*DefaultPropertySource) Name

func (p *DefaultPropertySource) Name() string

type GoFunc

type GoFunc func()

type PropertySource

type PropertySource interface {
	// 属性源的名称
	Name() string

	// 加载属性文件,profile 配置文件剖面。
	Load(profile string) map[string]interface{}
}

属性源

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL