GVKun编程网logo

如何在Spring Boot 2.1.0启动程序配置中将spring.main.allow-bean-definition- overriding设置为true

13

针对如何在SpringBoot2.1.0启动程序配置中将spring.main.allow-bean-definition-overriding设置为true这个问题,本篇文章进行了详细的解答,同时本

针对如何在Spring Boot 2.1.0启动程序配置中将spring.main.allow-bean-definition- overriding设置为true这个问题,本篇文章进行了详细的解答,同时本文还将给你拓展Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-defin...、idea:spring initializr 无 web 勾选,maven 方式搭建 springboot 项目。jdk7 创建 springboot 项目的版本不兼容问题。、IntelliJ IDEA 使用 spring Initializr 快速搭建 spring boot 项目遇到的坑、setting spring.main.allow-bean-definition-overriding=true 这个参数怎么设置?等相关知识,希望可以帮助到你。

本文目录一览:

如何在Spring Boot 2.1.0启动程序配置中将spring.main.allow-bean-definition- overriding设置为true

如何在Spring Boot 2.1.0启动程序配置中将spring.main.allow-bean-definition- overriding设置为true

如何解决如何在Spring Boot 2.1.0启动程序配置中将spring.main.allow-bean-definition- overriding设置为true?

Spring Boot的ErrorAttributesbean由定义ErrorMvcAutoConfiguration。带有注释,@ConditionalOnMissingBean因此如果ErrorAttributes已经定义了bean ,它将取消。由于ErrorsConfig类定义的Bean 试图覆盖Boot的ErrorAttributesbean而不是使其退出,因此ErrorsConfig必须在Boot的ErrorMvcAutoConfiguration类之后对您的类进行处理。这意味着您的起动器有订购问题。

可以使用@AutoConfigureBefore和来控制自动配置类的处理顺序@AutoConfigureAfter。假设ErrorsConfig本身就是在其中注册的自动配置类spring.factories,则可以通过使用注释解决问题@AutoConfigureBefore(ErrorMvcAutoConfiguration.class)。进行此更改后,ErrorsConfigErrorAttributesErrorMvcAutoConfiguration尝试进行定义之前定义其bean,这将导致BootErrorsAttributeBean 的自动配置退出。

解决方法

我维护了一个spring-boot-
starter,用于定制例如在调用未知终点时返回的错误属性。这是通过重写org.springframework.boot.web.servlet.error.ErrorAttributes
bean完成的。

在2.0.6上一切正常,但是默认情况下2.1.0禁用Bean覆盖,这使得启动器现在失败并显示以下消息。

