十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
方式一:
成都创新互联专业为企业提供徐州网站建设、徐州做网站、徐州网站设计、徐州网站制作等企业网站建设、网页设计与制作、徐州企业网站模板建站服务,十余年徐州做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
@Configuration
//@PropertySource("classpath:jdbc.properties")
//@PropertySource("classpath:config/jdbc222222.properties")
@PropertySources({@PropertySource("classpath:jdbc.properties"),@PropertySource("classpath:config/jdbc222222.properties")})
public class FileConfig {
}
@Component
public class Dbconfig {
@Value("${db.url}")
private String dbUrl;
@Value("${db.port}")
private Integer dbPort;
@Value("${db.password}")
private String dbPassword;
@Value("${db.username}")
private String dbUserName;
public void show(){
System.out.println(">>>>>>>>>>>Dbconfigz中获取到的参数");
System.out.println("dbUrl========="+dbUrl);
System.out.println("dbPort"+ "========="+dbPort);
System.out.println("dbPassword"+ "========="+dbPassword);
System.out.println("dbUserName"+ "========="+dbUserName);
}
}
配置文件的信息,配置文件的名称是jdbc.properties。路径是在resources下
db.password=root
db.username=root
data.url=127.0.0.12
data.port=12343
data.name=1233
data.password=root3
测试的主方式是:
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
context.getBean(Runnable.class).run();
//String property = context.getEnvironment().getProperty("local.ip");
//System.out.println("获取到的参数是:"+property);
//context.getBean(UserConfig.class).show();
context.getBean(Dbconfig.class).show();
context.getBean(DataSourceProperties.class).show();
context.close();
}
方式二:通过environment获取外部的配置信息
@Component
public class UserConfig {
@Autowired
Environment environment;
@Value("${local.port}")
private String localPort;
@Value("${local.port}")
private Integer localPort2;
@Value("${tomcat.port:9090}")
private String tomcatPort;
public void show(){
System.out.println("application.properties中的local.ip的值:"+environment.getProperty("local.ip"));
System.out.println("application.properties中的local.port的值:"+environment.getProperty("local.port"));
System.out.println("application.properties中的local.port的值:"+environment.getProperty("local.port", Integer.class));
System.out.println("application.properties中的name的值:"+environment.getProperty("name"));
System.out.println("application.properties中的app.name的值:"+environment.getProperty("app.name"));
System.out.println("application.properties中的tomcat.port的值:"+environment.getProperty("tomcat.port"));
System.out.println("application.properties中的tomcat.port的值:"+tomcatPort);
}
}
方式三:
@Component
@ConfigurationProperties(prefix="data")
public class DataSourceProperties {
private String url;
private String port;
private String name;
private String password;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void show(){
System.out.println(">>>>>>>>>>>>url"+url);
System.out.println(">>>>>>>>>>>>port"+port);
System.out.println(">>>>>>>>>>>>name"+name);
System.out.println(">>>>>>>>>>>>password"+password);
}
}
总的说明:
* 两种方法获取配置信息中的信息
* 配置文件的文件名字为application.properties
* 默认的位置在classpath根目录,或者classpath:/config file:/ file:config/
*
* 默认的配置文件名字可以使用 --spring.config.name来指定,只需要指定文件的名字,文件扩展名可以省略。
* 默认的配置文件路径可以使用--spring.config.location来指定,
* 配置文件需要指定全路径,包括目录和文件名字,还可以指定多个,多个用逗号隔开
* --spring.config.location=classpath:config/app.properties(如果是文件则file:文件的路径)
*
spring boot 支持获取数组,集合的配置
package com.zcp.springstart;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 支持获取数组,集合
* 配置方式为:name[index]=value
* 在application.properties文件中的写法:
* data.url=127.0.0.1
data.port=1234
data.name=123
data.password=root.post
ds.hosts[0]=128.128.128.0
ds.hosts[1]=128.128.128.1
ds.hosts[2]=128.128.128.2
ds.hosts[3]=128.128.128.3
ds.ports[0]=8888
ds.ports[1]=8889
ds.ports[2]=8890
ds.ports[3]=8891
ds.ports[4]=8892
*
Title: TomcatProperties
*
Description:
*
Company: www.itcast.cn
* @version 1.0
*/
@Component
@ConfigurationProperties("ds")
public class TomcatProperties {
private List
private String[] ports;
public String[] getPorts() {
return ports;
}
public void setPorts(String[] ports) {
this.ports = ports;
}
public List
return hosts;
}
public void setHosts(List
this.hosts = hosts;
}
@Override
public String toString() {
return "TomcatProperties [hosts=" + hosts + ", ports=" + Arrays.toString(ports) + "]";
}
}
在application.properties文件中的配置信息入下:
data.url=127.0.0.1
data.port=1234
data.name=123
data.password=root.post
ds.hosts[0]=128.128.128.0
ds.hosts[1]=128.128.128.1
ds.hosts[2]=128.128.128.2
ds.hosts[3]=128.128.128.3
ds.ports[0]=8888
ds.ports[1]=8889
ds.ports[2]=8890
ds.ports[3]=8891
ds.ports[4]=8892
测试的主方式是:
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
context.getBean(Runnable.class).run();
//String property = context.getEnvironment().getProperty("local.ip");
//System.out.println("获取到的参数是:"+property);
//context.getBean(UserConfig.class).show();
//context.getBean(Dbconfig.class).show();
//context.getBean(DataSourceProperties.class).show();
System.out.println(context.getBean(TomcatProperties.class));
context.close();
}