要介绍面向切面变成(Aspect-Oriented Programming,AOP),需要先考虑一个这样的场景:公司有一个人力资源管理系统目前已经上线,但是系统运行不稳定,有时运行的很慢,为了检测到底是哪个环节出现问题了,开发人员想要监控每一个方法执行的时间,再根据这些执行时间判断出问题所在。当问题解决后,再把这些监控移除掉。系统目前已经运行,如果手动修改系统成千上万个方法,工作量太大,而且这些监控方法以后还要移除掉;如果能够在系统运行过程中动态添加代码,就能很好的解决问题。这种在系统运行时动态添加代码的方式成为面向切面编程(AOP)。Spring Boot 对 AOP 提供了很好的支持。在 AOP 中,有一些常见的概念需要了解:
Spring Boot 在 Spring 的基础上对 AOP 的配置提供了自动化配置解决方案 spring-boot-starter-aop ,首先引入依赖,如下:
<!-- AOP 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
然后在com.sang.aop.service 包下创建 UserService 类,如下:
@Service public class UserService { public String getUserById(Integer id){ System.out.println("get..."); return "user"; } public void deleteUserById(Integer id){ System.out.println("delete..."); } }
然后创建切面,如下:
@Component @Aspect public class LogAspect { @Pointcut("execution(* com.sang.aop.service.*.*(..))") public void pc1() { } @Before(value = "pc1()") public void before(JoinPoint jp) { String name = jp.getSignature().getName(); System.out.println(name + "方法开始执行..."); } @After(value = "pc1()") public void after(JoinPoint jp) { String name = jp.getSignature().getName(); System.out.println(name + "方法执行结束..."); } @AfterReturning(value = "pc1()", returning = "result") public void afterReturning(JoinPoint jp, Object result) { String name = jp.getSignature().getName(); System.out.println(name + "方法返回值为:" + result); } @AfterThrowing(value = "pc1()",throwing = "e") public void afterThrowing(JoinPoint jp, Exception e) { String name = jp.getSignature().getName(); System.out.println(name+"方法抛异常了,异常是:"+e.getMessage()); } @Around("pc1()") public Object around(ProceedingJoinPoint pjp) throws Throwable { return pjp.proceed(); } }
代码解释:
配置完成后,接下来在Controller 中创建接口,分别调用 Userservice 中的两个方法,即可看到 LogAspect 中的代码动态的嵌入目标方法中执行了,如下:
getUserById方法开始执行...
get...
getUserById方法返回值为:user
getUserById方法执行结束...
deleteUserById方法开始执行...
delete...
deleteUserById方法返回值为:null
deleteUserById方法执行结束...
Spring Boot 项目在启动后,首先会去静态资源路径下查找 index.html 作为首页文件,若查找不到,则会去查找动态的 index.html 作为首页文件。
例如,如果想使用静态的 index.html 页面作为项目的首页,只需在 resources/static 目录下创建 index.html 文件疾苦。若想使用动态页面作为项目首页,则需在 resources/templages 目录下创建 index.html (使用Thymeleaf 模板) 或者 index.ftl(使用 FreeMarker 模板),然后在 Controller 中返回逻辑视图名,如下:
@Controller public class IndexController { @RequestMapping("/index") public String index(){ return "index"; } }
运行项目,输入"http://localhost:8081",查看结果
favicon.ico 是浏览器选项卡左上角的图标,可以放在静态资源路径下或者类路径下,静态资源路径下的 favicon.ico 优先级高于类路径下的 favicon.ico
可以使用在线转换网站:https://www.bitbug.net/ 将一张普通图片转为 .ico 图片,转换成功后,将文件重命名为 favicon.ico ,然后复制到 resources/static 目录下,如图
启动项目,查看效果
注意:清缓存,然后 Ctrl+F5 强制刷新
Spring Boot 中提供了大量的自动化配置类,在 Spring Boot 的入口类上有一个 @SpringBootApplication 注解。该注解是一个组合注解,由 @SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan 组成,其中 @EnableAutoConfiguration 注解开启自动化配置,相关的自动化配置就会被使用。如果开发者不想使用某个自动化配置,按如下方式除去相关配置即可:
@SpringBootApplication @EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class}) public class Chapter04Application { public static void main(String[] args) { SpringApplication.run(Chapter04Application.class, args); } }
在 @EnableAutoConfiguration 注解中使用 exclude 属性去除 Error 的自动化配置类,这时如果在 resources/static/error 目录下创建 4xx.htnl、5xx.html ,访问出错时就不会自动跳转了。由于 @EnableAutoConfiguration 注解的 exclude 属性值是一个数组,因此有多个要排除的自动化配置文件只需要继续添加即可。除了这种配置方式外,也可在 application.properties 中配置,如下:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
添加前
添加后