标签: spring-autoconfiguration

Spring中的server.error.include-binding-errors=on-param有什么作用?

application.propertiesSpring 应用程序中的以下设置有何作用?

server.error.include-binding-errors=on-param
Run Code Online (Sandbox Code Playgroud)

我在文档中找不到它。

alwaysnever非常不言自明,但我不明白on-param

spring spring-boot spring-autoconfiguration

16
推荐指数
1
解决办法
1万
查看次数

Java 记录不适用于 ConfigurationProperties 注释

我正在使用 @ConfigurationProperties 注释来自动配置我的属性。在我的配置类工作正常之前,我试图用记录实现同样的事情,但失败了。

我正在遵循这个答案,但就我而言,它不起作用:https ://stackoverflow.com/a/68358180/13189473

在我的中application.properties有一个属性是cache.validity=200.

这是代码

@Component
@ConfigurationProperties("cache")
public record MyConfig(int validity) {

    @ConstructorBinding
    public MyConfig(int validity) {
        this.validity= Optional.ofNullable(validity).orElse(0);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试启动我的应用程序时,出现以下错误:

Description:

Parameter 0 of constructor in ...MyConfig required a bean of type 'int' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'int' in your configuration.
Run Code Online (Sandbox Code Playgroud)

使这项工作有效的正确方法是什么?提前致谢。

编辑:我的 pom.xml:

    <properties>
        <java.version>17</java.version>
        <spring.boot.version>2.5.4</spring.boot.version>
        <spring.version>5.3.9</spring.version>
    </properties>
Run Code Online (Sandbox Code Playgroud)

我的应用程序属性:

server.port=8060
cache.validity=0

... (DB …
Run Code Online (Sandbox Code Playgroud)

java spring record spring-autoconfiguration

7
推荐指数
1
解决办法
4357
查看次数

为什么我不应该对我的自动配置进行组件扫描?

Spring Boot 的文档中明确指出,自动配置必须通过spring.factories文件指定自动配置:

\n
\n

自动配置必须仅以这种方式加载。确保它们是在特定的包空间中定义的,并且它们永远不是组件扫描的目标。

\n
\n

我确实尝试@Component在我的自动配置类上添加一个,并确保它会被组件扫描所拾取。似乎有效。

\n

虽然我确实认为这是不好的做法,因为组件扫描不太可能在现实世界中真正拾取它,但我想知道为什么文档对此有如此强烈的感觉。是否还有其他我未能预料到的危险?如果有,是哪些?

\n

编辑:\n在https://youtu.be/jDchAEHIht0?t=734 St\xc3\xa9phane 和 Brian 解释说,有两个阶段,一个称为“用户配置阶段”,另一个称为“自动配置阶段”。按照这种想法,建议使用@ComponentScan自动配置类会将其移至“用户配置阶段”,这基本上会破坏自动配置的语义。

\n

然而,我在实验中未能打破它。只要我保留@Conditional注释,它似乎就能按预期工作......

\n

spring-boot component-scan spring-autoconfiguration

6
推荐指数
1
解决办法
2439
查看次数

Spring Boot 自定义 Bean 加载器

我正在将 JDBI 与 Spring Boot 结合使用。我遵循了这个指南,这导致必须创建一个类:JdbiConfig其中,对于应用程序上下文中想要的每个 dao,您必须添加:

@Bean
public SomeDao someDao(Jdbi jdbi) {
    return jdbi.onDemand(SomeDao.class);
}
Run Code Online (Sandbox Code Playgroud)

我想知道 Spring Boot 中是否有某种方法可以创建自定义处理器来创建 bean 并将它们放入应用程序上下文中。关于这如何工作,我有两个想法:

  1. 使用自定义注释对 DAO 进行注释,@JdbiDao并编写一些内容来获取它们。我曾尝试将这些手动注入到应用程序启动中,但问题是它们可能无法及时加载以进行注入,因为在类扫描期间无法识别它们。
  2. 创建一个JdbiDao每个存储库接口都可以扩展的类。然后用标准注释接口@Repository并创建自定义处理器以通过以下方式加载它们Jdbi#onDemand

这是我的两个想法,但我不知道有什么方法可以实现。我坚持手动创建一个 bean?以前解决过这个问题吗?

java jdbi spring-boot jdbi3 spring-autoconfiguration

5
推荐指数
1
解决办法
255
查看次数

Spring 自定义 @Enable 注释元注释与 @ComponentScan

我正在尝试@Enable为 Spring 框架编写自己的注释,应按如下方式使用:

package com.example.package.app;

@SpringBootApplication
@com.example.annotations.EnableCustom("com.example.package.custom")
public class MyApplication {}
Run Code Online (Sandbox Code Playgroud)

使用自定义注释遵循Component scan,但这带来了几个限制:

  1. 我不能使基本包属性动态化,即我不能传递"com.example.package.base",但需要在配置中预定义包。

    我看了看@AliasFor,但无法让它工作。

  2. 当我省略基础包时,扫描从注释的定义包开始,而不是从被注释的类的包开始。在上面的例子中,它只会为 中的类扫描和创建 bean com.example.annotations,而不是为com.example.package.*.

    我看了一下EntityScanPackages.Registrar.class@EntityScan注释中导入的是哪个,但它是一个内部类,我的注释无法导入。

一切正常,如果我把@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, value = MyAnnotation.class))MyApplication类,但停止时这个被移动到的元注释工作@EnableCustom。如何告诉 Spring Framework 将其视为@EnableCustom指定@ComponentScan某些默认值的不同方式。我试着元注解注释我用@Configuration@Component和其他人,但没有成功:

@Configuration
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ComponentScan(
        includeFilters = @ComponentScan.Filter(
                type = FilterType.ANNOTATION,
                value = ApplicationService.class))
public @interface EnableApplicationServices {
    @AliasFor(annotation …
Run Code Online (Sandbox Code Playgroud)

java spring spring-annotations component-scan spring-autoconfiguration

5
推荐指数
1
解决办法
1360
查看次数

使用@Configuration是否会阻止@SpringBootConfiguration的自动检测?

我已经搜索了使用@TestConfiguration与@Configuration相比的真正含义,并且我在@TestConfiguration的官方评论中看到了一个声明。

...与常规@Configuration类不同,@TestConfiguration的使用不会阻止@SpringBootConfiguration的自动检测。

该声明的含义是,使用 @Configuration 可以防止自动检测 @SpringBootConfiguration,但 @TestConfiguration 不会。

我对“预防”这个词感到困惑。@Configuration的使用会影响@SpringBootConfiguration吗?

spring spring-annotations spring-boot spring-boot-test spring-autoconfiguration

5
推荐指数
0
解决办法
104
查看次数

需要一个类型为“io.micrometer.core.instrument.MeterRegistry”的 bean,但无法找到

最近我们将 spring-boot 版本升级到 2.7.2,将 spring Framework 版本升级到 5.3.22。从那时起,我们在其中一个应用程序中看到此错误

“需要一个‘io.micrometer.core.instrument.MeterRegistry’类型的 bean,但无法找到。”

Spring 文档说,如果有一个名为 micrometer-registry-<?> 的依赖项,那么它将自动装配所需的 MeterRegistry。我们确实对我们的项目有这种依赖性。但这并没有发生。

我们在 application.yml 中有以下属性。但执行器也不工作

management:
  metrics:
    export:
      statsd:
        enabled: true
        flavor: datadog
        host: localhost
        port: 8125
  endpoints:
    web:
      exposure:
        include: "*"
  metrics:
    enabled: true
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我我在这里缺少什么吗?

java spring-boot micrometer spring-autoconfiguration

4
推荐指数
1
解决办法
1万
查看次数

如何调试 Spring Boot 自动配置

使用 Spring Boot 时的一个常见问题模式是缺少一个 bean,尽管该 bean 期望它由 Spring Boot 创建。

如何调试这样的场景?如何找出该 Bean 没有按预期创建的原因!

注意:我创建了这个问题,它是针对特殊情况提出的问题的答案。我希望这对各种场景都有帮助。

java spring spring-boot spring-autoconfiguration

3
推荐指数
1
解决办法
109
查看次数

运行 Junit 测试时 SpringBoot 禁用 DataSourceAutoconfigure 错误

我的 Springboot 应用程序工作正常,连接到数据源。对于 Junit,我通过排除 DatasourceAutoConfiguration、DataSourceTransactionManagerConfiguration、HibernateJpaAutoConfiguration 类来禁用数据源的自动配置,以避免 SpringBoot 自动配置数据源。

pom.xml

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
<dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
  <version>2.2.5.RELEASE</version>
<dependency>
<dependency>
  <groupId>com.microsoft.sqlserver</groupId>
  <artifactId>mssql-jdbc</artifactId>
  <version>8.2.2.jre8</version>
<dependency>
Run Code Online (Sandbox Code Playgroud)

主班

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan({"com.basepackage"})
public class SampleClass{
  psvm(){
  SpringApplication.run(SampleClass.class,args);
}
}
Run Code Online (Sandbox Code Playgroud)

Juni测试班

@RunWith(SpringRunner.class)
@SpringBootTest(classes=SampleClass.class)
@AutoConfigureMockMvc
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class, DataSourceTransactionManagerConfiguration.class, HibernateJpaAutoConfiguration.class})
public class SampleControllerTest {
  @MockBean
  private SampleService service;

