1.SpringBoot使用(一)配置文件

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的配置文件

    1. 在application.yml中

      server:
      	 port: 8666
      
    2. 在application.properties中

      server.port=8888
      
  • yml配置文件是json的超集,在配置层次和可读性方面有着优良的表现,properties和yml配置文件是可以相互转换的。

  • springBoot提供了很多可供配置的属性:点击见官方属性

3.@value注解使用

  • 在springBoot对象中注入我们自定义属性可以直接使用’@Value’注解
    1. 在application.yml中定义属性:

      user-defined:
      	attribute: hi friends !
      
    2. 在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.总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值