Spring Boot 自动配置覆盖自定义(elasticsearchTemplate)配置

     2023-02-26     292

关键词:

【中文标题】Spring Boot 自动配置覆盖自定义(elasticsearchTemplate)配置【英文标题】:Spring boot auto configuration overriding custom (elasticsearchTemplate) configuration 【发布时间】:2018-08-13 09:14:12 【问题描述】:

我创建了以下配置(我复制了@Lazy,因为我无法使其工作)

@Lazy(true)
@Configuration
public class ElasticConfiguration

   private Logger logger = LoggerFactory.getLogger(ElasticConfiguration.class);

   @Value("$elasticsearch.host")
   private String esHost;

   @Value("$elasticsearch.port")
   private int esPort;

   @Value("$elasticsearch.clustername")
   private String esClusterName;

   @Lazy(true)
   @Bean
   public Client client() throws Exception
   
      Builder builder = Settings.builder();
      // builder.put("client.transport.sniff", true);
      Settings settings = builder.put("cluster.name", esClusterName).build();
      TransportClient client = new PreBuiltTransportClient(settings);
      InetAddress adress = InetAddress.getByName(esHost);
      client.addTransportAddress(new InetSocketTransportAddress(adress, esPort));
      logger.debug(
         "ES Client built with cluster.name;" + esClusterName + ";host;" + esHost + ";port;" + esPort + ";client.transport.sniff;"
            + builder.get("client.transport.sniff"));
      return client;
   

   @Lazy(true)
   @Bean
   public ElasticsearchOperations elasticsearchTemplate() throws Exception
   
      return new ElasticsearchTemplate(client());
   

我无法进行延迟初始化,因为我的配置似乎被 spring-boot 自动配置覆盖

使用 bean 'elasticsearchTemplate' 覆盖 bean 定义 不同的定义:替换 [Root bean: class [null];范围=; 摘要=假;懒惰初始化=真;自动线模式=3;依赖检查=0; 自动接线候选=真;主要=假; factoryBeanName=弹性配置; factoryMethodName=弹性搜索模板;初始化方法名=空; destroyMethodName=(推断);在类路径资源中定义 [xxx/xxx/ElasticConfiguration.class]] 与 [根 bean: 类 [null];范围=;摘要=假;懒惰初始化=假; 自动线模式=3;依赖检查=0;自动接线候选=真; 主要=假; factoryBeanName=org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration; factoryMethodName=弹性搜索模板;初始化方法名=空; destroyMethodName=(推断);在类路径资源中定义 [org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchDataAutoConfiguration.class]]

我也尝试在类上添加如下注解

@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)

与以下属性相关,但效果不佳

org.springframework.boot.autoconfigure.EnableAutoConfiguration=xxx.xxx.ElasticConfiguration

我该怎么办?

【问题讨论】:

【参考方案1】:

好的,所以我找到的唯一解决方案是使用以下属性

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration

请注意,使用以下注释对我不起作用...

@EnableAutoConfiguration(exclude=ElasticsearchDataAutoConfiguration.class)

编辑

事实上我的问题是我的方法elasticsearchTemplate()返回ElasticsearchOperations而不是ElasticsearchTemplate

当更改签名时,@Lazy 注释似乎起作用了。

【讨论】:

如何在 Spring Boot 应用程序中配置自定义数据库连接超时?

】如何在SpringBoot应用程序中配置自定义数据库连接超时?【英文标题】:HowtoconfigurecustomdatabaseconnectiontimeoutinSpringBootapplication?【发布时间】:2019-08-2522:27:03【问题描述】:在我的Springboot(2.0.7RELEASE)应用程序中,我无法在application... 查看详情

如何在不覆盖 Spring Boot 使用的情况下定义自定义 ObjectMapper bean

】如何在不覆盖SpringBoot使用的情况下定义自定义ObjectMapperbean【英文标题】:HowcanIdefineacustomObjectMapperbeanwithoutoverridingtheoneusedbySpringBoot【发布时间】:2018-08-1418:56:21【问题描述】:我有一个带有多个@RestController类的SpringBootWeb应用... 查看详情

Spring Boot - 如何通过实现 BeforeAllCallback 的自定义扩展类设置或覆盖 application.yml 中定义的属性?

】SpringBoot-如何通过实现BeforeAllCallback的自定义扩展类设置或覆盖application.yml中定义的属性?【英文标题】:SpringBoot-Howtosetoroverridepropertiesdefinedinapplication.ymlviaacustomextensionclassthatimplementsBeforeAllCallback?【发布时间】:2021-01-2909:10:49 查看详情

springboot自动化配置之自定义一个starter

大家好,我是小悟SpringBoot官网各类启动器:​​https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-build-systems.html#using-boot-starter​​SpringBoot将所有的功能场景都抽取出来,做成一个个starter,只需要在项目里面引入这些starte... 查看详情

210630:springboot自动配置-自定义start

参考技术ASpringBoot关于自动配置的源码在spring-boot-autoconfigure-x.x.x.x.jar中:当然,自动配置原理的相关描述,官方文档貌似是没有提及。不过我们不难猜出,SpringBoot的启动类上有一个@SpringBootApplication注解,这个注解是SpringBoot项目... 查看详情

