在Spring4.x中增加了新的特性:
如果类只提供了一个带参数的构造方法,则不需要对对其内部的属性写@Autowired注解,Spring会自动为你注入属性;
配合lombok的@RequiredArgsConstructor使用体验很好;
@RequiredArgsConstructor会将类的每一个final字段或者non-null字段生成一个构造方法
例子
@RestController
@RequiredArgsConstructor
@RequestMapping(Constant.MAPPING_BUSINESS_PREFIX)
@Api(tags ="XXX")
public class ArmyEmployeeController
{
private final ArmyEmployeeServicearmyEmployeeService;
}
@Service
public class ArmyEmployeeServiceImpl implements ArmyEmployeeService
{
}