Commit 3e5002da authored by duanledexianxianxian's avatar duanledexianxianxian 😁

Example

parent 1a1280bf
package com.duanledexianxianxian.core; package com.duanledexianxianxian.core;
import com.duanledexianxianxian.core.config.SystemEnvironmentProperties;
import com.duanledexianxianxian.core.config.SystemProperties; import com.duanledexianxianxian.core.config.SystemProperties;
import com.duanledexianxianxian.core.config.SystemPropertySourceValueProperties;
import com.duanledexianxianxian.core.config.SystemValueProperties;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
/** /**
* springboot 应用程序入口 * springboot 应用程序入口
*
* @author duanledexianxianxian * @author duanledexianxianxian
*/ */
@SpringBootApplication @SpringBootApplication
public class SpringWebApplication { public class SpringWebApplication {
public static void main(String[] args) { public static void main(String[] args) {
ApplicationContext applicationContext= (ApplicationContext) SpringApplication.run(SpringWebApplication.class, args); ApplicationContext applicationContext =
SystemProperties properties=applicationContext.getBean(SystemProperties.class); SpringApplication.run(SpringWebApplication.class, args);
System.out.println("用户的当前工作目录:"+System.getProperty("user.dir")); SystemProperties systemProperties = applicationContext.getBean(SystemProperties.class);
// 打印出配置参数 SystemValueProperties valueProperties = applicationContext.getBean(SystemValueProperties.class);
System.out.println(properties.getName()); SystemEnvironmentProperties environmentProperties = applicationContext.getBean(SystemEnvironmentProperties.class);
SystemPropertySourceValueProperties propertySourceValueProperties = applicationContext.getBean(SystemPropertySourceValueProperties.class);
System.out.println("用户的当前工作目录:" + System.getProperty("user.dir"));
// 通过@value加载配置方式
System.out.println("@Value:" + valueProperties);
System.out.println("@ConfigurationProperties:" + systemProperties);
System.out.println("Environment:" + environmentProperties.getEnvironment().getProperty("system.name"));
System.out.println("@PropertySource+@Value+@ConfigurationProperties:" + propertySourceValueProperties);
} }
......
package com.duanledexianxianxian.core.config;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
/**
* 通过Environment加载配置参数
* @author duanledexianxianxian
*/
@Component
@Data
public class SystemEnvironmentProperties {
@Autowired
private Environment environment;
}
package com.duanledexianxianxian.core.config;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* 通过@value加载配置参数
* @author duanledexianxianxian
*/
@Component
@ConfigurationProperties(prefix = "system.property")
@PropertySource(value = { "/config/system.properties" })
@Data
public class SystemPropertySourceValueProperties {
/**
* 名称
* //最好设置默认值,否则配置文件即使有相应key,也会应用启动失败
*/
@Value("${name:null}")
private String name;
/**
* 编码
* //最好设置默认值,否则配置文件即使有相应key,也会应用启动失败
*/
@Value("${code:null}")
private String code;
}
package com.duanledexianxianxian.core.config;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 通过@value加载配置参数
* @author duanledexianxianxian
*/
@Component
@Data
public class SystemValueProperties {
/**
* 名称
*/
@Value("${system.name}")
private String name;
/**
* 编码
*/
@Value("${system.code}")
private String code;
}
server: server:
port: 8080 port: 8080
#系统相关配置
system: system:
name: "classpath root config" # 系统名称
\ No newline at end of file name: "classpath root config"
# 系统编码
code: "classpath root code"
\ No newline at end of file
system.property.name=\u7CFB\u7EDF\u540D\u79F0
system.property.code=\u7CFB\u7EDF\u7F16\u7801
#name=ssss
#code=ssss
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment