Gone框架介绍24 - Gone vs Spring,写给想转 Go 的 Java程序员

Gone vs Spring,写给想转 Go 的 Java程序员

Gone是我们开发的 Golang 依赖注入框架,Spring是Java的依赖注入框架;为了便于Spring程序员转Golang快速上手Gone,我们在这里对他们的依赖注入做一下对比。

依赖模块导入

Spring

在项目管理工具中引入依赖;项目启动后自动扫描加载。
在 Maven 中 引入依赖

<dependency>
    <groupId>com.github.example</groupId>
    <artifactId>example</artifactId>
    <version>1.0.0</version>
</dependency>

在 Gradle 中 引入依赖

implementation 'com.github.example:example:1.0.0'

Gone

在MasterPriest函数中加载依赖模块提供的Priest函数;在启动前执行go mod tidy将依赖的包写入go.mod文件。

func MasterPriest(cemetery gone.Cemetery) error {
   
   
	_ = goner.XormPriest(cemetery) //加载xorm的依赖
	_ = goner.GinPriest(cemetery)  //加载gin的依赖

	_ = Priest(cemetery)
	return nil
}

func main() {
   
   
    gone.Run(MasterPriest)
}

注入依赖

Spring

在结构体属性上添加@Autowired注解,表示该属性是需要依赖注入的。

@Component
public class DemoController {
   
   

    @Autowired
    private IDemo demoSvc; //该属性是需要依赖注入的
}

Goner

在结构体属性上添加gone标签,表示该属性是需要依赖注入的,可以参考Gone支持哪些方式注入?

type demoController struct {
   
   
	gone.Flag
	demoSvc     service.IDemo `gone:"*"` //该属性是需要依赖注入的
}

配置注入<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dapeng-大鹏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值