不使用 @Autowired 和 @Resource, 它是怎么注入的?
项目: el-admin
后台
代码如下
deptService
对象没有用过注解, 为什么还能调用 deptService.download
? 有没有大佬知道这个对象是怎么被实例化的?
完整的 DeptController
代码如下
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.system.rest;
import cn.hutool.core.collection.CollectionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.system.domain.Dept;
import me.zhengjie.modules.system.service.DeptService;
import me.zhengjie.modules.system.service.dto.DeptDto;
import me.zhengjie.modules.system.service.dto.DeptQueryCriteria;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author Zheng Jie
* @date 2019-03-25
*/
@RestController
@RequiredArgsConstructor
@Api(tags = "系统:部门管理")
@RequestMapping("/api/dept")
public class DeptController {
private final DeptService deptService;
private static final String ENTITY_NAME = "dept";
@ApiOperation("导出部门数据")
@GetMapping(value = "/download")
@PreAuthorize("@el.check('dept:list')")
public void exportDept(HttpServletResponse response, DeptQueryCriteria criteria) throws Exception {
deptService.download(deptService.queryAll(criteria, false), response);
}
// ...
}
回复
1个回答

test
2024-06-29
是使用 Lombok
的 @RequiredArgsConstructor
注解实现构造函数注入的,详细可查看Lombok文档@RequiredArgsConstructor,该注解可以为所有未初始化的 final
字段以及标记为 @NonNull
的字段生成构造函数。
@RequiredArgsConstructor
generates a constructor with 1 parameter for each field that requires special handling. All non-initializedfinal
fields get a parameter, as well as any fields that are marked as@NonNull
that aren't initialized where they are declared. For those fields marked with@NonNull
, an explicit null check is also generated. The constructor will throw aNullPointerException
if any of the parameters intended for the fields marked with@NonNull
containnull
. The order of the parameters match the order in which the fields appear in your class.
另外可查看Spring Boot官方文档Spring Beans and Dependency Injection中关于依赖注入的相关说明。
We generally recommend using constructor injection to wire up dependencies.
import org.springframework.stereotype.Service;
@Service
public class MyAccountService implements AccountService {
private final RiskAssessor riskAssessor;
public MyAccountService(RiskAssessor riskAssessor) {
this.riskAssessor = riskAssessor;
}
// ...
}
回复

适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容