springboot整合aop

springboot整合aop主要步骤:①导入依赖,即启动器。②配置好增强类
。主要就这两步

这个案例就是给service层以Impl结尾的类进行增强


第一步:在pom.xml文件添加如下启动器

	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>


		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
			<version>1.2.3</version>
		</dependency>
		<!-- mybatis依赖 -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.0.0</version>
		</dependency>

		<!-- druid连接池 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.1.10</version>
		</dependency>

		<!-- mysql驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.48</version>
		</dependency>


		<!-- freemaker启动器-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-freemarker</artifactId>
		</dependency>


		<!--aop的启动器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-aop</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>


		<!-- shiro ,shiro和quartz不使用时不能放开,不然会报错-->
		<!--<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-spring-boot-web-starter</artifactId>
			<version>1.4.0</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-quartz</artifactId>
		</dependency>-->

		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.9.2</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.9.2</version>
		</dependency>

步骤二:在Advice包下,创建增强类。最好是将被增强的类先创建好,即如下的准备工作写完在写增强类,本案例为了突出重点,因此将增强类写在此处

该案例将该增强类命名为:TimeAdvice

package com.qf.springbootaop.Advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component//Component意思是该类交给ioc容器管理。也就是在使用时可以用Resouces等注解进行注入
@Aspect//这就是下面增强加切点形成的切面
public class TimeAdvice {
     //首先定义切点,即给哪些方法,哪些类增强
     @Pointcut("execution(* com..service.impl.*Impl.*(..))")//用excution告诉aop往哪切。本案例就是给service的impl包下的所有的以Impl结尾的类进行增强
     public void pc(){

     }

    //增强加切点形成切面,以下定义的是增强。3个value定义的切点,2个pointcut定义的切点
    //前置增强
    @Before(value = "pc()")//每个增强上面都需要申明上面创建的切点
    public  void  before(JoinPoint joinPoint){//joinPoint为获取连接点,通过它获取被增强的类的相关信息
        System.out.println("前置增强");
        //获取目标类
        String methodName = joinPoint.getSignature().getName();//通过joinpoint获取被增强的类的方法名
        System.out.println("name = " + methodName);
        String className = joinPoint.getTarget().getClass().getName();//通过jointPoint获取被增强类的类名
        System.out.println("className = " + className);
    }


    //后置增强,下面这个是不报错执行的后置增强,后置增强分为报错走的后置增强,一个分为不报错才执行的后置增强.
    @AfterReturning(pointcut = "pc()")
    public void returning(JoinPoint joinPoint){
        System.out.println("不报错执行的后置增强");
        //获取目标类
        String methodName = joinPoint.getSignature().getName();//通过joinpoint获取被增强的类的方法名
        System.out.println("name = " + methodName);
        String className = joinPoint.getTarget().getClass().getName();//通过jointPoint获取被增强类的类名
        System.out.println("className = " + className);
    }


    //报错执行的后置增强
    @AfterThrowing(pointcut = "pc()",throwing = "throwable")//throwable代表我们的异常对象
    public void afterthrowing(JoinPoint joinPoint,Throwable throwable){//joinPoint用于获取被增强类的相关信息,throwable用于获取发生的异常的信息
        System.out.println("报错执行的后置增强");
        //获取目标类
        String methodName = joinPoint.getSignature().getName();//通过joinpoint获取被增强的类的方法名
        System.out.println("name = " + methodName);
        String className = joinPoint.getTarget().getClass().getName();//通过jointPoint获取被增强类的类名
        System.out.println("className = " + className);

        String message = throwable.getMessage();//获取异常的信息
        System.out.println("throwableMessage = " + message);
    }


    //后置增强,无论发不发生错误都会执行的后置增强
    @After(value = "pc()")
    public void  after(JoinPoint joinPoint){
        System.out.println("无论发不发生错误都会执行的后置增强");
        //获取目标类
        String methodName = joinPoint.getSignature().getName();//通过joinpoint获取被增强的类的方法名
        System.out.println("name = " + methodName);
        String className = joinPoint.getTarget().getClass().getName();//通过jointPoint获取被增强类的类名
        System.out.println("className = " + className);
    }

