@SpringBootAppliaction 标注是一个集合 包含以下标注
执行SpringApplication.run(带有标注的类) 运行SpringBoot框架
@SpringBootConfiguration-->@Configuration-->@Component
实体类
类名前加 @SpringBootConfiguration 相当于applicationContext.xml
方法前加 @Bean(自定义名字/否则按方法名) 相当于applicationContext.xml里的<bean>标签
使用jsp页面和html页面,要导入依赖,jsp页面放在webapp,html页面要放在src/resource下的template
<dependency> //jsp 依赖
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
-<dependency> //html 依赖
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
自定义ErrorController(异常全局处理)
自定义ErrorController组件,需要实现ErrorController接口或继承AbstractErrorController都可以。
@Controller
public class MyErrorController implements ErrorController{
//自定义/error请求处理逻辑
@RequestMapping("/error")
public ModelAndView error(){
ModelAndView mav = new ModelAndView();
mav.setViewName("error");//error.html
return mav;
}
@Override
public String getErrorPath() {
return "/error";
}
}
使用@ExceptionHandler
处理某个Controller异常,应用方法就是在Controller添加带Exception参数的方法,然后使用@ExceptionHandler标记。
@ExceptionHandler
@ResponseBody
public MyResult handlerException(Exception e){
MyResult result = new MyResult();
result.setStatus(0);
result.setMsg("参数有错");
result.setData(e.getMessage());
return result;
}
SpringBootMVC拦截器
-
编写拦截器组件,实现HandlerInterceptor接口
@Component//扫描 public class SomeInterceptor implements HandlerInterceptor{ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("开始执行Controller处理"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // TODO Auto-generated method stub System.out.println("Controller执行完毕"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // TODO Auto-generated method stub String param = request.getQueryString(); System.out.println(request.getRequestURI()+"?"+param+"请求处理完毕"); } }
-
配置拦截器
@Configuration public class InterceptorConfiguration extends WebMvcConfigurerAdapter{ @Autowired private SomeInterceptor some; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(some).addPathPatterns("/compute.do"); } }
SpringBoot AOP
AOP关键概念:切面、切入点、通知。
案例:记录每个Controller方法执行的时间
- 切面:计算方法执行时间
- 切入点:所有Controller方法 within(cn.xdl.controller.*)
- 通知:环绕通知 @Around
-
在pom.xml追加aop定义
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
-
编写切面组件,使用@Aspect、@Around等标记
@Component//ioc @Aspect//定义为切面 public class WatchBean { @Around("within(cn.xdl.controller.*)") public Object execute(ProceedingJoinPoint pjp) throws Throwable{ //开始计时 StopWatch watch = new StopWatch(); watch.start(); Object obj = pjp.proceed();//执行controller方法 //结束计时 watch.stop(); long time = watch.getTotalTimeMillis();//执行时长 String targetClass = pjp.getTarget().getClass().getName();//组件名 String methodName = pjp.getSignature().getName();//方法名 System.out.println("组件:"+targetClass+"方法:"+methodName+"执行时长为:"+time+"ms"); return obj; } }
SpringBoot静态资源管理
在SpringBoot工程中,默认静态资源目录如下:
src/main/resources/public (最低)
src/main/resources/static
src/main/resources/resources
src/main/resources/META-INF/resources(最高)
资源目录根据优先级,从高到低查找。
如果需要自定义静态资源存储路径,可以采用下面配置类
@Configuration
public class MyResourceConfiguration extends WebMvcConfigurerAdapter{
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/mystatic/**")
.addResourceLocations("classpath:/mystatic/");
}
}
提示:不要使用/**映射,会破坏原有默认静态资源访问。
SpringBoot对JavaWeb集成
-
Servlet
编写Servlet组件,继承HttpServlet
利用@WebServlet注解配置
在启动类中,添加@ServletComponentScan
-
案例代码
@WebServlet(name="helloservlet",urlPatterns="/hello.do") public class HelloServlet extends HttpServlet{ public void service( HttpServletRequest request,HttpServletResponse response) throws IOException{ response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("Hello Servlet"); out.close(); } }
-
Filter
编写Filter组件,实现Filter接口
利用@WebFilter注解配置
在启动类中,添加@ServletComponentScan
-
案例代码
@WebFilter(urlPatterns="/hello.do",filterName="somefilter") public class SomeFilter implements Filter{ @Override public void init(FilterConfig filterConfig) throws ServletException { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("-----doFilter------"); chain.doFilter(request, response);//调用后续servlet\jsp等 } @Override public void destroy() { // TODO Auto-generated method stub } }
-
Listener
- 编写Listener组件
- 利用@WebListener注解配置
- 在启动类中,添加@ServletComponentScan
-
启用druid连接池的监控功能
-
配置启用StatViewServlet组件
@WebServlet(urlPatterns="/druid/*",initParams={ @WebInitParam(name="loginUsername",value="xdl"), @WebInitParam(name="loginPassword",value="123") }) public class DruidStatServlet extends StatViewServlet{ }
-
配置启用WebStatFilter组件
@WebFilter(urlPatterns="/*",initParams={ @WebInitParam(name="exclusions",value="*.js,*.jpg,*.css,/druid/*") }) public class DruidStatFilter extends WebStatFilter{ }
-
打开浏览器访问
-