springboot

springboot

1.如何禁用特定的自动配置

例如,此代码片段中和了DataSourceAutoConfiguration:

// other annotations
@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)
public class MyConfiguration {}

如果我们使用@SpringBootApplication注解启用自动配置(其中@EnableAutoConfiguration作为元注解),我们可以使用同名属性禁用自动配置:

// other annotations
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class MyConfiguration { }

我们还可以使用spring.autoconfigure.exclude环境属性禁用自动配置。application.properties文件中的此设置执行与之前相同的操作:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

2.如何注册自定义自动配置

要注册自动配置类,我们必须在META-INF/spring.factories文件中的EnableAutoConfiguration键下列出其完全限定名称:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.baeldung.autoconfigure.CustomAutoConfiguration

如果我们使用 Maven 构建项目,则该文件应放置在resources/META-INF目录中,该目录将在打包阶段最终位于上述位置。

3.当 Bean 存在时如何告诉自动配置退出

为了指示自动配置类在 bean 已存在时退出,我们可以使用@ConditionalOnMissingBean注释。
该注释最应该注意的属性是:
value – 要检查的 bean 类型
name – 要检查的 beans 的名称
当放置在用@Bean装饰的方法上时,目标类型默认为该方法的返回类型:

@Configuration
public class CustomConfiguration {
    @Bean
    @ConditionalOnMissingBean
    public CustomService service() { ... }
}

4.外部配置的可能来源有哪些

Spring Boot提供了对外部配置的支持,允许我们在各种环境下运行同一个应用程序。我们可以使用属性文件、YAML 文件、环境变量、系统属性和命令行选项参数来指定配置属性。
然后,我们可以使用@Value注释、通过@ConfigurationProperties注释的绑定对象或环境抽象来访问这些属性。

5.spring-boot-devtools

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

通过 HTTP 进行远程调试(远程调试隧道)
spring-boot-devtools通过 HTTP 提供开箱即用的远程调试功能,要拥有此功能,需要将spring-boot-devtools打包为应用程序的一部分。这可以通过在 maven 的插件中禁用exceptDevtools配置来实现。

这是一个快速示例:

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

5.1.在服务器上部署和启动的应用程序应在启用远程调试的情况下启动:

-Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n

可以看到,这里没有提到远程调试端口。因此,java将选择一个随机端口
5.2对于同一个项目,打开启动配置,选择以下选项:
选择主类:org.springframework.boot.devtools.RemoteSpringApplication
在程序参数中,添加应用程序的 URL,例如http://localhost:8080
5.3通过 spring-boot 应用程序调试器的默认端口是 8000,可以通过以下方式覆盖:

spring.devtools.remote.debug.local-port=8010

5.4现在创建一个远程调试配置,将端口设置为通过属性配置的8010或8000(如果坚持默认值)

  .   ____          _                                              __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _          ___               _      \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` |        | _ \___ _ __  ___| |_ ___ \ \ \ \
 \\/  ___)| |_)| | | | | || (_| []::::::[]   / -_) '  \/ _ \  _/ -_) ) ) ) )
  '  |____| .__|_| |_|_| |_\__, |        |_|_\___|_|_|_\___/\__\___|/ / / /
 =========|_|==============|___/===================================/_/_/_/
 :: Spring Boot Remote ::  (v1.5.2.RELEASE)

2017-03-12 22:24:11.089  ...: Starting RemoteSpringApplication v1.5.2.RELEASE on machine with PID 10476 (..\org\springframework\boot\spring-boot-devtools\1.5.2.RELEASE\spring-boot-devtools-1.5.2.RELEASE.jar started by user in project)
2017-03-12 22:24:11.097  ...: No active profile set, falling back to default profiles: default
2017-03-12 22:24:11.357  ...: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@11e21d0e: startup date [Sun Mar 12 22:24:11 IST 2017]; root of context hierarchy
2017-03-12 22:24:11.869  ...: The connection to http://localhost:8080 is insecure. You should use a URL starting with 'https://'.
2017-03-12 22:24:11.949  ...: LiveReload server is running on port 35729
2017-03-12 22:24:11.983  ...: Started RemoteSpringApplication in 1.24 seconds (JVM running for 1.802)
2017-03-12 22:24:34.324  ...: Remote debug connection opened
posted @ 2024-03-05 21:22  dkpp  阅读(8)  评论(0编辑  收藏  举报