lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter注入
使用 lombok 进行setter注入(尽量优先使用setter注入)
@service
@setter(onmethod_ = {@autowired})
public class testserviceimpl implements testservice {
private testdao testdao;
}看一下编译的内容
@service
public class testserviceimpl implements testservice {
private testdao testdao;
@autowired
public void settestdao(final testdao testdao) {
this.testdao= testdao;
}
}使用 lombok 进行构造器注入
@service
@requiredargsconstructor(onconstructor_ = {@autowired})
public class testserviceimpl implements testservice {
private final testdao testdao;
}或
@service
@requiredargsconstructor(onconstructor_ = {@autowired})
public class testserviceimpl implements testservice {
@lombok.nonnull
private testdao testdao;
}编译的内容
@service
public class testserviceimpl implements testservice {
private testdao testdao;
@autowired
public void testserviceimpl(final testdao testdao) {
this.testdao= testdao;
}
}到此这篇关于优雅的使用lombok进行spring 注入的文章就介绍到这了,更多相关lombok spring 注入内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论