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 @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>










