java无法统计应用程序。从2.0.6更新SpringBoot之后。发布到2.1.0。释放
我有一个基本的SpringBoot 2.0.6。发布应用程序。使用Spring初始值设定项、JPA、嵌入式Tomcat、Thymeleaf模板引擎,并将其打包为具有restful体系结构的可执行JAR 我更新了SpringBoot应用程序。从2.0.6开始。发布到2.1.0。释放
我有一个配置类:
public class DevApplicationConfig {
@Autowired
private ErrorAttributes errorAttributes;
@Bean
public AppErrorController appErrorController(){
return new AppErrorController(errorAttributes);
}
}
但在更改之后,我在启动应用程序时在Eclipse中遇到了以下错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'appErrorController', defined in class path resource [io/tdk/config/DevApplicationConfig.class], could not be registered. A bean with that name has already been defined in file [/Users/nunet/Documents/workspace-sts-3.9.2.RELEASE/tdk/target/classes/io/tdk/web/controllers/AppErrorController.class] and overriding is disabled.
@Controller
public class AppErrorController implements ErrorController {
private static final Logger LOG = LoggerFactory.getLogger(AppErrorController.class);
/**
* Error Attributes in the Application
*/
private ErrorAttributes errorAttributes;
private final static String ERROR_PATH = "/error";
@Autowired
private EmailService emailService;
@Value("${systemadmin.email}")
private String systemAdminEmail;
@Value("${webmaster.email}")
private String webMasterEmail;
/**
* Controller for the Error Controller
* @param errorAttributes
*/
public AppErrorController(ErrorAttributes errorAttributes) {
this.errorAttributes = errorAttributes;
}
/**
* Supports the HTML Error View
* @param request
* @return
*/
@RequestMapping(value = ERROR_PATH, produces = "text/html")
public ModelAndView errorHtml(HttpServletRequest request, WebRequest webRequest) {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setTo(systemAdminEmail);
mailMessage.setSubject("System Error !");
mailMessage.setText(getErrorAttributes(request, webRequest, true).toString());
mailMessage.setFrom(webMasterEmail);
emailService.sendGenericEmailMessage(mailMessage);
return new ModelAndView(serverContextPath +"/errors/error", getErrorAttributes(request, webRequest, true));
}
/**
* Supports other formats like JSON, XML
* @param request
* @return
*/
@RequestMapping(value = ERROR_PATH)
@ResponseBody
public ResponseEntity<Map<String, Object>> error(WebRequest webRequest, HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes( request, webRequest, true);
HttpStatus status = getStatus(request);
return new ResponseEntity<Map<String, Object>>(body, status);
}
/**
* Returns the path of the error page.
*
* @return the error path
*
*/
@Override
public String getErrorPath() {
return ERROR_PATH;
}
private Map<String, Object> getErrorAttributes(HttpServletRequest request, WebRequest webRequest,
boolean includeStackTrace) {
return this.errorAttributes.getErrorAttributes(webRequest, includeStackTrace);
}
private HttpStatus getStatus(HttpServletRequest request) {
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
if (statusCode != null) {
try {
return HttpStatus.valueOf(statusCode);
} catch (Exception ex) {
}
}
return HttpStatus.INTERNAL_SERVER_ERROR;
}
}
# 1 楼答案
将此行添加到
applicaton.propertie
的文件中# 2 楼答案
Spring Boot 2.1中做了一个更改,默认情况下禁用bean重写。这样做是为了更容易识别和纠正bean的意外重写。这里似乎发生了意外的超控
AppErrorController
用@Controller
注释,它在@Component
中生成,并且它似乎位于组件扫描覆盖的包中。当组件扫描遇到AppErrorController
时,它定义了一个名为appErrorController
的bean然后处理
DevApplicationConfig
。它包含一个@Bean
方法,该方法定义了一个名为appErrorController
的bean:在Spring Boot 2.0及更早版本中,这将导致通过组件扫描定义的
AppErrorController
bean被DevApplicationConfig
中定义的bean覆盖。如果在使用Spring Boot 2.0启动应用程序时仔细查看日志。x、 您应该看到,已经为被覆盖的bean记录了一条信息消息。在Spring Boot 2.1中,由于默认情况下禁用了bean重写,因此会导致失败要解决此问题,请从
DevApplicationConfig
中删除@Bean
方法。这允许使用组件扫描生成的定义。我建议在Spring Boot 2.0和更早的应用程序中进行同样的更改。删除不必要的覆盖会使应用程序启动更加高效,并避免应用程序日志中出现有关被覆盖bean的信息消息如果需要在Spring Boot 2.1中重写bean,可以通过设置以下属性来恢复2.0的行为:
我只建议您在异常情况下执行此操作,在这种情况下,重写是故意的,并且没有简单的方法来修改配置以避免发生这种情况