springboot学习总结外部配置(命令行参数配置常规属性配置类型安全的配置之基于properties)(代码片段)

vincentren vincentren     2023-02-20     211

关键词:

学习的内容主要是汪云飞的《Spring Boot实战》

(一)命令行参数配置

springboot项目可以基于jar包运行,打开jar的程序可以通过下面命令行运行:

java -jar xxx.jar

可以通过以下命令修改tomcat端口号

java -jar xxx.jar --server.port=9090

(二)常规属性配置

在springboot项目中,我们只需在application.properties定义属性,直接使用@Value注入即可

(1)application.properties中添加属性

book.author=wangyunfei
book.name=spring boot

(2)修改入口类

package com.vincent.demo;

import com.vincent.demo.config.Author;
import com.vincent.demo.config.HelloService;
import com.vincent.demo.config.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class DemoApplication 

    @Value("$book.author")
    private String bookAuthor;

    @Value("$book.name")
    private String bookName;

    @RequestMapping("/")
    String index()
        return "book name is :" + bookName + " and book author is: " + bookAuthor;
    

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

(三)类型安全的配置(基于properties)

通过@ConfigurationProperties将properties属性和一个Bean及其相关属性关联、从而实现类型安全的配置

(1)添加配置,在application.properties上添加:

author.name=wyf
author.age=32

(2)类型安全的Bean,代码如下:

package com.vincent.demo.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author rw
 * @date 2018/12/17 下午9:50
 */
@Component
//加载properties文件内的配置,通过prefix属性指定properties的配置的前缀,通过locations指定properties文件的位置
@ConfigurationProperties(prefix = "author")
public class Author 

    private String name;

    private Integer age;

    public String getName() 
        return name;
    

    public void setName(String name) 
        this.name = name;
    

    public Integer getAge() 
        return age;
    

    public void setAge(Integer age) 
        this.age = age;
    

(3)可以用@Autowired直接注入该配置

package com.vincent.demo;

import com.vincent.demo.config.Author;
import com.vincent.demo.config.HelloService;
import com.vincent.demo.config.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class DemoApplication 

    @Value("$book.author")
    private String bookAuthor;

    @Value("$book.name")
    private String bookName;

    @Autowired
    Author author;

    @RequestMapping("/")
    String index()
        return "book name is :" + bookName + " and book author is: " + bookAuthor + " author.name is:" + author.getName();
    public static void main(String[] args) 
        SpringApplication.run(DemoApplication.class, args);
    

 


springboot--外部配置的属性使用

SpringBoot允许使用propertities文件、yaml文件或者命令行参数作为外部配置。命令行参数配置SpringBoot可以基于jar包运行,打成jar包的程序可以直接通过下面的命令运行:java-jarxx.jar可以通以下命令修改Tomcat端口号:java-jarxx.jar--server.po... 查看详情

springboot配置文件放在jar外部

SpringBoot程序默认从application.properties或者application.yaml读取配置,如何将配置信息外置,方便配置呢?查询官网,可以得到下面的几种方案:通过命令行指定SpringApplication会默认将命令行选项参数转换为配置信息例如,启动时命令参... 查看详情

springboot3-外部配置

Springboot允许使用properties、yml、命令行参数作为外部配置。1、命令行配置。java-jarxxx.jar--server.prot=90902、常规属性配置。在application.yml里添加melo:name:tony@Value("$melo.name")privateStringname; 查看详情

3springboot:springboot配置文件(外部配置加载顺序自动配置原理,@conditional)

1.外部配置加载顺序SpringBoot也可以从以下位置加载配置;优先级从高到低高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置 1.命令行参数所有的配置都可以在命令行上进行指定先打包在进行测试java-jarspring-boot... 查看详情

外部化配置--springboot功能

一、外部化配置SpringBoot将你的配置外部化,因此你可以在不同的环境下运行相同的代码。你可以使用properties文件,YAML文件,环境变量,命令行参数在外部配置。使用@Value注解可以直接将属性值注入到bean中,通过Spring的Environment... 查看详情

springboot外部化配置简介