springboot怎么自定义一个starter

SpringBoot怎么自定义一个Starterstarter是什么spring-boot-starter是spring-boot的一个非常重要组成部分。spring-boot-starter可以理解为一个可拔插式的组件。它可以让模块开发独立化,相互间依赖更加松散,也可以更加方便地集成。  &n... 查看详情

Spring Boot 1.3.3.,Spring Security 基本自定义配置

】SpringBoot1.3.3.,SpringSecurity基本自定义配置【英文标题】:SpringBoot1.3.3.,SpringSecuritybasiccustomconfig【发布时间】:2016-09-0819:33:48【问题描述】:我知道这是n。关于SpringSecurity的帖子,我确实阅读了很多关于它的内容,直到我决定发... 查看详情

Spring Boot:自定义属性配置和测试

】SpringBoot:自定义属性配置和测试【英文标题】:SpringBoot:custompropertiesconfigurationandtests【发布时间】:2018-11-1400:49:27【问题描述】:我正在使用带有默认application.yml属性文件的SpringBoot2.0。我想将它拆分为单独的属性文件,因为... 查看详情

如何自定义一个springbootsrarter

...我们通过学习知道了自动配置原理,其实创建一个自定义SpringBootStarter也很简单。目录​​如何自定义一个SpringBootSrarter?​​​​首先创建一个项目,命名为demo-spring-boot-starter,引入SpringBoot相关依赖​​​​编写配置文件​​... 查看详情

Spring Boot 为嵌入式服务器配置自定义 jsessionid

】SpringBoot为嵌入式服务器配置自定义jsessionid【英文标题】:Springbootconfigurecustomjsessionidforembeddedserver【发布时间】:2014-11-1303:24:03【问题描述】:我想配置我的servlet上下文,例如设置自定义jsessionId键(请参阅ChangingcookieJSESSIONIDn... 查看详情

Spring Boot Actuator Endpoints 安全性不适用于自定义 Spring Security 配置

】SpringBootActuatorEndpoints安全性不适用于自定义SpringSecurity配置【英文标题】:SpringBootActuatorEndpointssecuritydoesn\'tworkwithcustomSpringSecurityConfiguration【发布时间】:2017-06-2719:24:03【问题描述】:这是我的SpringBoot1.5.1Actuatorapplication.prop 查看详情

springboot自定义starter

1,创建一个空工程2,new一个Modules ----------------maven(启动器):springboottest-spring-boot-starter3,new一个Modules ----------------spring(做自动配置的):springboottest-spring-boot-starter-autoconfigurer4,启动器pom文件 查看详情

在 Spring Boot 中编写自定义查询

】在SpringBoot中编写自定义查询【英文标题】:writingcustomqueriesinSpringboot【发布时间】:2016-03-2202:11:31【问题描述】:我最近开始使用Springboot,但遇到了一些问题。以前,当我只是将Spring数据与hibernate和JPA一起使用时,我可以创... 查看详情

无法从 Spring Boot 应用程序中的自定义 yml 文件加载配置

】无法从SpringBoot应用程序中的自定义yml文件加载配置【英文标题】:Cannotloadconfigfromcustomymlfileinspringbootapplication【发布时间】:2017-11-1608:30:55【问题描述】:我正在从我的SpringBoot服务中的application.yml加载自定义配置。我已经通... 查看详情

添加自定义过滤器 Apache Shiro + Spring Boot

】添加自定义过滤器ApacheShiro+SpringBoot【英文标题】:AddingCustomfilterApacheShiro+SpringBoot【发布时间】:2019-01-0405:28:35【问题描述】:我正在尝试将基于SpringMVCxml的项目配置重构为基于SpringBootjava的配置。同时设置shiro配置如下:@Confi... 查看详情

Spring Boot 从 1.5.3 升级到 1.5.4,自定义验证配置启动失败

】SpringBoot从1.5.3升级到1.5.4,自定义验证配置启动失败【英文标题】:SpringBootupgradeto1.5.4from1.5.3withcustomvalidationconfigstartupfailed【发布时间】:2017-11-2120:25:26【问题描述】:当1.5.3运行良好时。但是升级到1.5.4启动失败...异常:2017-0... 查看详情

自定义 Spring Boot 启动器不应用属性

】自定义SpringBoot启动器不应用属性【英文标题】:CustomSpringbootstarterdonotapplyproperty【发布时间】:2018-12-2414:19:13【问题描述】:创建自定义SpringBoot启动器我的弹簧启动启动器从spring-boot-starter-parent.在我的Starter的源文件夹中添加... 查看详情

在 Spring Boot 中配置 RequestContextListener

】在SpringBoot中配置RequestContextListener【英文标题】:ConfiguringRequestContextListenerinSpringBoot【发布时间】:2015-07-2701:23:31【问题描述】:我有一个使用Spring-Security的Spring-Boot应用程序。我有一个请求范围的bean,我想将它自动装配到安... 查看详情