  @Test
  public void fetchUsers() {
    Mockito.when(service.retrieveUsers().thenReturn(new SampleResponse());
}
Run Code Online (Sandbox Code Playgroud)

}

在 mvn 测试中,测试脚本已运行但失败,因为 Springboot 未自动配置 SampleRepository bean(可能是因为排除了 Datasource 和 HibernateJpa 自动配置类)

错误

Caused by UnsatisfiedDependencyException: Error creating bean with name ServiceDAOImpl, …
Run Code Online (Sandbox Code Playgroud)

junit spring-boot spring-boot-test spring-autoconfiguration

2
推荐指数
1
解决办法
2968
查看次数

春季启动自动配置。意外的行为

我正在尝试使用 Spring Boot 自动配置功能并遇到问题。我创建了一个 github 存储库,以便能够轻松地重现“问题”:

git clone https://github.com/clembo590/issues.git --branch spring_boot_auto_configuration
Run Code Online (Sandbox Code Playgroud)

只需运行mvn clean install,您将获得我在描述中提到的所有日志。

我已经“启用”了debug=truespring boot 属性,以查看哪个“自动配置”已激活或未激活(如果它未激活,为什么它未激活)。

我还添加了一些日志来“记录已添加到 Spring Boot 上下文中的所有 bean”。

这是我的自动配置类。


@Configuration
@ComponentScan(basePackageClasses = MyClass.class)
@ConditionalOnBean(ObjectMapper.class)
@AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE)
public class CleaningAutoConfiguration {

