@Autowire注入依赖并不是推荐的做法,因为不方便debug。对应的setter的@Autowire注入和有参构造的@Autowire注入是官方推荐的方式。而参构造注入可以使用lombok的@RequiredArgsConstructor替代
pom
<!-- lombak-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-core</artifactId>
<version>3.0.0</version>
<scope>compile</scope>
</dependency>
java代码实例
- 加上@RequiredArgsConstructor注解
- 所有注入的成员属性需加上private final
@RestController
@RequiredArgsConstructor
public class AnalysisController {
private final AnalysisService analysisService;
private final DataSource dataSource;
@PostMapping("/xxx/xxx")
public ResultBody<List<Integer>> analysisEvent() {
return analysisService.analysisEvent();
}
}
评论区