说明:
1)、SpringBoot启动会加载大量的自动配置类
3)、我们再来看这个自动配置类中到底配置了哪些组件;(只要我们要用的组件有,我们就不需要再来配置了)
4)、给容器中自动配置类添加组件的时候,会从properties类中获取某些属性。我们就可以在配置文件中指定这些属性的值;
开发环境:
–jdk1.8.0:Spring Boot 推荐jdk1.8及以上;java version "1.8.0_112"
–maven3.6.1:maven 3.3以上版本;Apache Maven 3.3.9
–IntelliJIDEA2017:IntelliJ IDEA 2017.2.2 x64
–SpringBoot 1.5.9.RELEASE:1.5.9;
1)打开idea,点击create New project
2)按照1,2,3点击next
3)输入groupId和artifactid,点击下一步
4)输入project name点击next
5)添加pom.xml依赖
//springboot的父项目是
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
//springboot的启动器,帮我们导入了web模块正常运行所依赖的组件;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
//说明:Spring Boot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器
6)创建一个主程序(主程序不能单独在一个包下,会加载不到)
package com.liuhui;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Created by LLH on 2019/11/24.
*/
@SpringBootApplication(此注解是说明这是一个springboot应用)
public class HelloMain {
public static void main(String[] args) {
SpringApplication.run(HelloMain.class,args);
}
}
7)写一个简单controller类
package com.liuhui.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created by LLH on 2019/11/24.
*/
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "hello springboot";
}
}
8)在主程序下进行运行
1、运行在控制台查看开启的端口,在浏览器中访问。出现这种原因,是没有访问到页面
在浏览器中输入访问地址http://localhost:8080/hello
简化部署:
<!-- 这个插件,可以将应用打包成一个可执行的jar包;在idea中,点击package,会生成jar包,直接运行
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
输入name,group,artiface,package其他默认就可以,点击下一步
点击需要的启动器,我们这只用spring web,之后一直next,完成项目
SpringBoot使用一个全局的配置文件,配置文件名是固定的;
•application.properties
•application.yml
1、yaml语法介绍
k:(空格)v;分号后面必须有空格
yaml以空格控制层级关系,只要左对齐都是一个层级关系
server: port: 8081 path: /hello
yaml对大小写敏感;
2、值的写法
(1)字符串默认不用加上单引号或者双引号;
"":双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思
name: "zhangsan \n lisi":输出;zhangsan 换行 lisi
‘‘:单引号;会转义特殊字符,特殊字符最终只是一个普通的字符串数据
name: ‘zhangsan \n lisi’:输出;zhangsan \n lisi
3、
student:
name:tom
age:18
行内写法:student: {name: tom,age: 18}
数组:list、set
用(-)表示数组中的一个元素
animal:
- dog
- cat
- pig
行内写法:animal: [dog,cat,pig]
@ConfigurationProperties | @Value | |
---|---|---|
功能 | 批量注入配置文件中的属性 | 一个个指定 |
松散绑定(松散语法) | 支持 | 不支持 |
SpEL | 不支持 | 支持 |
JSR303数据校验 | 支持 | 不支持 |
复杂类型封装 | 支持 |
说明:
都可以获取配置文件yml和properties他们都能获取到值;
如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value;
如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties;
示例:
读取yaml配置文件:
server: port: 8081 person: listName: 张三 age: 18 sex: nan birthday: 2019/11/25 map: {k1: v1,k2: v2} list: - aa - bb - cc dog: dogName: 小狗 dogAge: 2
获取配置文件的内容:
package com.liuhui.domain; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Map; /** * Created by LLH on 2019/11/25. */ @Component//将数据加入到容器中 @ConfigurationProperties(prefix = "person") public class Person { private String listName; private Integer age; private String sex; private Date birthday; private Map<String,Object> map; private List list; private Dog dog; @Override public String toString() { return "Person{" + "listName=‘" + listName + ‘\‘‘ + ", age=" + age + ", sex=‘" + sex + ‘\‘‘ + ", birthday=" + birthday + ", map=" + map + ", list=" + list + ", dog=" + dog + ‘}‘; } public String getListName() { return listName; } public void setListName(String listName) { this.listName = listName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Map<String, Object> getMap() { return map; } public void setMap(Map<String, Object> map) { this.map = map; } public List getList() { return list; } public void setList(List list) { this.list = list; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } }测试输出
package com.liuhui; import com.liuhui.domain.Person; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class SpringbootQuickApplicationTests { @Autowired Person person; @Test public void contextLoads() { System.out.println(person); } }
读取properties文件内容
package com.liuhui.domain; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Map; /** * Created by LLH on 2019/11/25. */ @Component//将数据加入到容器中 @ConfigurationProperties(prefix = "person") public class Person { private String listName; private Integer age; private String sex; private Date birthday; private Map<String,Object> map; private List list; private Dog dog; @Override public String toString() { return "Person{" + "listName=‘" + listName + ‘\‘‘ + ", age=" + age + ", sex=‘" + sex + ‘\‘‘ + ", birthday=" + birthday + ", map=" + map + ", list=" + list + ", dog=" + dog + ‘}‘; } public String getListName() { return listName; } public void setListName(String listName) { this.listName = listName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Map<String, Object> getMap() { return map; } public void setMap(Map<String, Object> map) { this.map = map; } public List getList() { return list; } public void setList(List list) { this.list = list; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } }测试类
package com.liuhui; import com.liuhui.domain.Person; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class SpringbootQuickApplicationTests { @Autowired Person person; @Test public void contextLoads() { System.out.println(person); } }
package com.liuhui.domain; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Map; /** * Created by LLH on 2019/11/25. */ @Component @PropertySource(value = "classpath:person.properties") public class Person { private String listName; private Integer age; private String sex; private Date birthday; private Map<String,Object> map; private List list; private Dog dog; @Override public String toString() { return "Person{" + "listName=‘" + listName + ‘\‘‘ + ", age=" + age + ", sex=‘" + sex + ‘\‘‘ + ", birthday=" + birthday + ", map=" + map + ", list=" + list + ", dog=" + dog + ‘}‘; } public String getListName() { return listName; } public void setListName(String listName) { this.listName = listName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Map<String, Object> getMap() { return map; } public void setMap(Map<String, Object> map) { this.map = map; } public List getList() { return list; } public void setList(List list) { this.list = list; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } }
2、
Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;
想让Spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上
@ImportResource(locations = {"classpath:beans.xml"})
@Configuration声明是一个配置类:就是来替代之前的Spring配置文件
@Bean //将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名
${random.value}、${random.int}、${random.long}
${random.int(10)}、${random.int[1024,65536]}
打印日志输出到控制台
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class HelloWorld { public static void main(String[] args) { Logger logger = LoggerFactory.getLogger(HelloWorld.class); logger.info("Hello World"); } }日志级别:
日志的级别;
由低到高 trace<debug<info<warn<error
可以调整输出的日志级别;日志就只会在这个级别以以后的高级别生效更改日志输出位置:
logging.path Example Description (none) (none) 只在控制台输出 指定文件名 (none) my.log 输出日志到my.log文件 (none) 指定目录 /var/log 输出到指定目录的 spring.log 文件中
原文:https://www.cnblogs.com/liulianghui/p/11937299.html