Spring Boot可以外部化程序配置,以便可以在不同环境中使用相同的应用程序代码。可以使用属性文件,YAML文件,环境变量和命令行参数来指定外部化配置。使用
@Value注释
,将属性值直接注入到bean中,或者通过@ConfigurationProperties
将配置绑定到结构化对象中。
1.定制个性化Banner
-
在spring启动的时候会有一个默认的banner图案,如下:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.2.0.RELEASE)
-
我们可以自定义此banner图案,在src/main/resources目录下新建banner.txt文件,然后将自己的图案黏贴进去即可。生成图案可通过此网址http://www.network-science.de/ascii/,测试如下,我们写入
hi guys!
字样:.__ .__ ._. | |__ |__| ____ __ __ ___.__. ______ | | | | \| | / ___\| | < | |/ ___/ | | | Y \ | / /_/ > | /\___ |\___ \ \| |___| /__| \___ /|____/ / ____/____ > __ \/ /_____/ \/ \/ \/ Hi guys !!
-
当然我们也可以关闭此banner显示,在启动类中:
@SpringBootApplication public class SpringBootConfigurationApplication { public static void main(String[] args) { //SpringApplication.run(SpringBootConfigurationApplication.class, args); SpringApplication app = new SpringApplication(SpringBootConfigurationApplication.class); app.setBannerMode(Banner.Mode.OFF); app.run(args); } }
2.properties和yml后缀的配置文件
-
在SpringBoot中可以用后缀为
properties
或者yml
的配置文件-
在application.yml中
server: port: 8666
-
在application.properties中
server.port=8888
-
-
yml配置文件是json的超集,在配置层次和可读性方面有着优良的表现,properties和yml配置文件是可以相互转换的。
-
springBoot提供了很多可供配置的属性:点击见官方属性
3.@value注解使用
- 在springBoot对象中注入我们自定义属性可以直接使用’@Value’注解
-
在application.yml中定义属性:
user-defined: attribute: hi friends !
-
在javaBean中获取属性
@Component public class TestValueAnnotation { @Value("${user-defined.attribute}") private String box; //忽略getter setter }
-
4.@ConfigurationProperties注解使用
-
使用@ConfigurationProperties注解可将配置文件直接注入JavaBean中,实现更为复杂便捷的注入,使用方式如下:
@ConfigurationProperties("acme.wo")//前缀 public class TestConfigurationPropertiesAnnotation { private boolean enabled; //更多属性...
-
注入boolean,String等基础类型,在java和yml中,分别如下:
@ConfigurationProperties("acme.wo") public class TestConfigurationPropertiesAnnotation { private boolean enabled; private String configStr; private Integer configInt; //忽略getter setter
acme: wo: configStr: hi boys! configInt: 133 enabled: true
-
注入javaBean类型,如下:
@ConfigurationProperties("acme.wo") public class TestConfigurationPropertiesAnnotation { private InetAddress remoteAddress ; private final Security security = new Security(); private MyPojo myPojo ; //忽略getter setter public static class MyPojo { private String firstName; private String lastName; //忽略getter setter } public static class Security { private String username; private String password; private List<String> roles = new ArrayList<>(Collections.singleton("USER")); //忽略getter setter } }
acme: wo: myPojo: firstName: Joseph lastName: Tribbiani remote-address: 192.168.1.1 security: username: admin password: admin roles: - USER - ADMIN
- 如果声明了final,需要显示初始化,如上例中的Security
- 也可以将配置注入到java框架提供类中,如:InetAddress
- 注入list,map,以及嵌套类型,如下:
@ConfigurationProperties("acme.wo") public class TestConfigurationPropertiesAnnotation { private String[] arrayProps; private final List<MyPojo> list = new ArrayList<>(); private List<String> listProp2 = new ArrayList<>(); private List<Map<String, String>> listProp1 = new ArrayList<>(); private Map<String, String> mapProps = new HashMap<>(); public static class MyPojo { private String firstName; private String lastName; } }
arrayProps: 1,2,3,4,5 listProp1: - name: abc value: abcValue - name: efg value: efgValue listProp2: - config2Value1 - config2Vavlue2 mapProps: key1: value1 key2: value2 list: - firstName: Joseph lastName: Tribbiani - firstName: Chandler lastName: Bing
- 注入list使用
-
来标识,map使用key,value来标识
4.总结
- 在SpringBoot中引入注入外部配置十分简便,①使用@Value引入外部配置;②使用@ConfigurationProperties注解引入外部配置;
- 此文章的github范例,点击获取
- 参考: