一、Spring Boot 简介
简化spring 应用开发的一个框架,整个spring技术栈的一个整合,J2EE开发的一站式解决方案。
二、微服务
微服务是一种架构风格,一个应用应该是一组小型服务,可以通过HTTP的方式进行互通,每一个功能都是一个可独立替换和独立升级的软件单元。
spring boot 环境准备
JDK1.8
MAVEN3.3.9
SPRINGBOOT 1.5.9
POM文件配置
4.0.0
com.spring.cn
springboot_demo
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.5.6.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework
springloaded
org.springframework.boot
spring-boot-devtools
com.alibaba
fastjson
1.2.15
org.springframework.boot
spring-boot-starter-thymeleaf
net.sourceforge.nekohtml
nekohtml
全局配置文件
# 全局配置文件
server.port=8089
server.context-path=/index
# 自定义属性
auth.name=Jack
auth.age=12
# 日志文件路径
#logging.level.root=INFO
logging.file=D:\\logger\\log
#配置视图解析 整合JSP
#spring.mvc.view.prefix=/jsps/
#spring.mvc.view.suffix=.jsp
#整合Thymeleaf
#关闭模板缓存,开发时使用,否则看不到实时页面
spring.thymeleaf.cache=false
#检查模板是否存在
spring.thymeleaf.check.template-locatio=true
#指定模板context-type
spring.thymeleaf.content-type=text/html
#指定模板格式
spring.thymeleaf.mode=LEGACYHTML5
#检查模板前缀
spring.thymeleaf.prefix=classpath:/templates/
#检查模板后缀
spring.thymeleaf.suffix=.html
相关配置介绍
SpringBoot日志管理
默认使用LogBack管理日志,并用INFO级别输出到控制台
日志输出格式:
时间 :精确到毫秒
日志级别 : ERROR,WARN,INFO,DEBUG,
进程ID :
分隔符 :- 标识实际日志的开始
线程名 :括号括起来的
Logger:源代码类名
日志内容
如果设置以INFO级别输出日志,则INFO以下级别的日志不会打印
在全局配置文件中设置日志输出级别
logging.level.root=INFO
设置日志文件路径,二者不能同时使用
logging.file 绝对路径
例:logging.path = /var/log 会在文件目录下生成spring.log日志文件
### springboot访问静态文件
默认路径是classes/resources 从浏览器访问时,无需写resources路径,直接访问即可
如果需求覆盖默认的路径
spring.resources.static-locations=classpath:/自定义路径/
修改后的路径,在访问时也无需添加
主程序类
主程序类就是带有main方法的Java类,是springboot程序的入口。
@SpringBootApplication
public class Run {
public static void main(String[] args) {
SpringApplication.run(Run.class, args);
}
}
@SpringBootApplication 包含以下三种注解,应用于主程序类
- @Configuration 生产bean对象,
- @EnableAutoConfiguration 自动配置,@EnableAutoConfiguration(exclude= {SolrAutoConfiguration.class}) 取消某个组件的自动配置
- @ComponentScan 扫描子包,注册bean
springboot自动配置原理:
- 主程序类main()中的SpringApplication.run(),会加载META-INF/spring.factories文件,文件中是所有需要自动装配的类文件,将所有类生成实例化对象。
spring-boot-autoconfigure-1.5.6.RELEASE.jar(文件目录)
springboot 交互方式
@Controller; @RestController
如果Controller中的每个方法都是返回Resultful内容,不需要进行跳转,则可以使用@RestController 注解,方法上无需再引用@ResponseBody
在全局配置文件中,已经设置server.port=8089;server.context-path=/index
访问方式:http://localhost:8089/index/hello.do
@Controller
public class DispacherController {
@RequestMapping("/hello.do")
@ResponseBody
public String helloWorld() {
return "Helle World ...";
}
}
Rest格式传参 请的url中传参
访问方式:http://localhost:8089/index/rest/value
注意: msg 的值就是 value
@Controller
public class DispacherController {
@RequestMapping("/rest/{msg}")
public String rest(@PathVariable String msg) {
return "restful is yoo 是是的 "+msg;
}
}
自定义属性
- ConfigurationProperties(prefix="auth") 获取配置文件中前缀是auth的自定义属性
@ConfigurationProperties(prefix="auth")
public class DispacherController {
获取全局配置文件中的自定义属性 @Value,值赋给注解下面的成员变量
@Value("${auth.name}")
private String authName ;
通过@ConfigurationProperties获取属性值,要求成员变量与自定义属性名相同
private String age;
通过@ConfigurationProperties获取属性值,要添加属性的get/set
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
springboot model to json
public class Converter {
springboot json转换;使用@Bean将消息转换器注入到容器中
@Bean
public HttpMessageConverters fastjsonConverter() {
//创建FastJson消息转换器
FastJsonHttpMessageConverter convert = new FastJsonHttpMessageConverter();
//创建FastJson配置对象
FastJsonConfig config = new FastJsonConfig();
//对数据进行json格式化
config.setSerializerFeatures(SerializerFeature.PrettyFormat);
convert.setFastJsonConfig(config);
HttpMessageConverter> con = convert;
return new HttpMessageConverters(con);
}
}
@RestController
public class FastjsonController {
@RequestMapping("/info")
public Person getInfo() {
Person p =new Person();
p.setAge(12);
p.setName("小米");
return p;
}
}
restful : {"name":"小米","age":12}
springboot 自定义拦截器
@Configuration //声明拦截器配置
public class Interceptor extends WebMvcConfigurerAdapter{
@Override
public void addInterceptors(InterceptorRegistry registry) {// 重写添加拦截器方法 ; registry 注册拦截器对象
// TODO Auto-generated method stub
//通过匿名内部类的方式,定义拦截器
HandlerInterceptor handler = new HandlerInterceptor() {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("preHandle is begin");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("postHandle is procesing");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println("afterCompletion is after");
}
};
//注册拦截器 ,指明拦截的url /** 表示所有请求
registry.addInterceptor(handler).addPathPatterns("/info");
}
}
springboot 整合JSP,Thymeleaf 参照全局配置文件内容