我有一个基本的SpringBoot 2.0.6.RELEASE应用程序。使用Spring Initializer,JPA,嵌入式Tomcat,Thymeleaf模板引擎并将其打包为具有宁静架构的可执行JAR 我更新了SpringBoot应用程序。从2.0.6.RELEASE到2.1.0.RELEASE,
我有这个配置类:
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;
}
}
答案 0 :(得分:2)
Spring Boot 2.1中进行了更改,默认情况下禁用Bean覆盖。这样做是为了使其更容易识别和更正bean的意外覆盖。意外的超载就是这里发生的事情。
AppErrorController
带有@Controller
的注释,该@Component
包含在AppErrorController
中,它似乎位于组件扫描覆盖的程序包中。当组件扫描遇到appErrorController
时,它将定义一个名为DevApplicationConfig
的bean。
@Bean
。它包含一个appErrorController
方法,该方法定义了一个名为 @Bean
public AppErrorController appErrorController(){
return new AppErrorController(errorAttributes);
}
的bean:
AppErrorController
在Spring Boot 2.0和更早的版本中,这将导致DevApplicationConfig
中定义的bean覆盖通过组件扫描定义的appErrorController
bean。如果在使用Spring Boot 2.0.x启动应用程序时仔细查看日志,应该会看到已记录了有关被覆盖的bean的信息消息。在Spring Boot 2.1中,由于默认情况下禁用Bean覆盖而导致失败。
要解决此问题,请从@Bean
中删除DevApplicationConfig
spring.main.allow-bean-definition-overriding=true
方法。这允许使用由组件扫描生成的定义。我建议在Spring Boot 2.0和更早的应用程序中进行相同的更改。删除不必要的覆盖将使应用程序启动效率更高一点,并避免由于有关被覆盖的bean的信息消息而使应用程序的日志混乱。
如果您需要能够在Spring Boot 2.1中覆盖Bean,则可以通过设置属性来恢复2.0的行为:
minHeap
仅当您处于特殊情况下,即您有意进行覆盖并且没有简单的方法来对配置进行重新整理以使其不会发生时,我才建议这样做。
答案 1 :(得分:1)
将此行添加到applicaton.propertie
的文件中
spring.main.allow-bean-definition-overriding=true