����λ�ã���ҳ > �����̳� > �̳� > ���Ŷ��棺SpringBoot��ȡ�������ȼ�˳����ʲô��

���Ŷ��棺SpringBoot��ȡ�������ȼ�˳����ʲô��

��Դ������������|��ʱ�䣺2024-05-09 11:56:01 |���Ķ���108��|�� ��ǩ�� T Ri S in ���� ���� �� |����������

���Ⲣ��������Spring Boot���ü��ص����ȼ������ڱ���Ӧ�õİ�ȫ�ԡ���ά�����Լ����Ͳ����Ӷ�������Ҫ���ر����ڴ��ģ΢����ܹ��У����������ù�����Ǩ�ƶ�������ϵͳ���ȶ������Ų��ɺ��ӵ����á�

����

Spring Boot��Ϊһ����������JavaӦ�ó����ܣ����俪�伴�á����ٴ����Ŀ������Ӯ���˹�󿪷��ߵ����������������֮һ���Ǽ����ù��̣�ʹ�������ܹ�������Ӧ���Ӷ���������������Ϊ��ʵ����һ�㣬Spring Boot֧�ַḻ���ⲿ�����û��ƣ�����Ӧ�ó�����ݲ�ͬ�IJ��𻷾���������Ӧ���������ԣ��������޸Ĵ��뱾����

��Spring Boot��̬ϵͳ�У��������Կ��ԴӸ�����Դ��ȡ�����磺Java�����ļ���YAML�ļ������������������в����ȡ���Щ���������ܹ�������ʱ��̬ע�뵽Bean�У�����������ϵͳ�Ŀ���չ�ԺͿ������ԡ�Ȼ����Ϊ��ȷ��һ���Ժͷ�ֹ���ó�ͻ��Spring Boot�ڼ�����Щ�ⲿ����ʱ��ѭһ���ϸ�����ȼ�˳�������������ȼ�����������Ҫ����Ϊ��ֱ��Ӱ����������Ч����������ֵ������������Ӧ�ó������Ϊģʽ��

���Ľ�����̽��Spring Boot�����ⲿ�������Ե����ȼ������꾡������������Դ�ļ���˳�򣬲����ʵ��Ӧ�ó�������˵�����Ա������ܹ�����Ч�ع�����Ǩ�����ã�ȷ���ڲ�ͬ������Ӧ�ó������ȶ���׼ȷ�����С�

Spring Boot�ⲿ�����ø���

Spring Boot�ĺ��ļ�ֵ֮һ������ǿ����ⲿ��������������ʹ��Ӧ�ó����ܹ��ڲ��ı������������Ӧ��ͬ�����л������ⲿ��������ζ�Ž�Ӧ�ó���Ĺؼ�������Ϣ����Ӧ�ó������֮�⣬���ڸ��ݲ�ͬ�������翪�������ԡ������ȣ����ж��ƻ����á�Spring Boot�ṩ�˶��������ⲿ����Դ�Լ���ݵ�����ע�뷽ʽ��ʹ���������û��Ʊ���쳣��������ڹ�����

����������Դ

Spring Boot֧�ֶ������͵��ⲿ����Դ����Ҫ�����¼������棺

  1. Properties�ļ�:
    ͨ��ʹ�� .properties ��ʽ�����ü�ֵ�Ե���ʽ�洢������Ϣ��
server.port=8080
logging.level.root=DEBUG
  1. YAML���
    ����ڴ�ͳ��properties�ļ���YAML�ṩ�˸�ֱ�ۡ���θ����������ݽṹ�������ʺϴ洢�������á�ʹ�� .yml ��ʽ��
server:
  port: 8080
logging:
  level:
    root: DEBUG
  1. �������� ��
    ����ϵͳ����Ļ����������Ա�Spring Bootʶ����Ϊ����Դ��������ƻ�����������������Ϊʵ�á�

  2. ������� ��
    ����Spring BootӦ��ʱ�����Դ��������в������� -- ��ͷ��ֱ�Ӹ����������á�

����ע�뷽ʽ

��Spring Boot�У��ⲿ���õ�����ֵ����ͨ�����¼��ַ�ʽ�����ע�뵽Bean�С�

  • @Value ע�� ������ֱ�����ֶλò·½·ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ê¹ï¿½Ã´ï¿½×¢ï¿½â£¬ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Öµ×¢ï¿½ëµ½Ä¿ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ð¡ï¿½

  • Environment �ӿ� ��Spring����ṩ�Ļ��������࣬����������ѯ�����Ѽ��ص�������Ϣ��

  • @ConfigurationProperties ע�� �����ڰ�һ��������õ�һ��ר�ŵ�Java Bean�У��ṩ���ṹ�������ù�����ʽ��

���ü������ȼ�

Spring Boot�����Բ�ͬ����Դ��ͬ�����Կ��԰���һ�������ȼ�˳����и��ǡ������ȼ����ϵ��±�ߣ������������Դ������ǰ�������Դ��

  1. Ĭ�����ԣ�ͨ�� SpringApplication.setDefaultProperties �������ã�
  2. @PropertySource ע����ص�����
  3. Config Data���������ݣ��������ļ�ϵͳ������jar�е�application.properties��application-{profile}.properties��
  4. ��������Դ���������������������������ϵͳ���ԡ�JNDI���Եȣ�
  5. Servlet������صij�ʼ������
  6. SPRING_APPLICATION_JSON��ʽ�Ļ���������ϵͳ����
  7. �������
  8. ������ص�����ע�뷽ʽ���� @SpringBootTest �� @DynamicPropertySource �� @TestPropertySource ��

�������ȼ�˳����Դ�ڹ����� Spring Boot Reference Documentation

Spring Boot���ü���˳�����

Ĭ������

Ĭ��������ָSpring Boot������õ�һЩĬ������ֵ�������ڴ��� SpringApplication ʵ��ʱ��ͨ������ setDefaultProperties(Map defaultProperties) �������ṩһ��Ĭ�����ԣ���Щ���Խ������ȼ��أ�����Ҳ�ᱻ�������ø��ǡ�

@SpringBootApplication
public class SpringBootBaseApplication {

	public static void main(String[] args) {
		Map defaultProperties = new HashMap<>();
		defaultProperties.put("server.port", "9000"); // �Զ���Ĭ�϶˿�
		SpringApplication app = new SpringApplication(SpringBootBaseApplication.class);
		app.setDefaultProperties(defaultProperties);
		app.run(args);
	}
}

ÃÀÍŶþÃæ£ºSpringBoot¶ÁÈ¡ÅäÖÃÓÅÏȼ¶Ë³ÐòÊÇʲô£¿

@PropertySourceע��

@PropertySource ע��������Spring Boot�� @Configuration ���ϼ����ⲿ�����ļ�������������������ʹ�� @PropertySource ʱ����Ҫע����ǣ���Щ����Դ���������������ӵ�Spring�� Environment �С���������SpringӦ��������ˢ�£�refresh���׶βŻᱻ�������ز��ϲ������������С�

����Ȥ�Ŀ��Ը�һ��Դ�룬 org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcessors ��ִ�еġ�

Spring Boot�����������ã���������˿ڣ�server.port������־��ܵij�ʼ����������־�������ã��ȣ�Ҳ����Ӧ��������ˢ��֮ǰ�ͱ���ȡ��Ӧ�õġ���ˣ������������ھ���Ҫ��ȡ�����ã�Ӧ��ֱ���� application.properties ���߻��������ȸ��类���ص�����Դ�н������á�

���Ǵ���һ�� propertysource.properties �ļ���

server.port = 9001
coderacademy.name = CoderAcademy

Ȼ�������� @Configuration ������ʹ�� @PropertySource ���� propertysource.properties �ļ���

@PropertySource(value = "classpath:propertysource.properties")
@Configuration
public class MyConfig {

}

������Ӧ��������һ���������ã�

@SpringBootApplication
public class SpringBootBaseApplication {

