Spring Boot学习第二篇:Spring Boot特性

本文详细介绍了Spring Boot的核心特性,包括如何构建Maven项目,无父POM的Spring Boot使用,Java版本的变更,以及Spring Boot Maven插件的运用。此外,还探讨了Spring Boot的外化配置,如@Value和@ConfigurationProperties的使用,以及如何通过@Profile进行环境切换,包括Profile特定配置文件和编程方式设置profiles。
摘要由CSDN通过智能技术生成

Spring Boot特性

构建Maven

Maven用户可以继承spring-boot-starter-parent项目来获取默认设置。该父项目提供以下特性:

  • 默认编译级别为Java 1.6 源码编码为UTF-8
  • 一个依赖管理节点,允许你省略普通依赖的标签,继承自spring-boot-dependencies POM。
  • 合适的资源过滤 合适的插件配置(exec插件,surefire,Git commit ID,shade)
  • 针对application.properties和application.yml的资源过滤
  • 最后一点:由于默认配置文件接收Spring风格的占位符(${…}),Maven
  • filtering改用@..@占位符(你可以使Maven属性resource.delimiter来覆盖它)。

标题

使用没有父POM的Spring Boot
<dependencyManagement>
     <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.3.0.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
改变Java版本
<properties>
    <java.version>1.8</java.version>
</properties>
使用Spring Boot Maven插件

Spring Boot包含一个Maven插件,它可以将项目打包成一个可执行jar

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

外化配置


Spring Boot允许使用properties文件、yaml文件或者命令行参数作为外部配置
使用@Value注解,可以直接将属性值注入到你的beans中,并通过Spring的Environment抽象或绑定到结构化对象来访问。

@Value(“${xxx}”)

application.properties

在properties文件中增加几个属性
server.port=8081  
server.context-path=/demo  
#声明一个name属性  
users.name=zhangsan
user.age=22 

DemoApplication.java

@SpringBootApplication
@RestController
public class DemoApplication {

    @Value("${users.name}")
    private String name;

    @Value("${user.age}")
    private String age;

    @RequestMapping("/")
    String home() {
        return "Hello World,name is" + name + ", age is"+ age;  
    }

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

    }
}

运行结果:
这里写图片描述

@ConfigurationProperties

使用@Value(“${property}”)注解注入配置属性有时可能比较笨重,特别是需要使用多个properties或你的数据本身有层次结构。为了控制和校验你的应用配置,Spring Boot提供一个允许强类型beans的替代方法来使用properties。

application.properties
在properties文件中增加几个属性

users.servers[0]=dev.bar.com
users.servers[1]=foo.bar.com

添加ConnectionSettings 类

@Component
/*Spring Boot 会将users开头的属性按照名字匹配注入到ConnectionSettings对象中。*/
@ConfigurationProperties(prefix="users")
public class ConnectionSettings {
    private String name;
    private Integer age;
    private List<String> servers = new ArrayList<String>();

   // ... getters and setters
}

然后只需要注入ConnectionSettings 就ok了

 @Autowired
 private ConnectionSettings connection;

同样的@ConfigurationProperties也可以在@Bean方法上使用它

@Profile

Spring Profiles提供了一种隔离应用程序配置的方式,并让这些配置只能在特定的环境下生效。

Profile特定配置文件

application-{profile}.properties 如:application-prod.properties

在本地模拟出一个开发环境和生产环境
创建application-prd.properties 和 application-dev.properties

这里写图片描述

application-dev.properties

server.port=8080
server.context-path=/demo
users.name=zhangsan
users.age=22
users.servers[0]=dev.bar.com
users.servers[1]=foo.bar.com

application-prd.properties

server.port=8081  
server.context-path=/demo
users.name=zhangsan
users.age=22
users.servers[0]=dev.bar.com
users.servers[1]=foo.bar.com

application.properties

spring.profiles.active=dev

启动之后
这里写图片描述

修改spring.profiles.active为prd并启动
这里写图片描述

以编程方式设置profiles
SpringApplication app = new SpringApplication(DemoApplication.class);
app.setAdditionalProfiles("prd");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值