spring boot笔记

1、

You should only ever add one @SpringBootApplication or @EnableAutoConfiguration annotation. We generally recommend that you add one or the other to your primary @Configuration class only.


Many Spring Boot developers like their apps to use auto-configuration, component scan and be able to define extra configuration on their "application class". A single@SpringBootApplication annotation can be used to enable those tree features, that is:

The @SpringBootApplication annotation is equivalent to using @Configuration@EnableAutoConfiguration, and @ComponentScan with their default attributes, as shown in the following example:

以上摘自官方文档  @SpringBootApplication的作用相当于 这三个标签的组合 @EnableAutoConfiguration 和 @ComponentScan 和@Configuration

----------------------------------

2、We generally recommend that you locate your main application class in a root package above other classes. 

以上摘自官方文档 ,文档建议 将主应用程序类定位在其他类上方的根包中。主应用程序通常被@SpringBootApplication注解修饰。

---------------------------

3、Although it is possible to use SpringApplication with XML sources,we generally recommend that your primary source be a single @Configuration class. 

文档建议使用 @Configuration修饰java类,作为配置的 主源

-----------------------------

4、You need not put all your @Configuration into a single class. The @Import annotation can be used to import additional configuration classes. Alternatively, you can use @ComponentScan to automatically pick up all Spring components, including @Configuration classes.

文档不建议把所有配置都放在@Configuration所修饰的一个类下。建议使用@Import注解导入额外的配置。

或者使用@ComponentScan去自动组装所有的@Configuration

-----------------------------

5、

Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added. For example, if HSQLDBis on your classpath, and you have not manually configured any database connection beans, then Spring Boot auto-configures an in-memory database.

You need to opt-in to auto-configuration by adding the @EnableAutoConfiguration or @SpringBootApplication annotations to one of your @Configurationclasses.



You should only ever add one @SpringBootApplication or @EnableAutoConfiguration annotation. We generally recommend that you add one or the other to your primary @Configuration class only.


若使用自动配置,则需要在 配置类(被@Configuration修饰)上加上注解 @EnableAutoConfiguration或者是注解 @SpringBootApplication 。文档建议把注解加在 主配置类上。

-----------------------------------

6、

You should only ever add one @SpringBootApplication or @EnableAutoConfiguration annotation. We generally recommend that you add one or the other to your primary @Configuration class only.


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

引入spring-boot-devtools可以进行热部署,修改java代码后,mvn package,不用重启服务,即可看到代码的改变。若不生效,可参考如下链接 https://www.cnblogs.com/sprinkle/p/7058630.html

--------------------

7、

You should only ever add one @SpringBootApplication or @EnableAutoConfiguration annotation. We generally recommend that you add one or the other to your primary @Configuration class only.



--------------------------------------------------------------------------------------

23.1 Startup Failure

spring boot可以支持自己定义启动失败后,捕获和显示的信息。

23.2 Customizing the Banner

可以自定义启动服务的banner,可以使用图片,会自动加载为字符画。

23.3 Customizing SpringApplication

可以在main方法中设置属性,定制化app

public static void main(String[] args) {
	SpringApplication app = new SpringApplication(MySpringConfiguration.class);
	app.setBannerMode(Banner.Mode.OFF);
	app.run(args);
}

23.4 Fluent Builder API

支持流形式的SpringApplicationBuilder代替ApplicationContext,适合于有父子结构的应用

new SpringApplicationBuilder()
		.sources(Parent.class)
		.child(Application.class)
		.bannerMode(Banner.Mode.OFF)
		.run(args);

23.5 Application Events and Listeners

支持事件监听。某些事件监听在ApplicationContext create的时候被触发,所以不能使用@Bean注册,需要使用SpringApplication.addListeners(…​) method or the SpringApplicationBuilder.listeners(…​) method.或者add a META-INF/spring.factories file

23.7 Accessing Application Arguments

可以通过 注入一个org.springframework.boot.ApplicationArguments bean给main方法的args赋值。

import org.springframework.boot.*
import org.springframework.beans.factory.annotation.*
import org.springframework.stereotype.*

@Component
public class MyBean {

	@Autowired
	public MyBean(ApplicationArguments args) {
		boolean debug = args.containsOption("debug");
		List<String> files = args.getNonOptionArgs();
		// if run with "--debug logfile.txt" debug=true, files=["logfile.txt"]
	}

}

23.8 Using the ApplicationRunner or CommandLineRunner

如果你需要在SpringApplication启动后运行一些特定的代码,你可以实现ApplicationRunner或者CommandLineRunner接口,两个接口都以相同的方式工作,并提供一个单独的运行方法,这个方法在SpringApplication.run(...)完成之前被调用。

import org.springframework.boot.*
import org.springframework.stereotype.*

@Component
public class MyBean implements CommandLineRunner {

	public void run(String... args) {
		// Do something...
	}

}

23.9 Application Exit

每个SpringApplication都向JVM注册一个关闭钩子,以确保ApplicationContext在退出时正常关闭。 可以使用所有标准的Spring生命周期回调(如DisposableBean接口或@PreDestroy注释)。


另外,如果在调用SpringApplication.exit()时希望返回特定的退出代码,bean可以实现org.springframework.boot.ExitCodeGenerator接口(这是一个函数式接口)。 然后可以将此退出代码传递给System.exit()以将其作为状态代码返回,如以下示例中所示:

