SpringBoot整合Web之AOP怎么配置

发布时间:2022-08-15 16:21:52 作者:iii
来源:亿速云 阅读:168

这篇文章主要介绍“SpringBoot整合Web之AOP怎么配置”,在日常操作中,相信很多人在SpringBoot整合Web之AOP怎么配置问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”SpringBoot整合Web之AOP怎么配置”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

配置AOP

AOP简介

要介绍面向切面变成(Aspect-Oriented Programming,AOP),需要先考虑一个这样的场景:公司有一个人力资源管理系统目前已经上线,但是系统运行不稳定,有时运行的很慢,为了检测到底是哪个环节出现问题了,开发人员想要监控每一个方法执行的时间,再根据这些执行时间判断出问题所在。当问题解决后,再把这些监控移除掉。系统目前已经运行,如果手动修改系统成千上万个方法,工作量太大,而且这些监控方法以后还要移除掉;如果能够在系统运行过程中动态添加代码,就能很好的解决问题。这种在系统运行时动态添加代码的方式成为面向切面编程(AOP)。Spring Boot 对 AOP 提供了很好的支持。在 AOP 中,有一些常见的概念需要了解:

Spring Boot 支持

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",查看结果

SpringBoot整合Web之AOP怎么配置

自定义 favicon

favicon.ico 是浏览器选项卡左上角的图标,可以放在静态资源路径下或者类路径下,静态资源路径下的 favicon.ico 优先级高于类路径下的 favicon.ico

可以使用在线转换网站:https://www.bitbug.net/ 将一张普通图片转为 .ico 图片,转换成功后,将文件重命名为 favicon.ico ,然后复制到 resources/static 目录下,如图

SpringBoot整合Web之AOP怎么配置

启动项目,查看效果

SpringBoot整合Web之AOP怎么配置

注意:清缓存,然后 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

添加前

SpringBoot整合Web之AOP怎么配置

添加后

SpringBoot整合Web之AOP怎么配置

到此,关于“SpringBoot整合Web之AOP怎么配置”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. Spring AOP之切面的配置
  2. Springboot2 如何去配置AOP日志

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot aop web

上一篇:ES6字符串的扩展实例分析

下一篇:vue怎么实现动态设置元素的高度

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》