	public static void main(String[] args) {
		Map defaultProperties = new HashMap<>();
		defaultProperties.put("server.port", "9000"); // �Զ���Ĭ�϶˿�
		SpringApplication app = new SpringApplication(SpringBootBaseApplication.class);
		app.setDefaultProperties(defaultProperties);
		ConfigurableApplicationContext context = app.run(args);
		Environment environment = context.getEnvironment();
		System.out.println("coderacademy.name: " + environment.getProperty("coderacademy.name"));
	}
}

��ӡ�����
ÃÀÍŶþÃæ£ºSpringBoot¶ÁÈ¡ÅäÖÃÓÅÏȼ¶Ë³ÐòÊÇʲô£¿

���Կ��� server.port �����9001���� @PropertySource ���ص����ø����� SpringBoot Ĭ�ϵ�����ֵ��

Config Data���������ݣ�

Config Data���������ݣ���Spring Boot�������ⲿ��Ӧ�����õĺ��IJ��֡���Ҫ���ڲ������ļ��Լ��ⲿ�����ļ���

�ڲ������ļ�

�ڲ������ļ��������Ӧ�������ļ���λ����Ŀ�������jar���ڲ���λ�� src/main/resource Ŀ¼�µ��ļ���

ÃÀÍŶþÃæ£ºSpringBoot¶ÁÈ¡ÅäÖÃÓÅÏȼ¶Ë³ÐòÊÇʲô£¿

�ⲿ�����ļ�

���Խ������ļ�����jar�������ij��·���¡����ַ�ʽ�������ڲ��޸�jar��������±�����á���������ʹ�õ���������( nacos �� apollo ��)��Ҳ����ͨ�� spring.config.location ���� spring.config.additional-location ָ�����ļ��ȡ�

SpringBoot������ʱ��Ĭ�ϴ��ض���Ŀ¼�м�����Щ�����ļ������ǿ��Դ� ConfigDataEnvironment ���ҵ���ЩĿ¼��

ÃÀÍŶþÃæ£ºSpringBoot¶ÁÈ¡ÅäÖÃÓÅÏȼ¶Ë³ÐòÊÇʲô£¿

��Ŀ¼�ļ���˳���ɵ͵���Ϊ��

file:./
file:./config/
file:./config/*/
classpath:/
classpath:/config/

���� file ����Ӧ�ø�Ŀ¼�µ��ļ����� classpath Ϊ resources �µ��ļ���

��Щ�����ļ����������ȼ�˳���ɵ͵���Ϊ��

classpath:/
classpath:/config/
file:./
file:./config/
file:./config/*/

��������SpringBoot2.7�汾��
����SpringBoot�����ڲ������ļ���ִ�������Լ�ԭ������ο���
��Ϊ���棺SpringBoot��ȡ_�����ļ�_��ԭ����ʲô������˳����ʲô��

���Ƿֱ�����ЩĿ¼�´��������ļ� application.properties ��

ÃÀÍŶþÃæ£ºSpringBoot¶ÁÈ¡ÅäÖÃÓÅÏȼ¶Ë³ÐòÊÇʲô£¿

�����ڶ�Ӧ�ļ���д�����ǵ�Ŀ¼·����

1: config.data.path = classpath:./
2: config.data.path = classpath:./config/
3: config.data.path = file:./
4: config.data.path = file:./config/
5: config.data.path = file:./config/dev

������SpringBoot����ʱ��ӡ config.data.path ��ֵ��

@SpringBootApplication
public class SpringBootConfigApplication {

	public static void main(String[] args) {
		Map defaultProperties = new HashMap<>();
		defaultProperties.put("server.port", "9000"); // �Զ���Ĭ�϶˿�
		SpringApplication app = new SpringApplication(SpringBootConfigApplication.class);
		app.setDefaultProperties(defaultProperties);
		ConfigurableApplicationContext context = app.run(args);
		Environment environment = context.getEnvironment();
		System.out.println("config.data.path: " + environment.getProperty("config.data.path"));
	}
}

���Ƿֲ�������֤������֤1,2����ӡ���:

