内容简介:Maven的用户可以通过继承spring-boot-starter-parent项目来获得一些合理的默认配置。这个parent提供了以下特性:启动器包含一些相应的依赖项, 以及自动配置等.Spring Boot 支持基于Java的配置, 尽管可以将 SpringApplication 与 xml 一起使用, 但是还是建议使用 @Configuration.
spring-boot-starter-parent
Maven的用户可以通过继承spring-boot-starter-parent项目来获得一些合理的默认配置。这个parent提供了以下特性:
- 默认使用 Java 8
- 使用UTF-8编码
- 一个引用管理的功能,在dependencies里的部分配置可以不用填写version信息,这些version信息会从spring-boot-dependencies里得到继承。
- 识别过来资源过滤(Sensible resource filtering.)
- 识别插件的配置(Sensible plugin configuration (exec plugin, surefire, Git commit ID, shade).)
- 能够识别application.properties和application.yml类型的文件,同时也能支持profile-specific类型的文件(如: application-foo.properties and application-foo.yml,这个功能可以更好的配置不同生产环境下的配置文件)。
- maven把默认的占位符${…}改为了@..@(这点大家还是看下原文自己理解下吧,我个人用的也比较少
since the default config files accept Spring style placeholders (${…}) the Maven filtering is changed to use @..@ placeholders (you can override that with a Maven property resource.delimiter).)
starter
启动器包含一些相应的依赖项, 以及自动配置等.
Auto-configuration
Spring Boot 支持基于Java的配置, 尽管可以将 SpringApplication 与 xml 一起使用, 但是还是建议使用 @Configuration.
可以通过 @Import 注解导入其他配置类, 也可以通过 @ImportResource 注解加载XML配置文件.
Spring Boot 自动配置尝试根据您添加的jar依赖项自动配置Spring应用程序. 例如, 如果项目中引入 HSQLDB jar, 并且没有手动配置任何数据库连接的bean, 则Spring Boot会自动配置内存数据库.
您需要将 @EnableAutoConfiguration 或 @SpringBootApplication 其中一个注解添加到您的 @Configuration 类中, 从而选择进入自动配置.
禁用自动配置
import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.jdbc.*;
import org.springframework.context.annotation.*;
@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}
如果该类不在classpath中, 你可以使用该注解的excludeName属性, 并指定全限定名来达到相同效果. 最后, 你可以通过 spring.autoconfigure.exclude 属性 exclude 多个自动配置项(一个自动配置项集合)
@ComponentScan
SpringBoot在写启动类的时候如果不使用 @ComponentScan 指明对象扫描范围, 默认指扫描当前启动类所在的包里的对象.
@SpringBootApplication
@Target(value=TYPE)
@Retention(value=RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters={@ComponentScan.Filter(type=CUSTOM,classes=TypeExcludeFilter.class),})
public @interface SpringBootApplication
使用 @SpringBootApplication 注解相当于使用了下面三个注解.
@EnableAutoConfiguration : 启用 Spring Boot 的自动配置.
@ComponentScan : 对应用程序所在的包启用 @Component 扫描.
@Configuration : 允许在上下文中注册额外的bean或导入其他配置类.
ApplicationRunner or CommandLineRunner 区别
应用服务启动时,加载一些数据和执行一些应用的初始化动作。如:删除临时文件,清除缓存信息,读取配置文件信息,数据库连接等。
1、SpringBoot提供了CommandLineRunner接口。当有该接口多个实现类时,提供了@order注解实现自定义执行顺序,也可以实现Ordered接口来自定义顺序。
注意:数字越小,优先级越高,也就是@Order(1)注解的类会在@Order(2)注解的类之前执行。
import com.example.studySpringBoot.service.MyMethorClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(value=1)
public class SpringDataInit implements CommandLineRunner {
@Autowired
private MyMethorClassService myMethorClassService;
@Override
public void run(String... strings) throws Exception {
int result = myMethorClassService.add(8, 56);
System.out.println("----------SpringDataInit1---------"+result);
}
}
2、SpringBoot提供的ApplicationRunner接口也可以满足该业务场景。不同点:ApplicationRunner中run方法的参数为ApplicationArguments,而CommandLineRunner接口中run方法的参数为String数组。想要更详细地获取命令行参数,那就使用ApplicationRunner接口
import com.example.studySpringBoot.service.MyMethorClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
@Component
public class SpringDataInit3 implements ApplicationRunner,Ordered {
@Autowired
private MyMethorClassService myMethorClassService;
@Override
public void run(ApplicationArguments applicationArguments) throws Exception {
int result = myMethorClassService.add(10, 82);
System.out.println("----------SpringDataInit3---------"+result);
}
@Override
public int getOrder() {
return 3;
}
}
外部化配置
Spring Boot允许你外部化你的配置,这样你就可以在不同的环境中使用相同的应用程序代码,你可以使用 properties 文件、YAML文件、环境变量和命令行参数来外部化配置,属性值可以通过使用 @Value 注解直接注入到你的bean中,通过Spring的 Environment 抽象访问,或者通过 @ConfigurationProperties 绑定到结构化对象。
@ConfigurationProperties("spring.datasource.username")
Spring Boot使用一种非常特殊的 PropertySource 命令, 该命令旨在允许对值进行合理的覆盖, 属性按以下顺序考虑:
- Devtools全局设置属性在你的主目录(
~/.spring-boot-devtools.properties当devtools处于激活状态时。 - 测试中的
@TestPropertySource注解 - 测试中的
@SpringBootTest#properties注解属性 - 命令行参数
- 来自
SPRING_APPLICATION_JSON(嵌入在环境变量或系统属性中的内联JSON)的属性 -
ServletConfig初始化参数 -
ServletContext初始化参数 -
java:comp/env中的JNDI属性 - Java系统属性(
System.getProperties()) - 操作系统环境变量
- 一个只有random.*属性的
RandomValuePropertySource - 在你的jar包之外的特殊配置文件的应用程序属性(
application-{profile}.properties和YAML 变体) - 在jar中打包的特殊配置文件的应用程序属性(
application-{profile}.properties和YAML 变体) - 在你的jar包之外的应用程序属性(
application.properties和YAML 变体) - 打包在jar中的应用程序属性(
application.properties和YAML 变体) -
@PropertySource注解在你的@Configuration类上 - 默认属性(通过设置
SpringApplication.setDefaultProperties指定)
访问命令行属性
在默认情况下, SpringApplication 会转换任何命令行选项参数 (也就是说,参数从 -- 开始, 像 --server.port=9000 ) 到一个 property , 并将它们添加到Spring Environment 中, 如前所述, 命令行属性总是优先于其他属性源.
如果不希望将命令行属性添加到 Environment 中, 你可以使用 SpringApplication.setAddCommandLineProperties(false) 禁用它们.
应用程序属性文件
SpringApplication 在以下位置从 application.properties 文件加载属性并将它们添加到Spring Environment :
/config /config
列表按优先顺序排序(在列表中较高的位置定义的属性覆盖在较低位置定义的属性).
特殊配置文件的属性
我们可能在不同环境下使用不同的配置, 这些配置有可能是在同一个文件中或不同文件中.
1.在相同文件中
##################################### Determime which configuration be used
spring:
profiles:
active: "dev"
# Mysql connection configuration(share)
datasource:
platform: "mysql"
driverClassName: "com.mysql.cj.jdbc.Driver"
max-active: 50
max-idle: 6
min-idle: 2
initial-size: 6
---
##################################### for dev environment
spring:
profiles: "dev"
datasource:
# mysql connection user(dev)
username: "root"
# mysql connection password(dev)
password: "r9DjsniiG;>7"
---
##################################### for product environment
spring:
profiles: "product"
datasource:
# mysql connection user(product)
username: "root"
# mysql connection password(product)
password: "root"
---
##################################### for test environment
spring:
profiles: "test"
datasource:
# mysql connection user(test)
username: "root"
# mysql connection password(test)
password: "root"
这样在配置完相同属性的时, 还可以对不同的环境进行不同的配置.
2.多个配置文件.
我们可以把特定环境的配置, 放入多个配置文件中, 但是要按照 application-{profile}.properties 格式. 如下图.
spring.profiles.active 属性进行设置.
我们也可以把配置文件放在 jar 外面, 使用 spring.config.location 属性进行设置.
java -jar beetltest-0.0.1-SNAPSHOT.jar -spring.config.location=classpath:/application.properties
以上所述就是小编给大家介绍的《最渣的 Spring Boot 文章》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 《文章推荐系统》系列之构建离线文章画像
- 《文章推荐系统》系列之构建离线用户和文章特征
- 批量导出某个简书用户的所有文章列表和文章超链接
- “狗屁不通文章生成器”在GitHub热榜火了:文章真够啰嗦的
- 用weexplus从0到1写一个app(2)-页面跳转和文章列表及文章详情的编写
- 8 - 博客文章详情页
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Persuasive Technology
B.J. Fogg / Morgan Kaufmann / 2002-12 / USD 39.95
Can computers change what you think and do? Can they motivate you to stop smoking, persuade you to buy insurance, or convince you to join the Army? "Yes, they can," says Dr. B.J. Fogg, directo......一起来看看 《Persuasive Technology》 这本书的介绍吧!