Spring boot 第2章预习

Spring Boot 核心配置与注解

1.核心配置

spring boot 的全局配置文件:application.properties 和 application.yaml

这两种配置文件的作用相同,如果同时都配置了,那么properties 将覆盖yaml

application.properties

#普通属性值的配置
server.port=8089
server.servlet.context-path=/chapter02

#对象类型
#基本数据类型
Person.id=1
Person.name=张三
#list数据类型
Person.hobby=play,eat
#String数据类型
Person.family=father,mother
#map数据类型
Person.map.k1=v1
Person.map.k2=v2
#对象数据类型
Person.pet.type=dog
Person.pet.name=hello dog

application.yaml

#普通属性值的配置
server:
  servlet:
    context-path: /chapter02
  port: 8081
#对象类型
Person:
  id: 2
  name: lisi
  hobby: [eat,sleep]
  family: [father,mather]
  map: {k1: v1,k2: v2}
  pet: {type: cat, name: tom}

2.使用注解加在核心配置文件

  1. @Component 注解

    加在类的上面,生成当前类的实例并存到IoC中

  2. @ConfigurationProperties ( prefix = “xxx” ) 注解

    加在类上面,和@Component 一起使用,将核心配置文件前缀xxx的配置项注入到这个类中,

    配置项中的属性名与类的set 方法名相对应

  3. @Value("${xxx.yyy}") 注解

    加在普通数据类型(如String)的变量上面,xxx 表示核心配置文件的配置项的名称,yyy 表示配置项的属性名,将yyy 的属性值注入到当前的变量中

  4. 两种注解的比较

    对比点 @ConfigurationProperties @Value
    底层框架 Spring Boot Spring
    功能 批量注入配置文件中的属性 单个注入
    属性set 方法 需要 不需要
    复杂类型属性注入 支持 不支持
    松散绑定(一种语法) 支持 不支持
    JSR303数据校验(例如Email格式) 支持 不支持
    SpEL表达式(例如数据库信息) 不支持 支持

3.使用@PropertySource 注解加载自定义配置文件

创建配置文件test.properties

test.id=2
test.name=wangwu

创建配置类

package cn.edu.sjzc.chapter02.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration  // 指定当前类为配置类
@PropertySource("classpath:test.properties") // 指定配置文件的位置和名称
@EnableConfigurationProperties(MyProperties.class) // 开启配置类的属性注入功能
@ConfigurationProperties(prefix = "test") // 前缀为test 的配置项
public class MyProperties {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "MyProperties{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

注意:@Component 注解的效果 和@Configuration 注解和@EnableConfigurationProperties 注解 组合起来使用的效果相同

然后配置文件中配置项的xxx属性的属性值 就注入到了配置类的xxx属性中

4.使用@ImportResource 注解加载XML 配置文件

创建MyService 类

package cn.edu.sjzc.chapter02.service;

public class MyService {
}

在resource文件夹下创建xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="myService" class="cn.edu.sjzc.chapter02.service.MyService"></bean>
</beans>

在启动类上加上@ImportResource(“classpath:beans.xml”) 注解,表示xml 文件的路径和名称

package cn.edu.sjzc.chapter02;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource("classpath:beans.xml")
public class Chapter02Application {

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

}

在测试类添加ApplicationContext属性,通过该类的getBean方法获取xml 文件中的bean的实例

package cn.edu.sjzc.chapter02;

@SpringBootTest
class Chapter02ApplicationTests {

    @Autowired
    private ApplicationContext applicationContext;
    @Test
    void contextLoads() {
       MyService myService =  (MyService)applicationContext.getBean("myService");
        System.out.println(myService);
    }

}

打印的结果为 cn.edu.sjzc.chapter02.service.MyService@a34e9f

5.使用@Configuration 编写自定义配置类

创建自定义配置类

package cn.edu.sjzc.chapter02.config;

import cn.edu.sjzc.chapter02.service.MyService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfig {

    @Bean(name = "myService1")  // 将标注方法的返回值存到spring 容器中
    public MyService getMyService(){
        return new MyService();
    }
}


测试

package cn.edu.sjzc.chapter02;

@SpringBootTest
class Chapter02ApplicationTests {

    @Autowired
    private ApplicationContext applicationContext;
    @Test
    void contextLoads() {
       MyService myService1 =  (MyService)applicationContext.getBean("myService1");
        System.out.println(myService1);
    }

}


6.使用Profile 多环境配置

多环境配置文件格式:application-{profile}.properties

1.创建多环境配置文件

application-dev.properties 开发时的配置文件

server.port=8081

application-test.properties 测试时的配置文件

server.port=8082

application-prod.properties 运行环境的配置文件

server.port=8083

2.激活指定环境的方式

  • 命令行方式

    1. 使用maven package 命令打包
    2. 进入target 目录,使用命令 java -jar xxx.jar --spring.profiles.active=yyy 运行打包生成的xxx.jar 文件,yyy 可以写成dev、test、prod 中的其中一种,表示使用其中一种配置文件运行,可以看见端口号根据不同选择 发生了不同的改变
  • 全局配置文件设置spring.profiles.active 属性方式

    在application.properties 配置文件中加入,表示当前使用的配置文件

    spring-profiles-active=dev
    
    

    运行启动类,发现端口号发生了改变

7.@Profile 注解多环境配置

创建

8.随机值设置及参数间引用

  • 配置文件中随机数值设置

在application.properties 配置文件中添加以下内容

#随机值设置
#随机值
my.secret=${random.value}
#随机整数
my.number=${random.int}
#随机长整数
my.bignumber=${random.long}
#随机uuid
my.uuid=${random.uuid}
#10以内的正整数
my.number.less.than.ten=${random.int(10)}
#1024到65536区间范围内的整数
my.number.in.range=${random.int[1024,65536]}

测试

    @Value("${my.number.in.range}")
    private String secrete;

    @Test
    void randomTest(){
        System.out.println(secrete);
    }

在测试类中添加以上属性和方法,便可取出配置文件中设置的随机值,每次取值取出的数都是随机的

  • 配置文件中引用值设置

    在配置文件中添加以下内容

app.name=MyApp
#引用app.name 中的内容
app.description=${app.name} is a wonderful app.

发布了25 篇原创文章 · 获赞 0 · 访问量 1279

猜你喜欢

转载自blog.csdn.net/weixin_42059368/article/details/104541440