介绍
Spring4.x和Spring Boot都推荐使用Java配置( 约定【规范】大于配置
按照这个规范来,很多的配置项可以省略)。
自动装配
Spring Boot启动的时候会通过@EnableAutoConfiguration注解找到META-INF/spring.factories配置文件中的所有自动配置类,并对其进行加载,而这些自动配置类都是以AutoConfiguration结尾来命名的,它实际上就是一个JavaConfig形式的Spring容器配置类,它能通过以Properties结尾命名的类中取得在全局配置文件中配置的属性如:server.port,而XxxxProperties类是通过@ConfigurationProperties注解与全局配置文件中对应的属性进行绑定的。
版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
启动类
@SpringBootApplication // 启动类上加的注解,类需要写在业务类的外面
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TPR10MNRVT6FFROBDOsJGcohVYsR2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnLxAzN2ATOzEjMwEjNwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
配置文件(下面的案例基本基于这个配置文件)
yml和properties文件的区别
properties文件不支持中文(中文需要换成unicode编码)
yml文件支持中文
springboot 2.0.2.RELEASE配置文件doc文档地址
https://docs.spring.io/spring-boot/docs/2.0.2.RELEASE/reference/htmlsingle/#common-application-properties
server:
port: 80
#DB Configuration:
spring:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
username: root
password: 123
#JPA Configuration:
jpa:
database: MySQL
show-sql: true
hibernate:
ddl-auto: update
redis:
host: 127.0.0.1
port: 6379
data:
elasticsearch:
cluster-name: my-elasticsearch
cluster-nodes: 127.0.0.1:9300,127.0.0.1:9301,127.0.0.1:9302
#spring集成Mybatis环境
#pojo别名扫描包
mybatis:
type-aliases-package: com.itheima.domain
#加载Mybatis映射文件
mapper-locations: classpath:mapper/*Mapper.xml
#spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/mystatic/
person:
name: 小明
password: 123
hobbies: [抽烟,喝酒,烫头]
获取配置文件
public class HelloController {
@value("${person.name}")
private String name;
@value("${person.password}")
private String password;
}
获取配置文件前缀
@ConfigurationProperties(prefix = "person")
public class HelloController {
private String name;
private String password;
private String[] hobbies;
public String[] getHobbies() {
return hobbies;
}
public void setHobbies(String[] hobbies) {
this.hobbies = hobbies;
}
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;
}
}
yml
person:
name: 'lilili'
password: 666
hobbies: '厉害了'
springboot集成freemarker
controller
- springboot集成的freemarker类上不能写@RestController,得写@Controller
springboot集成的freemarker的controller层函数的参数传递时,后端不需要使用注解(例如@RequestParam),直接写前端一样的参数就行,
当接收或者向前端传递的时候,还需要使用@ResponseBody和@RequestBody
函数参数传递是对象
// 服务端
@PostMapping("/addManager")
@ResponseBody
public String insertManager(
@RequestBody CmsManager manager) throws Exception {
String password = this.encodeByBCrypt(manager.getPassword());
manager.setPassword(password);
return managerInfoService.addManagers(manager);
}
// 前端
if (unametishi== "") {
$.ajax({
url:"/manager/addManager",
type:"post",
data:JSON.stringify(manager),
contentType:'application/json',
dataType: "json",
success:function(data){
$('#confirmMsg').text(data.msg);
if(data.flag==true)
{
$('#success_modal').modal();
}
}
})
}
函数参数传递是字段
// 服务端
@RequestMapping("/update")
@ResponseBody
public String updatePermissionById(Long id, String permissionName, String description, String url) {
return permissionService.updatePermissionById(id, permissionName, description, url);
}
// 前端
$("#btn_savePermission").click(function(){
let id=$('#permissionId').val();
let permissionName = $('#name').val().trim();
let description = $('#description').val().trim();
let url=$('#link').val().trim();
$.ajax({
type:"POST",
url:"authority/update",
data:{
'id':id,
'permissionName':permissionName,
'description':description,
'url':url
},
dataType:'json',
}).done(function (data) {
$('#confirmMsg').html(data.msg);
$('#success_modal').modal();
});
})
template
目录结构
list.ftl
<html>
<head>
<title>这是一个springboot集成的freemarker小例子</title>
</head>
<body>
<table border="1px" align="center" cellspacing="0px" width="80%">
<tr>
<td>id</td>
<td>用户名</td>
<td>密码</td>
<td>昵称</td>
</tr>
<#list users as user >
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
<td>${user.nickname}</td>
</tr>
</#list>
</table>
</body>
</html>
静态资源的目录
静态资源根目录规定的spring源码的位置
修改静态资源文件的目录(就是不用spring定义好的,我们自己定义)
springboot集成mybatis
配置文件
#spring集成Mybatis环境
#pojo别名扫描包
mybatis.type-aliases-package=com.itheima.domain
#加载Mybatis映射文件
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
启动类
package com.itheima;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
/*
* 指明mybatis的mapper接口所在位置的
* Mapper接口和映射文件在同一目录下的话
* 就不用写配置文件中的Mapper路径了
* */
@MapperScan("com.itheima.mapper")
// 启用事务控制,@Transactional
@EnableTransactionManagement
public class App {
public static void main(String[] args) {
System.setProperty("es.set.netty.runtime.available.processors","false");
SpringApplication.run(App.class,args);
}
}
springboot定时任务
@SpringBootApplication
// 开启使用定时任务,使用spring自带的定时任务
@EnableScheduling
public class CrawlerApplication {
public static void main(String[] args) {
SpringApplication.run(CrawlerApplication.class,args);
}
}
// 这两个不是在同一个类中,用spring定时任务的这个类需要交给spring管理
// 添加定时任务配置,这里一般是用corn表达式
// initialDelay,项目启动成功后,多久执行任务,单位毫秒
// fixedDelay,任务执行完成后,间隔多久下一次任务执行,单位毫秒
@Scheduled(initialDelay = 1000, fixedDelay = 10000)
public void run(){
}
Spring RestTemplate中几种常见的请求方式spring发http请求
研究一下
部分知识引用自:
https://blog.csdn.net/u014745069/article/details/83820511#comments_13807858