spring减少@Autowired代码方法@RequiredArgsConstructor(onConstructor_ = @Autowired)

spring中减少@Autowired代码方法

  • spring依赖注入常规操作,大量出现@Autowired,代码如下
@Service
public class DigitalMeetingServiceImpl extends ServiceImpl<DigitalMeetingMapper, DigitalMeeting> implements IDigitalMeetingService {

    @Autowired
    private DigitalMeetingMapper meetingMapper;
    @Autowired
    private IDigitalMeetingSignService meetingSignService;
    @Autowired
    private IDigitalPersonService digitalPersonService;
    @Autowired
    private RemoteFileAttachService remoteFileAttachService;
    @Autowired
    private RemoteFileService remoteFileService;
    @Autowired
    private ICommonService commonService;
    @Autowired
    private IDigitalPersonService iDigitalPersonService;
    @Autowired
    private IDigitalUniProjectService uniProjectService;
    @Autowired
    private IDigitalMeetingLogService meetingLogService;
    @Autowired
    private IDigitalFileService iDigitalFileService;

}
  • 使用lombok的@RequiredArgsConstructor注解
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class DigitalMeetingServiceImpl extends ServiceImpl<DigitalMeetingMapper, DigitalMeeting> implements IDigitalMeetingService {

    private final DigitalMeetingMapper meetingMapper;
    private final IDigitalMeetingSignService meetingSignService;
    private final IDigitalPersonService digitalPersonService;
    private final RemoteFileAttachService remoteFileAttachService;
    private final RemoteFileService remoteFileService;
    private final ICommonService commonService;
    private final IDigitalPersonService iDigitalPersonService;
    private final IDigitalUniProjectService uniProjectService;
    private final IDigitalMeetingLogService meetingLogService;
    private final IDigitalFileService iDigitalFileService;
    
}

注意 : 依赖注入的对象需要加上final

  • 使用方法可以参考源码
/**
 * Generates a constructor with required arguments.
 * Required arguments are final fields and fields with constraints such as {@code @NonNull}.
 * <p>
 * Complete documentation is found at <a href="https://projectlombok.org/features/Constructor">the project lombok features page for &#64;Constructor</a>.
 * <p>
 * Even though it is not listed, this annotation also has the {@code onConstructor} parameter. See the full documentation for more details.
 * 
 * @see NoArgsConstructor
 * @see AllArgsConstructor
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface RequiredArgsConstructor {
	/**
	 * If set, the generated constructor will be private, and an additional static 'constructor'
	 * is generated with the same argument list that wraps the real constructor.
	 * 
	 * Such a static 'constructor' is primarily useful as it infers type arguments.
	 * 
	 * @return Name of static 'constructor' method to generate (blank = generate a normal constructor).
	 */
	String staticName() default "";
	
	/**
	 * Any annotations listed here are put on the generated constructor.
	 * The syntax for this feature depends on JDK version (nothing we can do about that; it's to work around javac bugs).<br>
	 * up to JDK7:<br>
	 *  {@code @RequiredArgsConstructor(onConstructor=@__({@AnnotationsGoHere}))}<br>
	 * from JDK8:<br>
	 *  {@code @RequiredArgsConstructor(onConstructor_={@AnnotationsGohere})} // note the underscore after {@code onConstructor}.
	 * 
	 * @return List of annotations to apply to the generated constructor.
	 */
	AnyAnnotation[] onConstructor() default {};
	
	/**
	 * Sets the access level of the constructor. By default, generated constructors are {@code public}.
	 * 
	 * @return The constructor will be generated with this access modifier.
	 */
	AccessLevel access() default lombok.AccessLevel.PUBLIC;
	
	/**
	  * Placeholder annotation to enable the placement of annotations on the generated code.
	  * @deprecated Don't use this annotation, ever - Read the documentation.
	  */
	@Deprecated
	@Retention(RetentionPolicy.SOURCE)
	@Target({})
	@interface AnyAnnotation {}
}

用法在源码中已经写到@RequiredArgsConstructor(onConstructor_={@AnnotationsGohere})

  • 附上我的lombok的依赖
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
    <scope>provided</scope>
</dependency>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值