文章目录
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:"*"` //该属性是需要依赖注入的
}