初识spring-boot中的AOP(一)

面试的时候经常会被问到Spring中事务是如何实现的,学艺不精的我心想一个@Transactional注解不就搞定了么,哎,,too young too simple.最近在学习Spring-boot的内容,先学会简单实现spring-boot中的切面编程。
首先准备好一个简单的用户类,写好我们的控制器层和服务层,这里用的是一个Maven项目。代码如下:
User:
package com.kane.springboot002.aop.bean;

/**
 * 〈一句话功能简述〉<br>
 * 〈用户类〉
 *
 * @author Kane
 * Date:   2019/5/29 10:24
 * @since 1.0.0
 */
public class User {
    private int id;
    private String name;
    private String description;

    public User(int id, String name, String description) {
        this.id = id;
        this.name = name;
        this.description = description;
    }

    public User() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", description='" + description + '\'' +
                '}';
    }
}

控制层:
package com.kane.springboot002.aop.controller;

import com.kane.springboot002.aop.bean.User;
import com.kane.springboot002.aop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 〈一句话功能简述〉<br>
 * 〈用户服务前端控制器〉
 *
 * @author Kane
 * Date:   2019/5/29 13:53
 * @since 1.0.0
 */
@Controller
@RequestMapping("/")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/")
    public String index(){
        return "spring boot 实战";
    }

    @RequestMapping("/print")
    @ResponseBody
    public User printUser(int id,String name,String description){
        User user = new User(id,name,description);
        userService.printUser(user);
        return user;
    }

}
服务层
package com.kane.springboot002.aop.service;

import com.kane.springboot002.aop.bean.User;
import org.springframework.stereotype.Service;

/**
 * 〈一句话功能简述〉<br>
 * 〈用户服务接口〉
 *
 * @author Kane
 * @create 2019/5/29
 * @since 1.0.0
 */
@Service
public interface UserService {
    void printUser(User user);
}

package com.kane.springboot002.aop.service.impl;

import com.kane.springboot002.aop.bean.User;
import com.kane.springboot002.aop.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 〈一句话功能简述〉<br>
 * 〈用户服务实现类〉
 *
 * @author Kane
 * Date:   2019/5/29 10:32
 * @since 1.0.0
 */
@Service
public class UserServiceImpl implements UserService {

    private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
    @Override
    public void printUser(User user) {
        if(user == null){
            throw new RuntimeException("检查用户参数是否为空...");
        }
        logger.info("打印输出用户信息:"+user);
    }
}

然后自定义我们的切面类:注意execution正则表达式的写法,第一个* 后有个空格,不加空格我在IDEA上编辑会报error。@Pointcut就是定义切点,然后通过execution指定我们的连接点,因为是正则表达式,所有我们的连接点可以是某某包下任意方法,注意在spring中连接点指的就是方法。

package com.kane.springboot002.aop.aspect;

import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 〈一句话功能简述〉<br>
 * 〈切面类〉
 *
 * @author Kane
 * Date:   2019/5/29 10:41
 * @since 1.0.0
 */
@Aspect
public class MyAspect {
    private Logger logger = LoggerFactory.getLogger(MyAspect.class);

    @Pointcut("execution(* com.kane.springboot002.aop.service.impl.UserServiceImpl.printUser(..))")
    public void pointCut(){

    }
    @Before("pointCut()")
    public void before(){
        logger.info("before...");
    }

    @After("pointCut()")
    public void after(){
        logger.info("after...");
    }

    @AfterReturning("pointCut()")
    public void afterReturning(){
        logger.info("afterReturning...");
    }

    @AfterThrowing("pointCut()")
    public void afterThrowing(){
        logger.info("afterThrowing...");
    }


}

最后一定要记得在我们的启动类中加入该切面的初始化。

package com.kane.springboot002;

import com.kane.springboot002.aop.aspect.MyAspect;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.Transactional;

@SpringBootApplication
public class Springboot002Application {

    //定义切面
    @Bean(name = "myAspect")
    public MyAspect initMyAspect(){
        return new MyAspect();
    }

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

}

启动项目后,在浏览器输入:http://localhost:8989/print?id=3&name='zhangsan'&description='happy'
日志结果如下:

2019-05-29 14:17:36 INFO  (MyAspect.java:25)- before...
2019-05-29 14:17:36 INFO  (UserServiceImpl.java:26)- 打印输出用户信息:User{id=3, name=''zhangsan'', description=''happy''}
2019-05-29 14:17:36 INFO  (MyAspect.java:30)- after...
2019-05-29 14:17:36 INFO  (MyAspect.java:35)- afterReturning...

这次测试没有环绕通知(around),待续…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值