如何使用Lombok进行spring 注入
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;
}
}
php使用 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;
编程客栈 }
}
到此这篇关于优雅的使用Lombok进行sprin编程g 注入的文章就介绍到这了,更多相关Lombok spring 注入内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
加载中,请稍侯......
精彩评论