本文主要是介绍如何使用Lombok进行spring 注入,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效...
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;
}
}使用 Lojsmbok 进行构造器注入
@Service
@ZTBBKaabJXRequiredArgsConstructor(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;
China编程 }
}到此这篇关于优雅的使用Lombok进行sprin编程g 注入的文章就介绍到这了,更多相关Lombok spring 注入内容请搜索China编程(www.chinasem.cn)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程China编程(www.chinasem.cn)!
这篇关于如何使用Lombok进行spring 注入的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!