springboot学习笔记一
来源:互联网 发布:yy制作头像软件 编辑:程序博客网 时间:2024/05/29 03:59
application.properties中的属性
local.ip=192.168.1.118local.port=8080#配置的引入name=springbootapp.name=this is ${name}ds.url=jdbc:mysql://localhost:3306/testds.driver=com.mysql.jdbc.Driverds.username=rootds.password=root#注入集合list.hosts[0]=192.168.1.2list.hosts[1]=192.168.1.3list.hosts[2]=192.168.1.4
读取application.properties中属性的方式
package da.test;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;@Componentpublic class UserConifg { @Autowired private Environment env; @Value("${local.port}") private String localPort; // 可以类型转换 @Value("${local.port}") private Integer localPort2; // 直接赋值 不用在配置里面 // @Value默认要有配置项,配置项可以为空,如果没有配置项,则可以给默认值 @Value("${tomcat.port:9090}") private String tomcatPort; // 测试获取application.properties的值 public void show() { System.out.println("local ip=" + env.getProperty("local.ip")); // 类型转换成Integer型 System.out.println("local port=" + env.getProperty("local.port", Integer.class)); System.out.println("local port=" + localPort); System.out.println("local port=" + localPort2); System.out.println("name=" + env.getProperty("name")); System.out.println("appname=" + env.getProperty("app.name")); System.out.println("tomcat port=" + tomcatPort); // 下面的为null // System.out.println("tomcat port=" + env.getProperty("tomcat.port")); }}
读取application.properties中的属性,并转换为集合形式
package da.test;import java.util.ArrayList;import java.util.List;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;/** * private List<String> hosts声明的名字要和list.hosts[0]的名字对应 */@Component@ConfigurationProperties("list")public class CollectionConfig { private List<String> hosts = new ArrayList<>(); public void show() { System.out.println("=============CollectionConfig==============="); System.out.println("hosts=" + hosts); } public List<String> getHosts() { return hosts; } public void setHosts(List<String> hosts) { this.hosts = hosts; }}
读取带前缀的属性,通过set方法自动注入
package da.test;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Component@ConfigurationProperties(prefix = "ds")public class DsConfig { private String url; private String driver; private String username; private String password; public void show() { System.out.println("=============DsConfig============="); System.out.println("url=" + url); System.out.println("driveClass=" + driver); System.out.println("username=" + username); System.out.println("password=" + password); } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getDriver() { return driver; } public void setDriver(String driver) { this.driver = driver; } 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; }}
读取application.properties配置的方式1
package da.test;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component;/** * @PropertySource 可以加载一个外部配置,同时写几个可以加多个外部配置 */@Component@PropertySource("classpath:config/db.properties")// @PropertySource("file:/e:/temp/db.properties")public class DbConfig { @Value("${db.url}") private String url; @Value("${db.class}") private String driveClass; public void show() { System.out.println("=============DbConfig============="); System.out.println("url=" + url); System.out.println("driveClass=" + driveClass); }}
读取application.properties配置的方式2
说明:这里的application.properties改名为app.properties了
启动测试文件
package da.test;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ConfigurableApplicationContext;/** * * 默认的配置的名字可以通过--spring.config.name来指定,只需要指定文件的名字,不需要指定拓展的后缀 * * 默认的配置的路径可以通过--spring.config.location来指定。比如:--spring.config.location=classpath * :config/application.properties */@SpringBootApplicationpublic class Springboot01Application { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(Springboot01Application.class, args); context.getBean(UserConifg.class).show(); context.getBean(DbConfig.class).show(); context.getBean(DsConfig.class).show(); context.getBean(CollectionConfig.class).show(); }}
阅读全文
0 0
- springboot学习笔记(一)
- SpringBoot学习笔记(一)
- SpringBoot学习笔记一
- springboot学习笔记一
- Springboot学习笔记(一)
- SpringBoot学习笔记(一)
- SpringBoot 学习笔记一
- springboot 学习笔记(一)
- springboot 学习笔记(一)
- springboot 学习笔记(一)
- springboot学习笔记(一) HelloWorld
- SpringBoot笔记一
- SpringBoot笔记一
- SpringBoot笔记一
- SpringBoot笔记一
- springboot笔记(一)
- springboot学习笔记(一):基础程序和配置
- Spring Boot 学习笔记一(SpringBoot启动过程)
- sql中的函数总结
- 对城市名搜索(汉字、拼音)功能的改进 / 小程序城市区县定位模块改进
- 查看文件md5码
- GCD
- HDU
- springboot学习笔记一
- LINUX Shell 下求两个文件交集和差集的办法
- SVN 相关
- Quak浏览器使用Google
- HDU OJ 2041 超级楼梯
- git常用命令-版本回退
- java中Boolean类型到底占几个字节?
- java:单例模式
- 文章标题