RequiredArgsConstructor

简介: RequiredArgsConstructor

“世间事,除了生死,哪一件不是闲事。”——仓央嘉措《地空》。

分享一个lombok注解@RequiredArgsConstructor

同样和@AllArgsConstructor支持onConstructor参数

区别在于,@RequiredArgsConstructor只针对final的字段生成带参构造器

例如:

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;
/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/7/30 10:31
 */
@Service
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserDetailServiceImpl extends ServiceImpl<UserDetailMapper, UserDetail> implements IUserDetailService {
    private final JsonManager jsonManager;
    /**
     * 获取jsonManager
     *
     * @return jsonManager
     */
    @Override
    public JsonManager getJsonManager() {
        return jsonManager;
    }
}


生成后结果:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
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即可

就像这样:

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;
/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/7/30 10:31
 */
@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;
    /**
     * 获取jsonManager
     *
     * @return jsonManager
     */
    @Override
    public JsonManager getJsonManager() {
        return jsonManager;
    }
}

实际:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
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;
    }
}
相关文章
|
6月前
|
Java Spring
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
`@RequiredArgsConstructor(onConstructor = @__(@Autowired))` 是一个注解,通常用于在类中生成一个包含所有`final`和`@NonNull`注解的字段的构造函数,并且对这些字段进行自动注入(使用Spring框架的`@Autowired`注解)。让我们详细解释一下: 1. **`@RequiredArgsConstructor` 注解:** - **作用:** Lombok 提供的注解,用于自动生成类的构造函数。 - **生成的构造函数:** 生成一个包含所有`final`字段的构造函数,同时也包括被`@NonNull`注解
664 1
|
4月前
|
Java Spring
ApplicationContextAware 的理解和应用
ApplicationContextAware 的理解和应用
39 2
|
5月前
|
Java
@RequiredArgsConstructor(onConstructor_ = @Autowired)报错
@RequiredArgsConstructor(onConstructor_ = @Autowired)报错
|
4月前
|
Java Spring
@PostConstruct注解的使用
@PostConstruct注解的使用
103 0
@RequiredArgsConstructor(onConstructor=@_(@Autowired))是什么语法?
@RequiredArgsConstructor(onConstructor=@_(@Autowired))是什么语法?
233 0
|
6月前
|
Java Spring 容器
SpringAop
SpringAop
53 0
|
Java Spring 容器
@PostConstruct使用
@PostConstruct使用
61 0
|
XML 监控 Java
SpringAOP介绍与使用
SpringAOP介绍与使用
68 0
|
Java Spring
|
Java 数据库连接 数据库
SpringAOP(三)
SpringAOP(三)