    //环绕增强
    @Around(value = "pc()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {//环绕增强独有的参数类型,相当于是被增强类的一个占位的,这个方法就用它区分往前增加功能和往后增加的功能
        System.out.println("环绕增强");

        System.out.println("环绕前");
        Object proceed = joinPoint.proceed();//环绕增强的异常一定不能trycatch捕获,不然上面关于异常触发的那些增强就不会被触发
        System.out.println("环绕后");
        return  proceed;
    }
}


springboot整合aop就上面重点两步,下面就和普通的springboot的书写没什么区别了。因为增强类中会用到下面的的某些方法,因此在最后再定义增强类是最好。springboot添加aop对其他类几乎没任何影响


步骤三:创建要被增强的目标类

controller层:
命名为:UserController

package com.qf.springbootaop.controller;

import com.qf.springbootaop.entity.User;
import com.qf.springbootaop.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class UserController {
@Resource
    private UserService userService;
@RequestMapping("all")
    public String getAll(ModelMap modelMap){
    List<User> users = userService.queryList();
    modelMap.addAttribute("users",users);
    return "show";
}
}

service层:
service中的接口:UserService

package com.qf.springbootaop.service;

import com.qf.springbootaop.entity.User;

import java.util.List;

public interface UserService {
    List<User>  queryList();
}

UserService接口对应的实现类:UserServiceImpl

package com.qf.springbootaop.service.impl;

import com.qf.springbootaop.dao.UserDao;
import com.qf.springbootaop.entity.User;
import com.qf.springbootaop.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService{
    @Resource
    private UserDao userDao;

    @Override
    public List<User> queryList() {
        return userDao.selectAll();
    }
}

dao层:
命名为:UserDao

package com.qf.springbootaop.dao;

import com.qf.springbootaop.entity.User;

import java.util.List;

/**
 * 作者:赵伟风
 * 项目名:springboot-ssm-freemaker-03
 * 时间:2019/11/20  9:21
 * 描述:
 */
public interface UserDao {

    List<User> selectAll();
}

entity:

package com.qf.springbootaop.entity;



import java.io.Serializable;


public class User  implements Serializable {


    private String username;

    private String password;




    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }


}


步骤四:在启动类中扫描dao层接口

package com.qf.springbootaop;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.qf.springbootaop.dao")
public class SpringbootaopApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootaopApplication.class, args);
	}

}


步骤五:在resouces文件下创建mapper文件夹,定义dao层对应的sql语句

命名为:UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qf.springbootaop.dao.UserDao">

    <select id="selectAll" resultType="com.qf.springbootaop.entity.User">
          select * from user;
    </select>

</mapper>

步骤六:配置application.yaml属性文件,配置数据库等信息

#服务器端口号
server:
  port: 8080

#数据库信息
spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/shiroweb
    driver-class-name: com.mysql.jdbc.Driver
    #前后缀
  freemarker:
    prefix: /
    suffix: .ftl
    cache: false    #缓存
    content-type: text/html
  resources:
   #修改静态资源加载默认位置
    static-locations: classpath:/static
  servlet:
    multipart:
    #设置上传文件的大小
      max-request-size: 1000MB
      max-file-size: 500MB
#mybatis配置
mybatis:
  #设置加载mapper文件路径
  mapper-locations: mapper/*.xml
#  type-aliases-package: com.itqf.springbootssmfreemaker03.entity
#  configuration:
#    map-underscore-to-camel-case: true
#    auto-mapping-behavior: full
#    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

#shiro拦截器
shiro:
  loginUrl: /login    #登录地址
  unauthorizedUrl: /403      #未授权跳往403.ftl这个模板文件
  enabled: true  #web项目的支持
#\u914D\u7F6Efreemaker




本案例结构图:
在这里插入图片描述
本案例运行结果:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值