一、开发基础,首先这个demo是在springboot web基础框架上建立的,搭建请参照SpringBoot2.X之旅,开篇 hello world(Web Project),在这个基础上引入lombok包(idea需要另外安装lombok插件),通知引入aop的包,如果之前引用过jpa等包,也可先查看一下包结构,可以不用再引入,pom.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cobra</groupId>
<artifactId>webdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>webdemo</name>
<description>Demo project for Spring Boot</description>
<!--默认是jar包,编译成war包需要重新定义-->
<!--<packaging>war</packaging>-->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--@Slf4j使用-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--添加aop包,如果已经引入spring-boot-starter-data-jpa则已经包含,无需再次引入
在一个大的项目中,可以先查看pom中是否包含,当然包含了也可引入,不冲突
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
项目包结构如下:
二、Coding:
1、新建MyAspect类,加注解@Aspect声明切面类,@Component注入容器,同时用@Pointcut、@Before、@After、@AfterReturning定义切面点和插入相应的业务代码:
package com.cobra.webdemo.aspect;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* @Author: Baron
* @Description: 切面定义
* @Date: Created in 2019/3/7 22:38
*/
@Aspect
@Component
@Slf4j
public class MyAspect {
/** 这里可以使用“*”来对类名、部分类名、方法进行通配,用“..”对参数进行通配 */
@Pointcut("execution(public * com.cobra.webdemo.service.HelloService.doAspect(..))")
public void cut(){
}
/**
* 可以执行切面点之前的操作
*/
@Before("cut()")
public void doBefore(){
log.info("before cut, do something");
}
/**
* 切面点之后的操作
*/
@After("cut()")
public void doAfter() {
log.info("before cut, do something");
}
/**
* 可以获取返回值,后做操作
* @param object
*/
@AfterReturning(returning = "object",pointcut = "cut()")
public void doAfterReturning(Object object){
log.info("object={}",object);
}
}
2、HelloService、HelloServiceImpl的定义:
package com.cobra.webdemo.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* @Author: Baron
* @Description:
* @Date: Created in 2019/3/7 22:39
*/
public interface HelloService {
/**
* 配置切面
* @return
*/
public String doAspect();
/**
* 不配置切面
* @return
*/
public String doNotAspect();
}
package com.cobra.webdemo.service.impl;
import com.cobra.webdemo.service.HelloService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* @Author: Baron
* @Description:
* @Date: Created in 2019/3/7 22:42
*/
@Service
@Slf4j
public class HelloServiceImpl implements HelloService {
/**
* 配置切面
*
* @return
*/
@Override
public String doAspect() {
log.info("被切一刀的方法被访问了!!!!");
return "这个方法被切了一刀!";
}
/**
* 不配置切面
*
* @return
*/
@Override
public String doNotAspect() {
log.info("没有被切的方法被访问!!!!");
return "这个方法没有被切...";
}
}
3、HelloWorld的定义:
package com.cobra.webdemo.controller;
import com.cobra.webdemo.service.HelloService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @Author: Baron
* @Description:
* @Date: Created in 2019/3/4 18:40
*/
@RestController
@RequestMapping("/hello")
@Slf4j
public class HelloWorld {
@Autowired
private HelloService helloService;
/**
* hello world demo
* @param word
* @return
*/
@RequestMapping(value = "/say",method = RequestMethod.GET)
public String say(@RequestParam("word") String word) {
log.info("visit say(@RequestParam(\"word\") String word)...............");
return helloService.doAspect();
}
/**
* hello world demo
* @return
*/
@RequestMapping(value = "/listen",method = RequestMethod.GET)
public String listen() {
log.info("visit listen()...............");
return helloService.doNotAspect();
}
}
4、启动,测试:
扫描二维码关注公众号,回复:
5640593 查看本文章
![](/qrcode.jpg)
三、小记:编码过程中遇到发现@Service需要加在实现类上,记录一下,以免以后会犯傻