标签: spring-boot-starter

自定义 Spring Boot 3 Starter 不创建 ConfigurationProperties bean

我正在自定义 Spring Starter 项目中使用以下类和配置创建 Maven 工件:

@ConfigurationProperties(prefix = "commons.time.formats")
@Getter
@Setter
public class TimeFormatConf {
... fields
}
Run Code Online (Sandbox Code Playgroud)
@Component
@Configuration
@AllArgsConstructor
@EnableConfigurationProperties(TimeFormatConf.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
public class DateTimeConfigurer {
  private final TimeFormatConf timeFormatConf;

  @EventListener(ApplicationReadyEvent.class)
  public void configureTimeFormat() {
    TimeZone.setDefault(TimeZone.getTimeZone(timeFormatConf.getDynamicZone()));
    System.setProperty("user.timezone", timeFormatConf.getDynamicZone());
  }
}
Run Code Online (Sandbox Code Playgroud)

还有 spring.factories( src/main/resources/META-INF/spring.factories):

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
[package].DateTimeConfigurer,\
...
Run Code Online (Sandbox Code Playgroud)

当我尝试自动装配 TimeFormatConf 类时,我的 spring 应用程序在启动时崩溃,因为它找不到 TimeFormatConf 类的 bean:

com.some.package.b.Class 中构造函数的参数 0 需要类型为“com.some.package.a.TimeFormatConf”的 bean,但无法找到。

该工件在类路径上具有正确的配置。所以它必须对 Spring 的声明或配置做一些事情。

我还尝试添加@Component属性类和@ComponentScan配置类没有任何效果。

在两个项目上恢复到 Spring Boot 2.7.7 解决了这个问题。所以这似乎是一个错误或至少缺乏文档。我已经提出了一个问题来与 Spring 团队跟进:https://github.com/spring-projects/spring-boot/issues/33720

java spring-boot spring-boot-starter configurationproperties

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

Spring Boot Security 导致 java.lang.NoClassDefFoundError: org/springframework/security/web/access/WebInvocationPrivilegeEvaluator

我喜欢在我的 web 项目中添加 spring 安全性,当我添加安全性并扩展WebSecurityConfigurerAdapter时,当我启动 tomcat 服务器时出现以下错误:

java.lang.NoClassDefFoundError: org/springframework/security/web/access/WebInvocationPrivilegeEvaluator
at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_101]
at java.lang.Class.privateGetDeclaredMethods(Unknown Source) ~[na:1.8.0_101]
at java.lang.Class.getDeclaredMethods(Unknown Source) ~[na:1.8.0_101]
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:613) ~[spring-core-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:524) ~[spring-core-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:510) ~[spring-core-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:570) ~[spring-core-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:697) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:640) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:609) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1484) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:425) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:395) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:96) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:687) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:525) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] …
Run Code Online (Sandbox Code Playgroud)

spring-security spring-boot spring-boot-starter

10
推荐指数
3
解决办法
2万
查看次数

javax.net.ssl.SSLHandshakeException:没有适当的协议。如何强制Java使用TLSv1.2发送邮件?

我正在尝试使用 JavaMailSender 在 Spring Boot 中发送邮件,但出现此错误:

javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate). Failed messages: javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
    javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
Run Code Online (Sandbox Code Playgroud)

经过一些阅读后,我发现这是因为我们使用的是过时的 TLSv1 或 TLSv1.1,我们应该使用 v1.2 或更高版本。我尝试在 application.yml 中添加值为 TLSv1.2 的 ssl.protocols 属性,但它似乎不起作用。这是我的 application.yml:

spring:
  mail:
    host: smtp.gmail.com
    port: 587
    username: ******@gmail.com
    password: *******
    protocol: smtp
    tls: true
    properties.mail.smtp:
      auth: true
      ssl.trust: smtp.gmail.com
      starttls.required: true
      starttls.enabled: true
      ssl.protocols: TLSv1.2 …
Run Code Online (Sandbox Code Playgroud)

java spring-boot spring-boot-starter

8
推荐指数
2
解决办法
3万
查看次数

Spring boot自定义启动器org.springframework.boot.autoconfigure.AutoConfiguration.import未检测到配置类版本2.7.2

我读到我不应该在 github 上打开问题,所以我在这里问。我已经深入研究了代码,例如spring-boot-actuator-autoconfigure没有按照文件内容定义@Configuration\@AutoConfiguration内部类:META-INF/spring.factories

org.springframework.boot.diagnostics.FailureAnalyzer=\
org.springframework.boot.actuate.autoconfigure.metrics.ValidationFailureAnalyzer
Run Code Online (Sandbox Code Playgroud)

我检查过,ValidationFailureAnalyzer甚至没有注释@Configuration\@AutoConfigurationMETA-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports然后我看到声明所有类的文件@AutoConfiguration遵循文件的一些提取:

org.springframework.boot.actuate.autoconfigure.amqp.RabbitHealthContributorAutoConfiguration
org.springframework.boot.actuate.autoconfigure.audit.AuditAutoConfiguration
org.springframework.boot.actuate.autoconfigure.audit.AuditEventsEndpointAutoConfiguration
org.springframework.boot.actuate.autoconfigure.availability.AvailabilityHealthContributorAutoConfiguration
...
Run Code Online (Sandbox Code Playgroud)

所有这些类都用 注释@AutoConfiguration。到目前为止一切顺利如果我们阅读文档,他们会说:

Spring Boot 检查已发布的 jar 中是否存在 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 文件。

事实上,如果我们导入:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>2.7.3</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

一切都很好。我不熟悉 gradle 但我没有看到spring-boot-actuator-starter或中有任何特殊的依赖关系spring-boot-actuator-autoconfigure。在谷歌上搜索我发现这里有一个讨论,他们说:

在 Spring Boot v.2.7 中,自动配置注册从 spring.factories 移至名为 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 的新文件。每行包含自动配置的完全限定名称。为了向后兼容, spring.factories 中的条目仍将受到尊重。

但老实说,我尝试在新文件中移动配置类,但配置类未加载。我在这里写了一个例子。我的org.springframework.boot.autoconfigure.AutoConfiguration.imports文件:

com.example.springbootstarterexample.configuration.Config
Run Code Online (Sandbox Code Playgroud)

如果我转移到旧配置,spring.factries一切都会正常。

我的@AutoConfiguration课:

@AutoConfiguration(after = JpaRepositoriesAutoConfiguration.class)
//@AutoConfigureAfter(JpaRepositoriesAutoConfiguration.class)
@EnableJpaRepositories(basePackages …
Run Code Online (Sandbox Code Playgroud)

java spring-boot spring-boot-starter

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

带有 axon-spring-boot-starter 的 NoHandlerForCommandException

我正在使用 Axon + Spring Boot 创建一个简单的应用程序,只是为了确保我在实际项目中使用它之前了解 Axon 框架中的基本组件。在类 TaskAggregate 中有一个用 @CommandHandler 注释的方法,当我通过 CommandGateway 发送命令时应该调用它,但是在运行应用程序后我得到了异常:

Exception in thread "main" org.axonframework.commandhandling.NoHandlerForCommandException: No handler was subscribed to command [com.xxx.axontest.task.CreateTaskCommand]
Run Code Online (Sandbox Code Playgroud)

根据文档,@CommandHandler 注释应该足以将命令处理程序订阅到命令总线。我想我一定错过了什么。你能看看下面的代码并指出我正确的方向吗?

pom.xml

<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>
    <groupId>com.xxx</groupId>
    <artifactId>axon-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <axon.version>3.0.6</axon.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
    </parent>

    <dependencies>

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

        <dependency>
                <groupId>org.axonframework</groupId>
                <artifactId>axon-spring-boot-starter</artifactId>
                <version>${axon.version}</version>
        </dependency>

    </dependencies>

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

</project>
Run Code Online (Sandbox Code Playgroud)

应用程序.java

package com.xxx.axontest;

import org.axonframework.commandhandling.gateway.CommandGateway;
import org.axonframework.eventsourcing.eventstore.EventStorageEngine;
import org.axonframework.eventsourcing.eventstore.inmemory.InMemoryEventStorageEngine;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import …
Run Code Online (Sandbox Code Playgroud)

spring cqrs axon spring-boot spring-boot-starter

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

如何在 maven pom.xml 文件中拥有另一个父依赖项以及 Spring Boot 父项?

我目前正在开发一个 Spring Boot 多模块项目。

当你建立一个 Spring Boot 项目时,pom.xml你需要有一个对 Spring Boot 的父引用,例如

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>
Run Code Online (Sandbox Code Playgroud)

我想要的是我的项目是一个更广泛项目的子模块 Spring Boot 项目(即我想将它们打包在一个 Repo 中的一个项目文件夹下)。

现在,通常为了设置多模块项目,您将像上面这样的父 pom 信息放入您的子模块中pom.xml

但是由于需要将 Spring Boot 父项作为一项要求,因此无法这样做。

我已经在网上看到建议将我的子模块移出并将其作为一个单独的项目独立存在,但我认为这将是我现在的最后手段,除非这是唯一的方法。

有没有人对如何实现我想要的或我想要实现的是否实际上可行有任何想法?

期待听到选项...

maven-3 maven spring-boot spring-boot-starter

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

禁用Spring Boot Starter Cassandra进行单元测试

我需要在具有以下gradle依赖的项目中使用Cassandra:

compile "org.springframework.boot:spring-boot-starter-data-cassandra"
Run Code Online (Sandbox Code Playgroud)

问题是,尽管将Cassandra AutoConfiguration类标记为从单元测试中排除,但spring仍会以某种方式尝试实例化与此依赖性相关的类。

这是我的单元测试:

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@RunWith( SpringRunner.class )
@SpringBootTest
@AutoConfigureMockMvc
@EnableAutoConfiguration( exclude = {
        org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration.class,
        org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration.class
} )
public class HealthTest {

    @Autowired
    private MockMvc mockMvc;


    @Test
    public void contextLoads() {
        Assert.assertNotNull( mockMvc );
    }

    @Test
    public void healthControllerIsUp() throws Exception {
        mockMvc.perform( MockMvcRequestBuilders.get( "/health" ) )
                .andExpect( MockMvcResultMatchers.status().isOk() );
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误,显然是因为Spring无法实例化群集对象。

java.lang.IllegalStateException: Failed …
Run Code Online (Sandbox Code Playgroud)

cassandra spring-mvc-test spring-boot spring-data-cassandra spring-boot-starter

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

spring boot 项目中排除依赖的问题

如果我想用 maven 包构建我的项目,我会收到 maven 的警告:在构建有效模型时遇到了一些问题

ch.mobi.ama:logcollector:jar:1.0.0-SNAPSHOT [警告] 'dependencyManagement.dependencies.dependency.exclusions.exclusion.artifactId' for org.quartz-scheduler:quartz:jar 与值 '*' 不匹配有效的 ID 模式。@ org.springframework.boot:spring-boot-dependencies:2.1.6.RELEASE, c:\ieu.m2\repository\org\springframework\boot\spring-boot-dependencies\2.1.6.RELEASE\spring-boot- dependencies-2.1.6.RELEASE.pom,第 2619 行,第 25 列

我尝试过旧的 Spring Boot 版本。2.1.5 版也有同样的问题。2.1.4 没有警告。在指定文件 spring-boot-dependencies-2.1.6.RELEASE.pom 的第 2619 行实际上有如下代码

<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>${quartz.version}</version>
    <exclusions>
        <exclusion>
            <artifactId>c3p0</artifactId>
            <groupId>com.mchange</groupId>
        </exclusion>
        <exclusion>
            <artifactId>*</artifactId>
            <groupId>com.zaxxer</groupId>
        </exclusion>
    </exclusions>
</dependency>
Run Code Online (Sandbox Code Playgroud)

由于给定的代码来自Spring Boot,我无法影响它,有一种不好的感觉。我的项目会被正确构建吗?我必须回到 2.1.4 版吗?我是否必须报告 Spring Boot 的问题?我能做什么?

quartz-scheduler dependency-management parent-pom spring-boot-starter

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

“spring-boot-starter-integration”依赖项的目的是什么?

我正在重构一个实现 Spring 集成的遗留 Spring boot 项目。在其中一个模块中,我删除了以下依赖项来修复 CVE-2019-3772 漏洞。这些项目仍然可以编译和运行,没有任何失败。我检查了源代码,没有任何导入使用此依赖项。

那么,这个依赖有什么用呢?

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-integration</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

尽管如此,该模块仍然使用以下集成依赖项。

<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-http</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

java spring spring-integration spring-boot spring-boot-starter

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

Starter autoconfig bean 始终优先于自定义 autoconfig bean

我正在尝试制作一个自定义 Spring Boot 启动器,多个项目将使用它来对 Azure AD 进行身份验证。已设置所有 Azure AD 配置,并且使用所有设置硬编码的单个项目也可以与 Azure AD 配合使用。现在我正在尝试将这些设置移动到自定义 Spring Boot 启动器中,以便多个项目可以使用它。它在大多数情况下都有效,除了一件事:移动自定义AADAppRoleStatelessAuthenticationFilter. 如果我将自定义实现 ( CustomAADAppRoleStatelessAuthFilter) 硬编码在实际实现项目中,则一切正常并且仅CustomAADAppRoleStatelessAuthFilter创建,但是一旦我将其移入自定义启动器,我只会得到AADAppRoleStatelessAuthenticationFilter

请注意,我的CustomAADAppRoleStatelessAuthFilter扩展了启动器的 AADAppRoleStatelessAuthenticationFilter.

AADAppRoleStatelessAuthenticationFilterazure-spring-boot 项目中的自动配置( https://github.com/microsoft/azure-spring-boot/blob/master/azure-spring-boot/src/main/java/com/microsoft/azure/ spring/autoconfigure/aad/AADAuthenticationFilterAutoConfiguration.java ) 是:

@Bean
@ConditionalOnMissingBean(AADAppRoleStatelessAuthenticationFilter.class)
@ConditionalOnProperty(prefix = PROPERTY_PREFIX, value = PROPERTY_SESSION_STATELESS, havingValue = "true")
public AADAppRoleStatelessAuthenticationFilter azureADStatelessAuthFilter(ResourceRetriever resourceRetriever) {
    //bean details omitted
}
Run Code Online (Sandbox Code Playgroud)

我应该替换上述内容的自定义自动配置如下:

@Bean
@ConditionalOnMissingBean(AADAppRoleStatelessAuthenticationFilter.class)
@ConditionalOnProperty(prefix = PROPERTY_PREFIX, value = PROPERTY_SESSION_STATELESS, havingValue = "true")    
public AADAppRoleStatelessAuthenticationFilter customAADAppRoleStatelessAuthFilter(
        ResourceRetriever resourceRetriever) {
    return new …
Run Code Online (Sandbox Code Playgroud)

java azure-active-directory spring-boot spring-boot-starter

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