在类路径资源[com / mycompany / springboot / starter / config /
ErrorsConfig.class]中定义了名称为’errorAttributes’的无效bean定义:无法注册bean定义[root
bean:class [null]; scope =; abstract = false; lazyInit = false; autowireMode
= 3; dependencyCheck = 0; autowireCandidate = true; primary = false;
factoryBeanName = com.mycompany.springboot.starter.config.ErrorsConfig;
factoryMethodName = errorAttributes; initMethodName = null;
destroyMethodName =(推断); 在类路径资源[com / mycompany / springboot / starter /
config / ErrorsConfig.class]中为bean’errorAttributes’定义:已经存在[root bean:class
[null]; scope =; abstract = false; lazyInit = false; autowireMode = 3;
dependencyCheck = 0; autowireCandidate = true; primary = false;
factoryBeanName =
org.springframework.boot.autoconfigure.web.servlet。error.ErrorMvcAutoConfiguration;
factoryMethodName = errorAttributes; initMethodName = null;
destroyMethodName =(推断); 在类路径资源[org / springframework / boot / autoconfigure
/ web / servlet / error / ErrorMvcAutoConfiguration.class]中定义

如文档所述,将spring.main.allow-bean-definition-overriding属性设置为true可解决此问题。我的问题是如何
在启动程序中 执行此操作(我不希望启动程序的用户必须更改其application.properties文件(针对启动程序的特定内容))?

我尝试将@PropertySource(“
classpath:/com/mycompany/starter/application.properties”)批注添加到我的@Configuration中,并在该文件中定义了该属性,但它不起作用。

我想念什么?有什么办法可以让我的配置覆盖该bean?

这是配置的(简化)源代码:

@Configuration
@PropertySource("classpath:/com/mycompany/starter/application.properties")
public class ErrorsConfig {
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    @Bean
    public ErrorAttributes errorAttributes() {
        return new DefaultErrorAttributes() {
            @SuppressWarnings("unchecked")
            @Override
            public Map<String,Object> getErrorAttributes(WebRequest request,boolean includeStackTrace) {
                Map<String,Object> errorAttributes = super.getErrorAttributes(request,includeStackTrace);
                // CustomeError is a (simplified) bean of the error attributes we should return.
                CustomError err = new CustomError("myErrorCode",(String) errorAttributes.get("error"));
                return OBJECT_MAPPER.convertValue(err,Map.class);
            }
        };
    }
}

并且我的资源文件com / mycompany / starter / application.properties包含

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

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-defin...

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-defin...

Description:

The bean ''characterEncodingFilter'', defined in class path resource [zipkin/autoconfigure/ui/ZipkinUiAutoConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/HttpEncodingAutoConfiguration.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

问题描述:运行spring cloud子项目出现以上异常。

问题分析:英语水平有限,大致可以看出zipkin相关的东西注册不了,而zipkin不是自己写的代码。

解决问题:综合上述,加上最近几天碰了好几次上面的现象,得出的结论是依赖之间的版本不匹配。建议对照maven repository调整相关依赖的版本。

idea:spring initializr 无 web 勾选,maven 方式搭建 springboot 项目。jdk7 创建 springboot 项目的版本不兼容问题。

idea:spring initializr 无 web 勾选,maven 方式搭建 springboot 项目。jdk7 创建 springboot 项目的版本不兼容问题。

一、idea 使用 spring initializr 不选择 web 搭建 springboot 项目

1.file => new => project

 

 

2. 直接 next 到 finish 结束。

 

 

3. 完成后创建 controller 用例测试

 

4.pom 文件

 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cc.ash</groupId>
    <artifactId>boot-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boot-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--  手动添加web依赖,@restController @requestMapping注解等      -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

5.application.properties

#端口号修改。默认8080
server.port=

 

6.springboot 入口启动类

package cc.ash.bootdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@SpringBootApplication
@Controller
public class BootDemoApplication {


    @RequestMapping("/boot")
    @ResponseBody
    public String bootTest() {

        return "hello springboot";
    }
    public static void main(String[] args) {
        SpringApplication.run(BootDemoApplication.class, args);
    }

}

 

7. 新建包下 controller 测试类

package cc.ash.bootdemo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @RequestMapping(name="springboot test", value = "/test")
    public String test() {
        return "booted";
    }
}

 

 

8. 运行结果

新建包下 controller 测试类:访问页面

 

springboot 启动类中访问 404。改 requestMapping、rerun,做了很多操作到怀疑人生。重启 idea,世界终于正常了。

 

能正常访问的结果如下

 

 

二、maven 搭建 springboot 项目

 

1.jdk7 的情况下

2.maven 搭建

 

 

创建完成后的项目结构和 pom 文件。一个字就是干净

 

3. 手动添加 pom 依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>


<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>


<groupId>cc.ash</groupId>
<artifactId>boot-demo</artifactId>
<version>1.0-SNAPSHOT</version>


<properties>
<java.version>1.8</java.version>
</properties>

<!-- =================jdk1.7与springboot版本不兼容======================= -->
<!--<properties>
<java.version>1.7</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.myproject.Application</start-class>
</properties>-->

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- 手动添加web依赖,@restController @requestMapping注解等 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

 

 

maven 导入依赖(未选择自动导入的情况下)。

 

4. 编写测试用例代码

1). 测试 controller:

  TestController

package cc.ash.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @RequestMapping(name="maven手动创建springboot",value={"/manual", "mvnManual"})
    @ResponseBody
    public String test() {

        return "maven manual";
    }

}

 

2. 启动入口:

DemoApplication
package cc.ash;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

 

 

3. 完成后代码结构

 

5. 访问测试

 

