阿超
>
RequiredArgsConstructor
“世间事,除了生死,哪一件不是闲事。”——仓央嘉措《地空》。
分享一个lombok
注解@RequiredArgsConstructor
同样和@AllArgsConstructor
支持onConstructor
参数
区别在于,@RequiredArgsConstructor
只针对final
的字段生成带参构造器
例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| package com.ruben.simplescaffold.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruben.simplescaffold.entity.UserDetail; import com.ruben.simplescaffold.manager.JsonManager; import com.ruben.simplescaffold.mapper.UserDetailMapper; import com.ruben.simplescaffold.service.IUserDetailService; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;
@Service @Transactional(rollbackFor = Exception.class) @RequiredArgsConstructor(onConstructor = @__(@Autowired)) public class UserDetailServiceImpl extends ServiceImpl<UserDetailMapper, UserDetail> implements IUserDetailService { private final JsonManager jsonManager;
@Override public JsonManager getJsonManager() { return jsonManager; }
}
|
生成后结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
package com.ruben.simplescaffold.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruben.simplescaffold.entity.UserDetail; import com.ruben.simplescaffold.manager.JsonManager; import com.ruben.simplescaffold.mapper.UserDetailMapper; import com.ruben.simplescaffold.service.IUserDetailService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;
@Service @Transactional( rollbackFor = {Exception.class} ) public class UserDetailServiceImpl extends ServiceImpl<UserDetailMapper, UserDetail> implements IUserDetailService { private final JsonManager jsonManager;
public JsonManager getJsonManager() { return this.jsonManager; }
@Autowired public UserDetailServiceImpl(JsonManager jsonManager) { this.jsonManager = jsonManager; } }
|
如果有循环依赖,我们再加一个@Lazy
即可
就像这样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| package com.ruben.simplescaffold.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruben.simplescaffold.entity.UserDetail; import com.ruben.simplescaffold.manager.JsonManager; import com.ruben.simplescaffold.mapper.UserDetailMapper; import com.ruben.simplescaffold.service.IUserDetailService; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;
@Service @Transactional(rollbackFor = Exception.class) @RequiredArgsConstructor(onConstructor = @__({@Autowired,@Lazy})) public class UserDetailServiceImpl extends ServiceImpl<UserDetailMapper, UserDetail> implements IUserDetailService { private final JsonManager jsonManager; private final IUserDetailService userDetailService;
@Override public JsonManager getJsonManager() { return jsonManager; }
}
|
实际:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
package com.ruben.simplescaffold.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruben.simplescaffold.entity.UserDetail; import com.ruben.simplescaffold.manager.JsonManager; import com.ruben.simplescaffold.mapper.UserDetailMapper; import com.ruben.simplescaffold.service.IUserDetailService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;
@Service @Transactional( rollbackFor = {Exception.class} ) public class UserDetailServiceImpl extends ServiceImpl<UserDetailMapper, UserDetail> implements IUserDetailService { private final JsonManager jsonManager; private final IUserDetailService userDetailService;
public JsonManager getJsonManager() { return this.jsonManager; }
@Autowired @Lazy public UserDetailServiceImpl(JsonManager jsonManager, IUserDetailService userDetailService) { this.jsonManager = jsonManager; this.userDetailService = userDetailService; } }
|