1.springboot热部署
1.1首先添加jar包依赖,若是eclipse工具开发只需添加jar包以来即可若是idea工具还需要配置第二第三步
<!--热部署依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
1.2设置setting
1.3设置Registry 首先Ctrl+Shift+Alt+/弹出对话框选中Registry 然后如图
做到这不出意外就配置成功了,热部署自动部署可能有点延迟,敲完代码不要着急运行,注意控制台有没有自动部署,自我感觉手动部署比较方便,而且boot部署项目也挺快的,此博客只为记录一下热部署配置
2.单元测试
2.1引入单元测试依赖
<!--引入单元测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2.2新建一个测试类注意测试类位置要在test目录下
2.3测试类内容配置
//SpringBoot单元测试
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {BootDay021Application.class})//BootDay021Application.class为启动类要写自己项目中的启动类
public class BootTest {
@Autowired
private Demoservice demoservice;//这是自己随便写的service用来测试验证
@Before
public void befor(){
System.out.println("测试准备");
}
@Test
public void test01(){
//测试内容
demoservice.demo();
}
@After
public void after(){
System.out.println("测试收场");
}
}
3.Springboot自定义异常(后端异常,后端逻辑错误时会触发)
3.1引入依赖
<!--引入 thymeleaf模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
3.2 application.properties配置
#关闭 thymeleaf 模板缓存 (开发环境,建议关闭。如果是生产环境,设置为true)
spring.thymeleaf.cache=false
3.3编写自定义异常类,当发现异常时会自动执行此类
/**
* 自定义异常处理类
*/
@RestControllerAdvice //通知将异常信息返回一个字符串类型
public class MyExceptionHandler {
/**
* 捕获全局异常信息,处理所有可能发生的异常
* @param e
* @param request
* @return
*/
@ExceptionHandler(value=Exception.class) //抛出一个父类异常Exception
public Object handlerExce(Exception e, HttpServletRequest request){
Map<String,Object> map = new HashMap<>();
map.put("code",500); //定义异常码
map.put("url",request.getRequestURL()); //拿到返回异常的 url地址
map.put("msg",e.getMessage()); //拿到异常信息
return map;
}
}
3.4在controller类中写一个异常调用测试
@GetMapping("/test03")
public Object test03(){
int i=1/0;
return serverUtil;
}
测试结果
3.5补充
无意间发现如果在templates目录下放进一个名为error得html文件当执行程序时出现404错误时,会自动跳转到error.html页面
4.注解读取XXX.properties文件
4.1新建一个.properties文件
文件内容:
server.ip=192.168.0.102
server.name=456
4.2 介绍@Component @PropertySource与 @ConfigurationProperties
@Component:说起@Component就会联想到 @service @repository @controller 他们的用途就是把类存放到IOC容器中,分别对应 服务层 持久层 控制层 而@Component则对应哪些分层不明确的类
@PropertySource:读取指定properties文件
@ConfigurationProperties:一般与@PropertySource配合使用用来简化properties中的键值对调用
使用方式看下面类就明白了
@Component
@PropertySource("classpath:demo.properties")//读取properties文件
@ConfigurationProperties(prefix = "server")//properties文件中key值前缀在这里配置
public class ServerUtil {
//因为上面@ConfigurationProperties定义了前缀“server”
//而此处调用的键值对的K值前缀正好时“server”所以@Value可以省略不写
//但是属性名必须时ip因为properties文件中的k值为server.ip前缀上面定义了
//后缀若与属性名相同则会自动赋值若是不同则不会赋值,此处就是自动赋值
// @Value("${server.ip}")//若上面没有使用@ConfigurationProperties赋值方法就是@value("${K值}")
private String ip;
// @Value("${server.name}")
private String name;
public ServerUtil() {
}
public ServerUtil(String ip, String name) {
this.ip = ip;
this.name = name;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4.3编写controller测试是否引用properties文件成功
@Autowired
private ServerUtil serverUtil;//此类是4.2
@GetMapping("/test02")
public Object test02(){
return serverUtil;
}
测试结果引用成功