    @Bean
    public Fake fake(){
        return new Fake();
    }

    private static class Fake{

    }

}
Run Code Online (Sandbox Code Playgroud)

第一个奇怪的事情是这个日志:

   CleaningAutoConfiguration:
      Did not match:
         - @ConditionalOnBean (types: com.fasterxml.jackson.databind.ObjectMapper; SearchStrategy: all) did not find any beans of type com.fasterxml.jackson.databind.ObjectMapper (OnBeanCondition)
      Matched:
         - @ConditionalOnBean (types: com.fasterxml.jackson.databind.ObjectMapper; SearchStrategy: all) found bean …
Run Code Online (Sandbox Code Playgroud)

spring spring-boot spring-autoconfiguration

2
推荐指数
1
解决办法
1087
查看次数

为什么@AutoConfiguration使用proxyBeanMethods = false,@AutoConfigureBefore,@AutoConfigureAfter

Spring Boot 自动配置最近在 2.7 版本中进行了更改,并且大多数设置在 3.0 版本中已弃用(您可以在此处找到详细信息)。此外,他们还为自动配置类引入了新的注释,即@AutoConfiguration. 我无法理解下面所述的注释的默认设置:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore
@AutoConfigureAfter
public @interface AutoConfiguration {
}
Run Code Online (Sandbox Code Playgroud)

为什么他们强制用户继承proxyBeanMethods = false,@AutoConfigureBefore@AutoConfigureAfter

spring spring-boot spring-boot-configuration spring-autoconfiguration

2
推荐指数
1
解决办法
807
查看次数

spring.jackson.default-property-inclusion 被忽略

在我的 Spring Boot/Kotlin 项目中,我试图让 JSON 转换器忽略其余控制器响应中的空值。

我尝试在 application.yml 中设置以下内容:

spring:
    jackson:
        default-property-inclusion: non_null
Run Code Online (Sandbox Code Playgroud)

我还尝试提供 of@Bean类型Jackson2ObjectMapperBuilder@ObjectMapper配置为.serializationInclusion(JsonInclude.Include.NON_NULL),但它仍然序列化所有空属性。

使用 Spring Boot 2.3.0、Kotlin 1.3.72、AdoptOpenJDK 13

jackson kotlin spring-boot spring-autoconfiguration

1
推荐指数
1
解决办法
8985
查看次数