前提:
安装jdk1.8,maven,idea。
1、使用idea创建一个maven项目。
2、在pom文件中添加依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <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>
spring-boot-starter-web是创建一个web项目必须的依赖。
spring-boot-devtools是实现热部署必须添加的依赖。
spring-boot-starter-aop是使用Spring AOP功能必须添加的依赖。
使用idea时的热部署可能会遇到问题。参考:
3、书写代码。
3.1、启动类
package cn.ancony.springboot03ex01; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
注解@SpringBootApplication表示这是一个SpingBoot应用程序。
3.2、Controller类
package cn.ancony.springboot03ex01.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloController { @RequestMapping("/sayHello.html") public @ResponseBody String say() { return "hello,spring boot"; } @RequestMapping("/say.html") public @ResponseBody String say(String name) { return "hello, " + name; } }
注解@Controller表示这是一个Controller类,对应http请求的处理类。@RequestMapping表示请求/sayHello.html时将使用String say()
方法作为处理的方法。请求/say.html时将使用方法String say(String name)作为处理的方法。方法的参数在请求的时候通过在"html"的后面加上"?name=xxx"传入。
此时,如果启动应用程序,使用curl并访问http://192.168.128.128:8080/sayHello.html时,控制台显示:
[ancony@bogon ~]$ curl 172.20.10.2:8080/sayHello.html
hello,spring boot
使用curl并访问http://192.168.128.128:8080/say.html?name=ancony时,控制台显示:
[ancony@bogon ~]$ curl 172.20.10.2:8080/sayHello.html
hello,ancony
而Sping程序是没有任何反应的。它只是将处理的结果直接返回给了客户端。
3.3、Config类
package cn.ancony.springboot03ex01.conf; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.context.annotation.Configuration; import java.util.Arrays; @Configuration @Aspect public class AOPConfig { @Around("@within(org.springframework.stereotype.Controller)") public Object functionAccessCheck(final ProceedingJoinPoint pjp) throws Throwable { Object[] args = pjp.getArgs(); System.out.println("args: " + Arrays.asList(args)); Object o = pjp.proceed(); System.out.println("return: " + o); return o; } }
注解@Configuration表示这是一个配置类。注解@Aspect表示这是一个切面。注解@Around声明了一个表达式,描述了要织入目标的特性。@within表示目标类型带有注解。其注解的类型参数为org.springframework.stereotype.Controller,表示Spring Controller方法被调用的时候,都会执行@Around注解的方法。
Object functionAccessCheck(final ProceedingJoinPoint pjp)是用来织入的代码。其参数的名称为ProceedingJoinPoint pjp,方法pjp.proceed()表示执行原来的应用代码。proceed方法可能会抛出异常。
这时,通过curl访问172.20.10.2:8080/say.html?name=ancony,控制台显示:
[ancony@bogon ~]$ curl 172.20.10.2:8080/say.html?name=ancony
hello, ancony
Spring应用程序显示:
args: [ancony]
return: hello, ancony
表示织入的代码被成功地执行了。