
本文共 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")@SpringBootApplicationpublic 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������)���������������������������������
���������
@Servicepublic class UserService { // ������������������������������}@Configurationpublic 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���������������������������������������������
������������������������������������������������������
���������������������SpringBoot������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
