RequiredArgsConstructor

2022-08-21 13:54:20 浏览数 (2)

分享一个lombok注解@RequiredArgsConstructor

同样和@AllArgsConstructor支持onConstructor参数

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

例如:

代码语言:javascript复制
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;
    }

}

生成后结果:

代码语言:javascript复制
//
// 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即可

就像这样:

代码语言:javascript复制
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;
    }

}

实际:

代码语言:javascript复制
//
// 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;
    }
}

0 人点赞