config.data.path: classpath:./config/

������֤1,2,3����ӡ�����

config.data.path: file:./

��֤1,2,3,4,��ӡ�����

config.data.path: file:./config/

��֤1,2,3,4,5,��ӡ�����

config.data.path: file:./config/dev

���ֵ����Դ

RandomValuePropertySource ��Spring Boot�У� RandomValuePropertySource ��һ����������Դ����������Դ�ڹ̶��������ļ��򻷾�������������Spring Boot���������ʱ�Զ����ӡ��������Դ�ṩ���������� random.* ��ͷ�����������������ֵ�����磬������������ļ������� random.int �� random.long �����ԣ�Spring Boot������ʱ��Ϊ��Щ���������������ֵ���������Ҫ������ʱ����һЩ��ʱ�����ֵ�ij����dz����ã�����ʱ���롢������Կ�ȡ�

���������� application.properties ������ random.int=100

random.int=100

������SpringBoot����ʱ��ȡ``random.int`��ֵ��

@SpringBootApplication
public class ConfigApplication
{
    public static void main( String[] args )
    {
        SpringApplication app = new SpringApplication(ConfigApplication.class);
        ConfigurableApplicationContext context = app.run(args);
        Environment environment = context.getEnvironment();
        System.out.println("random.int: " + environment.getProperty("random.int"));
    }
}

��ӡ���Ϊ��

random.int: -510589238

����ÿ����������Ӧ�ã���ӡ�Ľ������һ����

����ϵͳ��������

��Spring Boot�У���������������������Դ��Spring Boot���Զ���Ⲣ������Щ����������ΪӦ�õ��������ԡ����磬����ڲ���ϵͳ�������˻������� MY_APP_PORT=8080 ����ô��Spring BootӦ���п���ͨ�� ${MY_APP_PORT} ���������ֵ��

�������û�������Ϊ config.data.path=�������� :

ÃÀÍŶþÃæ£ºSpringBoot¶ÁÈ¡ÅäÖÃÓÅÏȼ¶Ë³ÐòÊÇʲô£¿

�����������ã���Ȼ��ӡ config.data.path �Ľ��Ϊ��

config.data.path: ��������

Javaϵͳ����

Javaϵͳ������ͨ�� System.setProperty() ��������һϵ�м�ֵ�ԡ�

@SpringBootApplication
public class ConfigApplication
{
    static {
        System.setProperty("config.data.path", "SystemProperty"); // ����ϵͳ����
    }

    public static void main( String[] args )
    {
        SpringApplication app = new SpringApplication(ConfigApplication.class);
        ConfigurableApplicationContext context = app.run(args);
        Environment environment = context.getEnvironment();
        System.out.println("config.data.path: " + environment.getProperty("config.data.path"));
    }
}

��ӡ���Ϊ��

config.data.path: SystemProperty

SPRING_APPLICATION_JSON���������е���ǶJSON����

SPRING_APPLICATION_JSON �� Spring Boot �ṩ��һ�ֻ��ƣ�����ͨ�������������� JSON ��ʽ�����ø�Ӧ�ó�������������������ݻᱻ������һ�� JSON ���󣬲��ϲ���Spring�� Environment �У�������������Դһ����

@SpringBootApplication
public class ConfigApplication
{
    static {
        System.setProperty("config.data.path", "SystemProperty"); // ����ϵͳ����
        System.setProperty("SPRING_APPLICATION_JSON", "{\"config.data.path\":\"SPRING_APPLICATION_JSON���������е���ǶJSON����\"}");
    }

    public static void main( String[] args )
    {
        SpringApplication app = new SpringApplication(ConfigApplication.class);
        ConfigurableApplicationContext context = app.run(args);
        Environment environment = context.getEnvironment();
        System.out.println("config.data.path: " + environment.getProperty("config.data.path"));
    }
}

��ӡ�����

config.data.path: SPRING_APPLICATION_JSON���������е���ǶJSON����

�������

����Spring BootӦ��ʱ������ֱ��ͨ�������в��������ǻ������������ԡ������в���ͨ���� -- ��ͷ�����������������ֵ���� --server.port=8080 �����ַ�ʽ�����ڲ��޸������ļ���ǰ������ʱ����Ӧ�����á������в������нϸߵ����ȼ������Ը�����������Դ�е�����ֵ��

����ʹ�� java -jar ����SpringBoot��

java -jar ./springboot-config-1.0-SNAPSHOT.jar --config.data.path=�������

��ӡ���Ϊ��

config.data.path: �������

����SpringBoot��jar��������ͨ��java -jar����ֱ��ִ�е�ԭ����ο��� �ֽڶ��棺ΪʲôSpringBoot��?jar?����ֱ�����У���˵��Ƕ��Tomcat�����������ҳ�����ת

���������ĸ������йص����ã����ǾͲ�һһϸ˵��

�ܽ�

Spring Boot���ü������ȼ�����ƾ�����Զ��ʵ���������Ҫ�ԡ���һ����ȷ����Ӧ���ڲ�ͬ�����Ͳ��𳡾��µĸ߶�����ԺͿ���ֲ�ԣ�ͬʱҲ���������˿�������ά�Ŷӵ���������ЭͬЧ�ʡ�

���ȼ�˳����Ͻ���ʹ�ÿ������ܹ���ϸ�ؿ������õĸ��Dz㼶���Ӷ�ʹͬһ�ݴ�����Ը��ݲ�ͬ������������ز�ͬ���������ԡ����磬�ڿ��������Ժ����������зֱ����ò�ͬ�����ݿ����ӡ���־�����API��Կ��������Ϣ���������ڴ�����Ӳ���롣

Spring Boot�����ü����������ȿ�����Ĭ�����ã�Ȼ���𲽼����û�ͨ�� @PropertySource ע�����������Դ�������jar������ĸ���application.properties��application-{profile}.properties��YAML�ļ�������������ϵͳ���ԣ�ֱ�������в����ȡ����ֲַ���ز���ȷ����Խ���������Դӵ�и��ߵ����ȼ����Ӷ����Ը���֮ǰ�����ã���Ҳ���������õ�����Ժͼ�ʱ�ԡ�

���Ⲣ��������Spring Boot���ü��ص����ȼ������ڱ���Ӧ�õİ�ȫ�ԡ���ά�����Լ����Ͳ����Ӷ�������Ҫ���ر����ڴ��ģ΢����ܹ��У����������ù�����Ǩ�ƶ�������ϵͳ���ȶ������Ų��ɺ��ӵ����á�ͨ�����������ȼ��������ƿأ������ߺ���ά��Ա�ܹ�����Ӧ�Ը��ӻ����µ����ù�����ս��ʹ��Spring BootӦ�þ߱����õ���չ�Ժ���Ӧ�ԡ�

��������¼���ҵĸ��˲��ͣ� ��ũAcademy�IJ��ͣ�רע����Java�����ɻ�������Java������Spring Boot��Spring Cloud��Mysql��Redis��Elasticsearch���м�����ܹ���ơ������⡢����Ա���Ե�

С���Ƽ��Ķ�

�������������Ľ�Ϊ������Ϣ����������������ͬ���޹۵��֤ʵ��������

RPG RiÐòÕÂ 0.2.1
RPG Ri���� 0.2.1
���ͣ���ɫ����������Ӫ״̬����ʽ��Ӫ�������ԣ� ���ġ���

��Ϸ����

��Ϸ���

��Ϸ��Ƶ

��Ϸ����

��Ϸ�

��RPG_Ri���¡���GameMaker'Child-Dream'������һ����������RPG���Σ���ȫ��ѵĻ��������RPG�dz���Ԫ

�����Ƶ����

����

ͬ������

����

ɨ��ά�����������ֻ��汾��

ɨ��ά����������΢�Ź��ںţ�

��վ�������������������ϴ��������ַ���İ�Ȩ���뷢�ʼ�[email protected]

��ICP��2022002427��-10 �湫��������43070202000427��© 2013~2025 haote.com ������