web应用程序开发案例分析实验二 Spring Boot核心配置和注解

实验二 Spring Boot核心配置和注解

一、实验目的

1、熟悉 Spring Boot 全局配置文件的使用

2、熟悉 Spring Boot 自定义配置

3、掌握 Spring Boot 配置文件属性值注入

4、掌握 Profile 多环境配置

二、实验内容

1、application.properties配置文件

2、application.yaml配置文件

3、使用注解@ConfigurationProperties和@Value注入属性

4、两种注解对比分析

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

6、使用Profile文件进行多环境配置

7、使用@Profile注解进行多环境配置

三、实验步骤

1.application.properties配置文件

(1)创建项目

创建Spring Boot项目

(2)依赖添加及配置

① 在java下面的com.lg.ch02中创建domain文件夹并创建Pet.java文件和Person.java文件

public class Pet {
    private String type;
    private String name;
    public Pet() {
    }
    public String getType() {
        return this.type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String toString() {
        return "Pet{type='" + this.type + '\'' + ", name='" + this.name + '\'' + '}';
    }
}

Person.java文件

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(
        prefix = "person"
)
public class Person {
    private int id;
    private String name;
    private List hobby;
    private String[] family;
    private Map map;
    private Pet pet;

    public Person() {
    }
    public int getId() {
        return this.id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List getHobby() {
        return this.hobby;
    }
    public void setHobby(List hobby) {
        this.hobby = hobby;
    }
    public String[] getFamily() {
        return this.family;
    }
    public void setFamily(String[] family) {
        this.family = family;
    }
    public Map getMap() {
        return this.map;
    }
    public void setMap(Map map) {
        this.map = map;
    }
    public Pet getPet() {
        return this.pet;
    }
    public void setPet(Pet pet) {
        this.pet = pet;
    }
    public String toString() {
        return "Person{id=" + this.id + ", name='" + this.name + '\'' + ", hobby=" + this.hobby + ", family=" + Arrays.toString(this.family) + ", map=" + this.map + ", pet=" + this.pet + '}';
    }
}

③ 修改resources文件夹下的application.properties文件

person.id=1
person.name=tom
person.hobby=play,read,sleep
person.family=father,mother
person.map.k1=v1
person.map.k2=v2
person.pet.type=dog
person.pet.name=kity

④ 修改在test\java下的com.lg.ch02文件夹下的Ch02ApplicationTests.java文件

import com.lg.ch02.domain.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Ch02ApplicationTests {
    @Autowired
    private Person person;
    @Test
    public void contextLoads(){
        System.out.println(person);
    }
}

⑤ 程序运行

2.application.yaml配置文件

① resources文件夹下创建application.yaml并注释application.properties中内容

person:
  id: 2
  name: 张三
  hobby: [sing,read,sleep]
  family: [father,mother]
  map: {k1: v1,k2: v2}
  pet: {type: cat, name: tom}

② 运行程序

3.使用注解@ConfigurationProperties和@Value注入属性

① 在java下面的com.lg.ch02\domain文件夹中创建Student.java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.*;
@Component
public class Student {
    @Value("${person.id}")
    private int id;      //id
    @Value("${person.name}")
    private String name; //名称
    private List hobby;  //爱好
    private String[] family; //家庭成员
    private Map map;
    private Pet pet;   //宠物

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", hobby=" + hobby +
                ", family=" + Arrays.toString(family) +
                ", map=" + map +
                ", pet=" + pet +
                '}';
    }
}

② 修改在test\java下的com.lg.ch02文件夹下的Ch02ApplicationTests.java文件

import com.lg.ch02.domain.Person;
import com.lg.ch02.domain.Student;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Ch02ApplicationTests {
    @Autowired
    private Person person;
    @Autowired
    private Student student;
    @Test
    public void contextLoads(){
        System.out.println(person);
    }
    @Test
    public void studentTest(){
        System.out.println(student);
    }
}

③ 运行程序

4.两种注解对比分析

对比点

@ConfigurationProperties

@Value

底层架构

Spring Boot

Spring

功能

批量注入配置文件中的属性

单个注入

setter方法

需要

不需要

复杂类型属性注入

支持

不支持

松散绑定

支持

不支持

JSR303数据校验

支持

不支持

SpEL表达式

不支持

支持

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

① 在com.lg.ch02文件下创建config并创建MyService.java的空文件

② 在config中创建MyConfig.java文件

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfig {
    @Bean
    public MyService myService(){
        return new MyService();
    }
}

③ 在resources文件夹下创建beans.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="com.sun.ch02.config.MyService" />
</beans>

④ 修改启动类Ch02Application.java

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

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

⑤ 在测试类Ch02ApplicationTests.java中添加

@Test
public void iocTest(){
    System.out.println(applicationContext.containsBean("myService"));
}

⑥ 运行程序

6.使用Profile文件进行多环境配置

(1).创建不同类型的配置文件

① 在resources文件夹下创建application-dev.properties文件

server.port=8081

② 在resources文件夹下创建application-test.properties文件

server.port=8082

③ 在resources文件夹下创建application-prod.properties文件 

server.port=8083

(2).修改resources文件夹下的application.properties文件 

spring.profiles.active=dev

(3).运行程序 

7.使用@Profile注解进行多环境配置

 (1)在config中创建接口文件DBConnector.java

public interface DBConnector {
    public void configure();
}

(2)在config下创建实现接口类的实现类

① DevDBConnector.java

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
@Profile("dev")    // 指定多环境配置类标识
public class DevDBConnector implements DBConnector {
    @Override
    public void configure() {
        System.out.println("数据库配置环境dev");
    }
}

② TestDBConnector.java

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
@Profile("test")   // 指定多环境配置类标识
public class TestDBConnector implements DBConnector {
    @Override
    public void configure() {
        System.out.println("数据库配置环境test");
    }
}

③ ProdDBConnector.java

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
@Profile("prod")   // 指定多环境配置类标识
public class ProdDBConnector implements DBConnector {
    @Override
    public void configure() {
        System.out.println("数据库配置环境prod");
    }
}

(3)在java下面的com.lg.ch02下创建controller文件夹并在其中创建DBController.java文件

import com.lg.config.DBConnector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DBController {
    @Autowired
    private DBConnector dbConnector;
    @GetMapping("/showDB")
    public void showDB(){
        dbConnector.configure();
    }
}

(4)启动项目

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

(1)修改resources文件夹下的application.properties文件

tom.description=tom\u7684\u5E74\u9F84\u53EF\u80FD\u662F${tom.age}
tom.age=${random.int[10,20]}

(2)在test\java下的com.lg.ch02文件夹下的Ch02ApplicationTests.java文件内添加

   @Value("${tom.description}")
    private String description;
    @Test
    public void placeholderTest() {
        System.out.println(description);
    }

(3)程序运行

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小孙同学1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值