@SpringBootApplication
public class ExitCodeApplication {

	@Bean
	public ExitCodeGenerator exitCodeGenerator() {
		return () -> 42;
	}

	public static void main(String[] args) {
		System.exit(SpringApplication
				.exit(SpringApplication.run(ExitCodeApplication.class, args)));
	}

}

23.10 Admin Features

通过指定spring.application.admin.enabled属性,可以为应用程序启用与管理相关的功能。 这暴露了平台MBeanServer上的SpringApplicationAdminMXBean。 您可以使用此功能远程管理您的Spring Boot应用程序。 此功能对于任何服务包装器实现也可能很有用。

24.Externalized Configuration(外部化配置)

Spring Boot允许您将配置外部化,以便您可以在不同环境中使用相同的应用程序代码。 您可以使用属性文件,YAML文件,环境变量和命令行参数来外部化配置。 属性值可以通过使用@Value注解直接注入到bean中,通过Spring的Environment抽象来访问,或者通过@ConfigurationProperties绑定到结构化对象。

24.1 Configuring Random Values

RandomValuePropertySource用于注入随机值(例如,注入秘密或测试用例)。 它可以产生整数,长整数,uuids或字符串,如下例所示:

my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.uuid=${random.uuid}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}

24.2 Accessing Command Line Properties

默认情况下,SpringApplication将任何命令行选项参数(即以 - 开头的参数,例如--server.port = 9000)转换为属性并将它们添加到Spring环境中。 如前所述,命令行属性始终优先于其他属性源。

如果您不想将命令行属性添加到环境中,可以使用SpringApplication.setAddCommandLineProperties(false)将其禁用。

24.3 Application Property Files

SpringApplication从以下位置的application.properties文件加载属性并将它们添加到Spring环境中:
1、当前目录的A / config子目录
2、当前目录
3、一个classpath / config包

4、The classpath root

该列表按优先顺序排列(在列表中较高的位置定义的属性会覆盖在较低位置定义的属性)。

注:以上是读取配置文件的顺序,我们习惯性把配置文件写在 classpath 下。但读取顺序 classpath root的目录是优先级最低的,所以一些个性化设置可以在 其他地方配置,用以覆盖默认配置。

24.4 Profile-specific Properties(配置文件特定的属性)

除了application.properties文件外,还可以使用以下命名约定来定义特定于配置文件的属性:

application-{profile}.properties. The Environment has a set of default profiles (by default, [default]) that are used if no active profiles are set. In other words, if no profiles are explicitly activated, then properties from application-default.properties are loaded.

application - {profile}.properties。 如果未设置活动配置文件,则环境具有一组默认配置文件(默认情况下为[默认值])。 换句话说,如果没有显式激活配置文件,则会加载来自application-default.properties的属性。

24.5 Placeholders in Properties

The values in application.properties are filtered through the existing Environment when they are used, so you can refer back to previously defined values (for example, from System properties).

app.name=MyApp
app.description=${app.name} is a Spring Boot application

使用它们时,application.properties中的值将通过现有环境进行过滤,因此您可以引用回以前定义的值(例如,从System properties)。

25. Profiles

Spring Profiles provide a way to segregate parts of your application configuration and make it be available only in certain environments. Any @Component or @Configuration can be marked with @Profile to limit when it is loaded, as shown in the following example:

Spring Profiles提供了一种分离部分应用程序配置的方法,并使其仅在特定环境中可用。 可以使用@Profile标记任何@Component或@Configuration,以限制何时加载它,如以下示例所示:

@Configuration
@Profile("production")
public class ProductionConfiguration {

	// ...

}

You can use a spring.profiles.active Environment property to specify which profiles are active. You can specify the property in any of the ways described earlier in this chapter. For example, you could include it in your application.properties, as shown in the following example:

您可以使用spring.profiles.active环境属性来指定哪些配置文件处于活动状态。 您可以用本章前面所述的任何方式指定属性。 例如,您可以将其包含在application.properties中,如以下示例所示:

spring.profiles.active=dev,hsqldb

27. Developing Web Applications

Spring Boot is well suited for web application development. You can create a self-contained HTTP server by using embedded Tomcat, Jetty, Undertow, or Netty. Most web applications use the spring-boot-starter-web module to get up and running quickly. You can also choose to build reactive web applications by using the spring-boot-starter-webflux module.

Spring Boot非常适合Web应用程序开发。 您可以使用嵌入式Tomcat,Jetty,Undertow或Netty创建自包含的HTTP服务器。 大多数Web应用程序都使用spring-boot-starter-web模块来快速启动和运行。 您还可以选择使用spring-boot-starter-webflux模块构建反应性Web应用程序。

27.1 The “Spring Web MVC Framework”

The Spring Web MVC framework (often referred to as simply “Spring MVC”) is a rich “model view controller” web framework. Spring MVC lets you create special @Controller or @RestController beans to handle incoming HTTP requests. Methods in your controller are mapped to HTTP by using @RequestMapping annotations.


Spring Web MVC框架(通常简称为“Spring MVC”)是一个丰富的“模型视图控制器”Web框架。 Spring MVC允许您创建特殊的@Controller或@RestController bean来处理传入的HTTP请求。 您的控制器中的方法通过使用@RequestMapping注释映射到HTTP。


猜你喜欢

转载自blog.csdn.net/u012817635/article/details/79887742
今日推荐