6.jdk1.7 错误留存

不兼容问题

https://blog.csdn.net/wj197927/article/details/79557017

https://www.jianshu.com/p/95d8a0cf0244

 

idea 日志记录(非控制台日志,控制台日志如上)

 7.springboot 项目结构导致 controller 访问 404 的问题

自建的所有需要扫描注解的类,必须与启动类的包平级或者在其之下。如启动类在包【aa.bb】,其他需要扫描的类必须在【aa.bb】中或【aa.bb】之下的包里。

参考链接:

详细说明的:https://www.cnblogs.com/remember-me/p/10091126.html

直接上解决方法的:https://blog.csdn.net/dong__CSDN/article/details/85342180

 

IntelliJ IDEA 使用 spring Initializr 快速搭建 spring boot 项目遇到的坑

IntelliJ IDEA 使用 spring Initializr 快速搭建 spring boot 项目遇到的坑

>> 号外:关注 “Java 精选” 公众号,菜单栏 -> 聚合 -> 干货分享,回复关键词领取视频资料、开源项目。

前言

以前用习惯了 sts,今天尝试使用 IDEA 搭建 springboot 项目工程,却遇到了个小坑。不知道你们是否遇到过,分享如下:
首先我的 maven 使用的是 3.5.3
首先创建 springboot:
使用的是默认 spring 官网的镜像。这里改动不大。
选了个 web。
项目创建成功
在启动类右键,没有 run 方法
在 pom.xml 上右键,将其添加为 maven 项目
然后发现 Test 模块报错。
查看 pom.xml 文件,发现 2.3.5Release 版本变红,怀疑是版本不对应问题。
创建时发现没有 2.3.4
于是试了一下阿里云的镜像:

新建个项目

成功运行!
得时版本不对应问题,应该是 SpringInitializar 创建的 Springboot 版本太高。
于是我直接尝试了在第一个创建的项目中,将之前的 “2.3.5" 改成了:
现也能成功运行。

blog.csdn.net/qq_45273552/article/details/109435442

往期精选   点击标题可跳转

面试官问:为什么 MySQL 中的 utf8 并不是真正的 UTF-8 编码?

为什么程序员都说 SELECT * 效率低,那究竟是什么原因造成的?

厉害了,淘宝千万级并发,14 次分布式架构演进

Spring Cloud 中 Zuul 网关到底有何牛逼之处?竟然这么多人在用!

一次神奇的 sql 查询经历,group by 慢查询优化记录

腾讯 Tendis 正式开源,兼容 Redis 协议企业级分布式高性能 KV 存储数据库

中国铁路 12306 网站的高并发架构带来的思考?研究分析后,果然超牛逼…

IDEA JetBrains 推出 Mono 编程字体真牛逼,更适合程序开发人员!

为什么阿里规范中要求代码禁用 static 修饰 SimpleDateFormat?

心惊肉跳,新来的程序媛妹纸 rm -rf 把公司整个数据库删没了!

为什么很多 Spring Boot 开发者放弃了 Tomcat,选择了 Undertow ?

点个赞,就知道你 “在看”!

本文分享自微信公众号 - Java 精选(w_z90110)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与 “OSC 源创计划”,欢迎正在阅读的你也加入,一起分享。

setting spring.main.allow-bean-definition-overriding=true 这个参数怎么设置?

setting spring.main.allow-bean-definition-overriding=true 这个参数怎么设置?

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
 

关于如何在Spring Boot 2.1.0启动程序配置中将spring.main.allow-bean-definition- overriding设置为true的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-defin...、idea:spring initializr 无 web 勾选,maven 方式搭建 springboot 项目。jdk7 创建 springboot 项目的版本不兼容问题。、IntelliJ IDEA 使用 spring Initializr 快速搭建 spring boot 项目遇到的坑、setting spring.main.allow-bean-definition-overriding=true 这个参数怎么设置?等相关知识的信息别忘了在本站进行查找喔。

本文标签:

上一篇Spring Security中角色和GrantedAuthority之间的区别(spring security 角色)

下一篇Python-如何取消转义的反斜杠字符串?(python 取消转义)