标签:auth span .property value 依赖注入 drive framework spring容器 .data
使用idea自带插件创建项目
一直下一步到完成
application.properties
local.ip.addr = 192.168.2.110 redis.host = 192.168.3.11 redis.auth = redis-01 redis.port = 6380 gerry.datasource.driverClassName = com.mysql.jdbc.Driver gerry.datasource.url = jdbc:mysql:///db gerry.datasource.username = root gerry.datasource.password = 123
application.yml
local: ip: test: 192.168.2.220-yml
主方法类
package com.boot.config.bootconfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.Environment; @SpringBootApplication /** * 总结springBoot项目配置: * 1、通过环境Environment方式读取配置文件中的信息 * 2、通过@Value方式来读取配置信息 * 3、通过@Configuration+@PropertySource("指定配置文件的路径") * 4、通过@ConfigurationProperties指定配置类 * 注意:配置文件的加载顺序及文件名称 * 1、config/application.properties * config/application.yml * application.properties * application.yml * 2、指定配置文件(名称,路径)(设置到运行参数中, * 如设置带vm参数中要加-Dspring.config.name=xxx/axxx/app) * 设置到program参数中直接--spring.config.name=xxx/axxx/app * --spring.config.name=xxx/axxx/app(后缀可以省略) * --spring.config.location=classpath:xxx.properties/yml多个配置文件可以使用逗号分隔 * file:/D:/tmp/app.properties 指定绝对路径 * */ public class BootConfigApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(BootConfigApplication.class, args); // 通过spring容器获取环境对象 Environment env = context.getEnvironment(); String ip = env.getProperty("local.ip.addr"); String testId = env.getProperty("local.ip.test"); String currentDir = env.getProperty("user.dir");//当前目录 String envProperty = env.getProperty("spring.app.name"); String namePro = env.getProperty("gerry.name"); System.out.println(ip+","+currentDir+","+testId+","+envProperty+","+namePro); // 通过@Value方式读取 RedisConfig redisConfig = context.getBean(RedisConfig.class); redisConfig.show(); // 通过ConfigurationProperties方式 DatasourceProperties datasourceProperties = context.getBean(DatasourceProperties.class); System.out.println(datasourceProperties); context.close(); } }
RedisConfig
package com.boot.config.bootconfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class RedisConfig { @Autowired private DatasourceProperties dp; @Value("${redis.port:6379}") private int port; @Value("${redis.host:127.0.0.1}") private String host; @Value("${redis.auth:123}") private String auth; public void show() { System.out.println(port+","+host+","+auth+","+dp.getDriverClassName()); } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getAuth() { return auth; } public void setAuth(String auth) { this.auth = auth; } }
DatasourceProperties
package com.boot.config.bootconfig; /*@Component @ConfigurationProperties(prefix = "gerry.datasource")*/ public class DatasourceProperties { private String driverClassName; private String url; private String username; private String password; public String getDriverClassName() { return driverClassName; } public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "DatasourceProperties{" + "driverClassName=‘" + driverClassName + ‘\‘‘ + ", url=‘" + url + ‘\‘‘ + ", username=‘" + username + ‘\‘‘ + ", password=‘" + password + ‘\‘‘ + ‘}‘; } }
DataSourceConfig(通过依赖注入获取实例得到DatasourceProperties实例)
package com.boot.config.bootconfig.config; import com.boot.config.bootconfig.DatasourceProperties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class DataSourceConfig { @Bean @ConfigurationProperties(prefix = "gerry.datasource") public DatasourceProperties dp() { return new DatasourceProperties(); } }
FileConfig (使用此方式可配置运行时参数)
package com.boot.config.bootconfig; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @Configuration @PropertySource("classpath:conf/app.yml") // 不生效 @PropertySource("classpath:conf/app1.properties") @PropertySource("file:/D:/tmp/app.properties") public class FileConfig { }
标签:auth span .property value 依赖注入 drive framework spring容器 .data
原文地址:https://www.cnblogs.com/lm970585581/p/9800425.html