Spring Boot 基本配置 学习

北战南征 提交于 2019-12-29 23:22:43

Spring Boot 一些基本配置

 

# 定制化Banner

Spring Boot 项目在启动的时候会有一个默认的启动图案:

1
2
3
4
5
6
7

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.9.RELEASE)

我们可以 把这个图案修改为我们自己想要的。 在 src/main/resources 目录下新建banner.txt文件,然后将自己的图案粘贴进去即可,ASCII图案可通过网址: http://www.network-science.de/ascii/ 一键生成,比如输入mrbird 生成团后复制到banner.txt, 启动项目。

banner 也可以关闭,在main方法中:

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

# 全局配置文件

在 src/main/resources 目录下,Spring Boot提供了一个名为application.properties 的全局配置文件,可对一些默认配置的配置值进行修改

 

$ 自定义属性值
Spring Boot允许我们在 application.properties 下自定义一些属性.比如:

1
2

mrbird.blog.name=mrbird's blog
mrbird.blog.title=Spring Boot

 

定义一个BlogProperties Bean, 通过@Value(“${属性名}”)来加载配置文件中的属性值

@Component
public class BlogProperties {
        
    @Value("${mrbird.blog.name}")
   
private String name;
   
    @Value("${mrbird.blog.title}")
   
private String title;
   
    // get,set

}

 编写IndexController,注入该Bean

@RestController
public class IndexController {
    @Autowired
   
private BlogProperties blogProperties;
   
    @RequestMapping("/")
   
String index() {
       
return blogProperties.getName()+"——"+blogProperties.getTitle();
    }
}

在属性非常多的情况下,也可以定义一个和配置文件对应的Bean:

@ConfigurationProperties(prefix="mrbird.blog")
public class ConfigBean {
   
private String name;
   
private String title;
    // get,set

}

通过注解@configurationProperties(prefix=”mrbird.blog”) 指明了属性的通用前缀,通用前缀加属性名和配置文件名一一对应。

除此之外还需在Spring Boot入口类加上注解 @EnableConfigurationProperties({ConfigBean.class})来启用该配置

1
2
3
4
5
6
7
8

@SpringBootApplication
@EnableConfigurationProperties({ConfigBean.class})

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

之后便可在IndexController中注入该Bean,并使用了。

1
2
3
4
5
6
7
8
9
10

@RestController
public class IndexController {
    @Autowired
   
private ConfigBean configBean;
   
    @RequestMapping("/")
   
String index() {
       
return configBean.getName()+"——"+configBean.getTitle();
    }
}

$ 属性间的引用

在application.properties 配置文件中,各个属性可以相互引用,如下:

mrbird.blog.name=mrbird's blog
mrbird.blog.title=Spring Boot
mrbird.blog.wholeTitle=${mrbird.blog.name}--${mrbird.blog.title}

# 自定义配置文件

除了可以在application.properties 里配置属性,我们还可以自定义一个配置文件,在src/main/resources目录下新建一个test.properties:

test.name=KangKang
test.age=25

定义一个对应该配置文件的Bean:

@Configuration

@ConfigurationProperties(prefix=”test”)

@PropertySource(“classpath: test.properties”)

@Component

public class TestConfigBean{

private String name;

private int age;

}

# 通过命令行设置属性值

在运行Spring Boot jar文件时,可以使用java jar xxx.jsr –server.port=8081 来改变端口的值,这条命令等价于我们手动到application.properties中修改。

如果不想项目的配置被命令行修改,可以在入口文件的main方法中进行如下设置:

1
2
3
4
5

public static void main(String[] args) {
    SpringApplication app =
new SpringApplication(Application.class);
    app.setAddCommandLineProperties(
false);
    app.run(args);
}

# 使用XML配置

虽然Spring Boot并不推荐我们继续使用xml配置,但如果出现不得不使用xml配置的情况,Spring Boot允许我们在入口类里通过注解 @ImportResource({“classpath: some-application.xml”})来引入xml配置文件

# Profile配置

Profile用来针对不同的环境使用不同的配置文件,多环境配置文件必须以application-{profile}.properties的格式命名,其中{profile}为环境标识符,比如定义两个配置文件

  • application-dev.properties:开发环境

1

server.port=8080

  • application-prod.properties:生产环境

1

server.port=8081

至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应{profile}值。

如:spring.profiles.active=dev就会加载application-dev.properties配置文件内容。可以在运行jar文件的时候使用命令java -jar xxx.jar --spring.profiles.active={profile}切换不同的环境配置。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!