SpringBoot学习笔记(三):SpringBoot的配置文件(2)
发布日期:2021-05-10 07:53:43 浏览次数:14 分类:精选文章

本文共 5157 字,大约阅读时间需要 17 分钟。

SpringBoot������������

������������������������������

���SpringBoot������������������������������������������������������������������������������������������������������������������������������������������������������������������

1. @PropertySource������

@PropertySource���������������������������������������������������������������������������������������������������������������������������������������������

��������������� ������������������������������������������������������������@PropertySource������������������������������

���������

@PropertySource("classpath:teacher.properties")
@Component
@ConfigurationProperties(prefix = "teacher")
public class Teacher {
// ���������������������������������������
}

���������������

  • YAML������������@Value������������������������������������null���
  • ������������������������������UTF-8���������

2. @ImportResource������

@ImportResource������������Spring���������������������������������Spring bean���������������������������������

���������������

  • ���������������������Spring bean������������������������������bean���������
  • ������������SpringBoot������������������������������������������

���������

@ImportResource(locations = "classpath:bean.xml")
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class);
}
}

���������������

  • ���������������������������������Spring bean���������������
  • bean.xml���������������������bean���������

3. @Bean������

@Bean������������������������������������bean���������������������������������������������������SpringBoot������������������������

���������������

  • ���������������������������Spring bean���
  • ���������������������������������(���������Dal������)���������������������������������

���������

@Service
public class UserService {
// ������������������������������
}
@Configuration
public class MyConfig {
@Bean(name = "userService")
public UserService getInstance() {
return new UserService();
}
}

���������������������������

���SpringBoot������������������������������������������������������������������������������������������������������������������������

1. ���������������

���������������������������������������������������������

������������������${random.value},${random.int},${random.long},${random.int(10)},...

2. ���������������������������

������������������������������key���������������������������������������������

teacher.name=tom ${person.name: jerry}_dog

3. ������������

application.properties���������

# ���������������������
num������������person.num=${random.int(10)}
# name1���tom���name2���������person.name1=tom# student1���tom���������person.student1.name=${person.name1}_student# student2���name2������������������name2���������������,���������������jerry person.student2.name=${person.name2: jerry}_student

Person������

@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private Integer num;
private String name1;
private String name2;
private Student student1;
private Student student2;
}

Student������

public class Student {
private String name;
private String address;
}

���������������SpringBootTest������������������������������������������������

������SpringBoot���������������

���������������������������������������������������������������������SpringBoot������������������������������Spring Profile���������������������

1. ������YAML������������������

SpringBoot������YAML���������������������������������������������������Profile���

spring:
profiles:
active: dev
---
server:
port: 8081
---
server:
port: 8082
spring:
profiles: dev

������������"active"���������������������������������������Profile���������������������������������������

2. ������������������

  • ������������������������
java -jar spring-boot.jar --spring.profiles.active=test
  • ��������������������� ���IntelliJ IDEA���Eclipse������������VM������-Dspring.profiles.active=dev���

  • ��������������������������� ���������������������������������������

export SpringBoot_PROFILES_ACTIVE=dev

������������������������������

SpringBoot������������������������������������������������������������������

  • /file:./config/
  • /file:./
  • /classpath:/config/
  • /classpath:/
  • ���������������������������������������������������������������������������������������������������

    ������������������������ ������������������spring.config.location���������������������������������������������������������������������������������������������������������

    java -jar spring-boot.jar --spring.config.location=D:/application.properties

    ������������������������

    SpringBoot���������������������������������������������

  • ������������������������������������
  • JNDI������������
  • ������������������
  • Java System Properties 5.en��m i��in���������������������
  • ���������������������������
  • ������������������
  • ������������������������
  • @Configuration������������@PropertySource
  • SpringApplication.setDefaultProperties
  • ������������������������������������������������������

    ���������������������SpringBoot������������������������������������������������������������������������������������������������������������

    上一篇:SpringBoot学习笔记(四):SpringBoot中的日志
    下一篇:SpringBoot学习笔记(二):SpringBoot的配置文件(1)

    发表评论

    最新留言

    做的很好,不错不错
    [***.243.131.199]2025年04月27日 20时52分38秒