java - 无法统计应用程序。将 SpringBoot 从 2.0.6.RELEASE 更新到 2.1.0.RELEASE 后

标签 java spring spring-mvc spring-boot

我有一个基本的 SpringBoot 2.0.6.RELEASE 应用程序。使用 Spring Initializer、JPA、嵌入式 Tomcat、Thymeleaf 模板引擎,并打包为具有 Restful 架构的可执行 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;
    }

}

最佳答案

在 Spring Boot 2.1 中进行了更改以默认禁用 bean 覆盖。这样做是为了更容易识别和更正 bean 的意外覆盖。此处似乎发生了意外覆盖。

AppErrorController@Controller 注释,它在 @Component 中生成,它似乎位于组件扫描覆盖的包中。当组件扫描遇到 AppErrorController 时,它定义了一个名为 appErrorController 的 bean。

然后处理

DevApplicationConfig。它包含一个 @Bean 方法,该方法定义了一个名为 appErrorController 的 bean:

 @Bean
 public AppErrorController appErrorController(){
     return new AppErrorController(errorAttributes);
 }

在 Spring Boot 2.0 及更早版本中,这将导致通过组件扫描定义的 AppErrorController bean 被 DevApplicationConfig 中定义的 bean 覆盖。如果您在使用 Spring Boot 2.0.x 启动应用程序时仔细查看日志,您应该会看到已为覆盖的 bean 记录了一条信息消息。在 Spring Boot 2.1 中,由于默认禁用 bean 覆盖,它会导致失败。

要解决此问题,请从 DevApplicationConfig 中删除 appErrorController @Bean 方法。这允许使用由组件扫描生成的定义。我建议在 Spring Boot 2.0 和更早的应用程序中进行相同的更改。删除不必要的覆盖使应用程序启动效率更高一些,并避免使用有关 bean 被覆盖的信息消息使应用程序的日志困惑。

如果您需要能够覆盖 Spring Boot 2.1 中的 bean,您可以通过设置一个属性来恢复 2.0 的行为:

spring.main.allow-bean-definition-overriding=true

我只建议您在有意覆盖的异常情况下执行此操作,并且没有简单的方法来重新配置配置以使其不会发生。

关于java - 无法统计应用程序。将 SpringBoot 从 2.0.6.RELEASE 更新到 2.1.0.RELEASE 后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53103991/

相关文章:

java - 改变点的方向

Java:showInputDialog 和 ProcessBuilder

spring - 如何编写在 spring-data-hatoas 中公开资源或资源列表的分页 Controller

java - org.springframework.beans.factory.BeanDefinitionStoreException : Failed to read candidate component class: file [******]

java - Ant 与 hudson war

java - 如何在Java中检查目录是否为空

java - 抽象类中 LinkedHashMap 的初始容量应该始终为零吗?

java - 在 @GetMapping 映射方法内部是否可以获取 value 属性中定义的相对 URL

java - 有条件 :action in spring

java - Spring模板Hello world项目不起作用