Starting with Spring 4.3, if a class, which is configured as a Spring bean, has only one constructor, the Autowired annotation can be omitted and Spring will use that constructor and inject all necessary dependencies.
如果一个类被配置为一个spring bean,并且只有一个构造器,那么这个构造器的所有参数默认根据autowired匹配注入,而不需要在构造器上加入注解@Autowired
例如:
@Configuration
public class SecuritySecureConfig {
private final String adminContextPath;
//adminContextPath字段会自动通过构造器注入,而不需要加上注解@Autowired
public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
}