一、配置为开发模式
当我们进行修改后,需要重新启动才能看到修改结果。
加入以下依赖,代码做了修改,不用重新运行
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
二、SpringBoot的Web开发
SpringBoot提供了spring-boot-starter-web为Web开发予以支持,spring-boot-starter-web提供了嵌入的Tomcat以及SpringMvc的依赖,Web相关的自动配置存储在spring-boot-autoconfigure.jar的org.springframework.boot.autoconfigure.web下。
三、访问静态资源
在SpringBoot中加载静态资源和在普通的web应用中不太一样。默认情况下,Spring Boot从classpath的/static,/public或/META-INF/resources文件夹或从ServletContext根目录提供静态内容
#自定义静态文件路径,js,css,image等,在application.properties中加入:
spring.resources.static-locations=classpath:/static/
四、自定义消息转化器
只需要在类中添加消息转化器的@Bean,就会被Spring Boot自动加入到容器中。
@Bean
public StringHttpMessageConverter stringHttpMessageConverter(){
StringHttpMessageConverter converter=new StringHttpMessageConverter(Charset.forName("UTF-8"));
return converter;
}
五、自定义拦截器
有些时候我们需要自已配置SpringMVC而不是采用默认,比如说增加一个拦截器,这个时候就得通过继承WebMvcConfigurerAdapter然后重写父类中的方法进行扩展。
@Configuration:表示这是一个配置类
@Configuration
public class MyInterceptor extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
HandlerInterceptor handlerInterceptor=new HandlerInterceptor() {
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println("自定义拦截器.....");
return true;
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub
}
};
registry.addInterceptor(handlerInterceptor).addPathPatterns("/**");
}
六、定义全局异常处理器
创建一个全局异常处理类,如下:
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
@ResponseBody
public Map<String,Object> allExceptionHandler(
Exception exception) throws Exception
{
Map<String,Object> map = new HashMap<String,Object>();
map.put("errorCode", 500);
map.put("errorMsg", exception.toString());
return map;
}
}
七、异步调用
在项目中,当访问其它接口较慢或者做耗时任务时,不想程序一直卡在耗时任务上,想程序能够并行执行,我们可以使用多线程来并行的处理任务,SpringBoot提供了异步处理方式@Async。
1.定义异步调用的service
如下:
@Service
public class AsyncServiceImpl implements AsyncService {
public static Random random =new Random();
@Async
@Override
public Future<String> doTask1() throws Exception {
System.out.println("开始做任务一");
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
System.out.println("完成任务一,耗时:" + (end - start) + "毫秒");
return new AsyncResult<>("任务一完成");
}
@Async
@Override
public Future<String> doTask2()throws Exception {
System.out.println("开始做任务二");
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
System.out.println("完成任务二,耗时:" + (end - start) + "毫秒");
return new AsyncResult<>("任务二完成");
}
@Async
@Override
public Future<String> doTask3() throws Exception{
System.out.println("开始做任务三");
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
System.out.println("完成任务三,耗时:" + (end - start) + "毫秒");
return new AsyncResult<>("任务三完成");
}
}
2.在controller层使用
@Controller
public class TestController {
@Autowired
private AsyncService asyncService;
@RequestMapping("/async")
@ResponseBody
public String getEntityById() throws Exception {
long start = System.currentTimeMillis();
Future<String> task1 = asyncService.doTask1();
Future<String> task2 = asyncService.doTask2();
Future<String> task3 = asyncService.doTask3();
while(true) {
if(task1.isDone() && task2.isDone() && task3.isDone()) {
// 三个任务都调用完成,退出循环等待
break;
}
Thread.sleep(1000);
}
long end = System.currentTimeMillis();
return "任务全部完成,总耗时:" + (end - start) + "毫秒";
}
}
3.在启动类上加入@EnableAsync注解。