参考技术ASpringBoot提供properties文件、YAML文件、环境变量(environmentvariables)和命令行参数的方式进行外部化配置,可以在不同的环境,使用不同的配置信息。应用可以通过@Value注入到bean,也可以通过@ConfigurationProperties进行对象的绑... 查看详情

springboot属性配置和使用(转)

SpringBoot属性配置和使用SpringBoot允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置。SpringBoot入门 请看:http://blog.csdn.net/isea533/article/details/50278205SpringB... 查看详情

springboot梳理-springboot属性配置和使用(转)

转:https://blog.csdn.net/isea533/article/details/50281151SpringBoot支持多种外部配置方式,这些方式优先级如下:命令行参数来自java:comp/env的JNDI属性Java系统属性(System.getProperties())操作系统环境变量RandomValuePropertySource配置的random.*属性值j... 查看详情

springboot学习总结(26)——springboot容器启动详解(代码片段)

一、容器启动springboot一般是指定容器启动main方法,然后以命令行方式启动Jar包,如:@SpringBootApplicationpublicclassApplicationpublicstaticvoidmain(String[]args)SpringApplication.run(Application.class,args);这里核心关注2个东西:@SpringBootA 查看详情

springboot配置文件放在jar外部

SpringBoot配置文件放在jar外部2018年03月16日10:09:17 qq_37334435 阅读数:1488  SpringBoot程序默认从application.properties或者application.yaml读取配置,如何将配置信息外置,方便配置呢?查询官网,可以得到下面的几种方案:通过... 查看详情

springboot

http://docs.spring.io/spring-boot/docs/1.2.8.RELEASE/reference/htmlsingle/#boot-features-logging-file-outputSpringBoot支持多种外部配置方式这些方式优先级如下:命令行参数来自java:comp/env的JNDI属性Java系统属性(System.getProperties())操作系 查看详情

springboot学习总结

参考技术ASpringBoot基于Spring,集成SpringBoot,不会对原有项目的SpringMvc,MyBatis等框架产生冲突。SpringBoot会选择最适合的Spring子项目和第三方开源库进行整合。大部分SpringBoot应用只需要非常少的配置就可以快速运行起来。1.很多配... 查看详情

如何从 docker 命令行设置参数来配置 Spring Boot 应用程序?

】如何从docker命令行设置参数来配置SpringBoot应用程序?【英文标题】:HowcanIsetparametersfromdockercommandlinetoconfigurespringbootapplication?【发布时间】:2015-12-1118:58:41【问题描述】:我有一个带有yml的SpringBoot应用程序,可以在Docker容器... 查看详情

使用外部 jar 命令通过命令行向主类发送参数

】使用外部jar命令通过命令行向主类发送参数【英文标题】:sendargumenttomainclassbycommandlinewithexternaljarcommand【发布时间】:2017-11-2015:38:55【问题描述】:我在命令行linux中使用这样的外部jar运行java应用程序:java-cp".:commons-net-3.6.jar... 查看详情

如何将 SpringBoot 配置外部文件覆盖到类路径文件?

】如何将SpringBoot配置外部文件覆盖到类路径文件?【英文标题】:HowtooverrideSpringBootconfigexternalfiletoclasspathfile?【发布时间】:2021-11-0223:09:11【问题描述】:当我通过命令行运行jar时,我想将SpringBoot外部的一些配置覆盖到类路径... 查看详情

spring编程:springboot属性配置和属性优先级

SpringBoot允许我们外部化程序的配置,以便我们可以在不同的环境中使用相同的应用代码。我们可以使用properties、YAML、环境变量、命令行参数进行外部化配置。配置的属性值可以通过@Value注解直接注入到beans中,通过Sp... 查看详情

springboot学习总结springsecurity配置

...MVC的配置类似,只需在一个配置类上注解@EnableWebSecurity(Springboot项目可以不用),并让这个类继承WebSecurityConfigurerAdapter。@Configuration@EnableWebSecuritypublicclassWebSecurityConfigextendsWebSecurityConfigurerAdapter@Overrideprotectedvoidconfigure(Authenticati... 查看详情

springboot属性配置和使用(代码片段)

SpringBoot属性配置和使用SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。简单说就... 查看详情