新建立一个maven项目
![v2-7202e027b3bc86d5e8c876247941f630_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=ecd9d93a-292f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-7202e027b3bc86d5e8c876247941f630_b.jpg)
![v2-bd9bc4cc91fc8fd79e28d8954c115407_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=ecd9d93a-292f-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-bd9bc4cc91fc8fd79e28d8954c115407_b.jpg)
进入spring官网
- (http://spring.io/)访问网站,点击project找到springboot子项目
![v2-359b4b900e9478812352fbeee6e88024_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=ecd9d93a-292f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-359b4b900e9478812352fbeee6e88024_b.jpg)
- 点击learn,选择一个版本,查看文档(以前的版本没有找到文档,就直接用最新的了)
知识拓展:看下面图片能够发现有一些标志SHAPSHOT,GA等等
![v2-89a26e1995fea22b56fbb94006d3b9c6_b.jpg](http://img-02.proxy.5ce.com/view/image?&type=2&guid=ecd9d93a-292f-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-89a26e1995fea22b56fbb94006d3b9c6_b.jpg)
流程:SNAPSHOT->alpha->beta->release->GA
Alpha:是内部测试版,一般不向外部发布,会有很多Bug.一般只有测试人员使用。
Beta:也是测试版,这个阶段的版本会一直加入新的功能。在Alpha版之后推出。
RC:(Release Candidate) 顾名思义么 ! 用在软件上就是候选版本。系统平台上就是发行候选版本。RC版不会再加入新的功能了,主要着重于除错。
GA:General Availability,正式发布的版本,在国外都是用GA来说明release版本的。
详情查看:https://www.cnblogs.com/lexiaofei/p/7065720.html
- 点击Getting Started(入门)进入springboot官方教学
- 查看目录 System Requirements(系统要求)查看硬性要求,最低需要java8,可以通过java-version命令查看自己的java版本信息
- 查看Maven Installation(maven安装),复制依赖的jar包
<?
项目运行
创建一个注入的实体bean
package com.yxj.spring;
/**
* @ProjectName: springBootDemo
* @Package: com.yxj.spring
* @Description:
* @Author: 杨小杰
* @CreateDate: 2019/1/18 20:22
* @UpdateUser: 暂无
* @UpdateDate: 2019/1/18 20:22
* @UpdateRemark: The modified content
* @Version: 1.0
*/
public class User {
private String userName;
private String passWord;
public String getUserName() {
return userName;
}
public User setUserName(String userName) {
this.userName = userName;
return this;
}
public String getPassWord() {
return passWord;
}
public User setPassWord(String passWord) {
this.passWord = passWord;
return this;
}
}
新建一个main方法,通过@SpringBootApplication和@Bean注解创建bean依赖注入并输出
package com.yxj.spring;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
/**
* @ProjectName: springBootDemo
* @Package: com.yxj.spring
* @Description:
* @Author: 阿杰
* @CreateDate: 2019/1/18 20:18
* @UpdateUser: 暂无
* @UpdateDate: 2019/1/18 20:18
* @UpdateRemark: The modified content
* @Version: 1.0
*/
@SpringBootApplication
public class SpringBootTestRun {
@Bean
public User getUserBean(){
return new User();
}
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(SpringBootTestRun.class, args);
System.out.println("-------------分割线---------------");
User getUserBean = run.getBean(User.class);
System.out.println(getUserBean);
}
}
运行main方法,得到输出结果
关于本类中使用的注解标签含义,详情查看:https://www.jianshu.com/p/4e1cab2d8431
读取配置文件
springboot会默认去读取
- classPath:application.properties
- classPath:/config/application.properties
- file:application.properties
- file:/config/application.properties
下面的内容,后面会写读取yml这方面的文章。
在maven项目的resource目录下面新建一个application.properties文件
localhost.test.userName=test123
localhost.test.passWord=passWordTest
test.remark=test userName is ${localhost.test.userName}
配置文件加入测试配置key与value值
- 通过 @Value("${配置文件的key}") 方式读取配置文件的值
- 如果配置文件中没有这个key,通过@Value("${给定的key:默认值}")可以给定默认值,如果不加“:默认值”会报错。
- 如果配置文件有key,但是value为空,给定默认值这种方式,不会得到默认值的结果,会输出空值
package com.yxj.spring;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
/**
* @ProjectName: springBootDemo
* @Package: com.yxj.spring
* @Description:
* @Author: 阿杰
* @CreateDate: 2019/1/18 20:18
* @UpdateUser: 暂无
* @UpdateDate: 2019/1/18 20:18
* @UpdateRemark: The modified content
* @Version: 1.0
*/
@SpringBootApplication
public class SpringBootTestRun {
@Value("${localhost.test.userName}")
private String testUserName;
@Value("${localhost.test.passWord}")
private String testPassWord;
@Value("${localhost.test.default:testDefault}")
private String testDefault;
@Value("${test.remark}")
private String testRemark;
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(SpringBootTestRun.class, args);
System.out.println("-------------分割线---------------");
SpringBootTestRun bean = run.getBean(SpringBootTestRun.class);
System.out.println(bean.testUserName);
System.out.println(bean.testPassWord);
System.out.println(bean.testRemark);
System.out.println(bean.testDefault);
}
}
- 通过Environment对象获取
package com.yxj.spring;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.ConfigurableEnvironment;
/**
* @ProjectName: springBootDemo
* @Package: com.yxj.spring
* @Description:
* @Author: 阿杰
* @CreateDate: 2019/1/18 20:18
* @UpdateUser: 暂无
* @UpdateDate: 2019/1/18 20:18
* @UpdateRemark: The modified content
* @Version: 1.0
*/
@SpringBootApplication
public class SpringBootTestRun {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(SpringBootTestRun.class, args);
System.out.println("-------------分割线---------------");
ConfigurableEnvironment environment = run.getEnvironment();
String userName = environment.getProperty("localhost.test.userName");
String passWord = environment.getProperty("localhost.test.passWord");
String testRemark = environment.getProperty("test.remark");
System.out.println(userName);
System.out.println(passWord);
System.out.println(testRemark);
}
}
- 测试结果
-------------分割线---------------
test123
passWordTest
test userName is test123
修改默认配置文件的名字
- 修改默认配置文件的名称,点击Edit Configurations
![v2-ec5314b0eaeb69afd9ee5d6a839519d2_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=ecd9d93a-292f-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-ec5314b0eaeb69afd9ee5d6a839519d2_b.jpg)
- 添加参数 --spring.config.name=修改配置文件的前缀名称
![v2-5bfb06bd671fa14ba214dc69a158631e_b.jpg](http://img-02.proxy.5ce.com/view/image?&type=2&guid=ecd9d93a-292f-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-5bfb06bd671fa14ba214dc69a158631e_b.jpg)
修改默认路径和配置文件的名字
假设上级目录名称为testConfig,里面有一个test.properties
- spring.config.location=classpath:/testConfig/test.properties
- spring.config.location=file:D://test/testConfig/test.properties
- spring.config.location=classpath:XXX,file:XXX…… 指定多个
指定多个配置文件
- 上面spring.config.location,或者spring.config.name这种方式
- 通过@PropertySource注解方式,添加多个@PropertySource,也可以使用
@PropertySources(@PropertySource("classpath:app.properties"),@PropertySource("classpath:appTwo.properties")) 这种方式
package com.yxj.spring;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
/**
* @ProjectName: springBootDemo
* @Package: com.yxj.spring
* @Description:
* @Author: 阿杰
* @CreateDate: 2019/1/18 20:18
* @UpdateUser: 暂无
* @UpdateDate: 2019/1/18 20:18
* @UpdateRemark: The modified content
* @Version: 1.0
*/
@SpringBootApplication
@PropertySource("classpath:app.properties")
@PropertySource("classpath:appTwo.properties")
public class SpringBootTestRun {
@Value("${localhost.test.userName}")
private String testUserName;
@Value("${app.test.value}")
private String appValue;
@Value("${appTwo.test.value}")
private String appTwoValue;
/**
* 也可以通过file这种方式来指定配置文件的位置
* @param args
*/
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(SpringBootTestRun.class, args);
System.out.println("-------------分割线---------------");
SpringBootTestRun bean = run.getBean(SpringBootTestRun.class);
System.out.println(bean.testUserName);
System.out.println(bean.appValue);
System.out.println(bean.appTwoValue);
}
}
app.properties
app.test.value=app
appTwo.properties
appTwo.test.value=appTwo
测试结果
-------------分割线---------------
test123
app
appTwo
- 未完待续。。。