SringAop切面
SpringBoot实现的切面
创建SpringBoot应用
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
package com.example.demo.controller;
import com.example.demo.service.service.IAop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by 隔壁老王 on 2019/8/8
*/
@RequestMapping("/aop")
@RestController
public class AopController {
@Autowired
IAop aop;
@RequestMapping("/testaop")
public void testAop() {
aop.testAop();
}
}
package com.example.demo.service.service;
/**
* Created by 隔壁老王 on 2019/8/8
*/
public interface IAop {
public void testAop();
}
package com.example.demo.service.ipml;
import com.example.demo.config.LoggerMessage;
import com.example.demo.service.service.IAop;
import org.springframework.stereotype.Service;
/**
*Created by 隔壁老王 on 2019/8/8
*/
@Service("aop")
public class Aop implements IAop {
@Override
@LoggerMessage("111111")
public void testAop() {
System.out.println("执行目标方法");
}
}
package com.example.demo.config;
import java.lang.annotation.*;
/**
* Created by 隔壁老王 on 2019/8/8
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LoggerMessage {
String value();
}
package com.example.demo.config;
import com.example.demo.service.ipml.Aop;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Service;
/**
* Created by 隔壁老王 on 2019/8/8
*/
@Aspect
@Service
public class SomeAspect {
/**
* 在chapter4.practice1.Performance类的perform方法调用前执行
*/
@Before("execution(public * com.example.demo.service.ipml.Aop.testAop()))")
public void drink() throws Exception {
AnnonTestB annonTestB = new AnnonTestB();
Boolean testAop = annonTestB.methodB("testAop");
if (testAop) {
System.out.println("before: 执行通知方法.");
}
}
}
package com.example.demo.config;
import com.example.demo.service.ipml.Aop;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
/**
* Created by 隔壁老王 on 2019/8/8
*/
public class AnnonTestB {
public Boolean methodB(String methodName, Class<?>... parameterTypes) throws Exception {
Boolean flag = false;
Aop annonTestA = new Aop();
// 获取AnnotationTest2的Class实例
Class<Aop> c = Aop.class;
// 获取需要处理的方法Method实例
Method method = c.getMethod(methodName, parameterTypes);
Method[] methods = c.getMethods();
// 判断该方法是否包含MyAnnotation注解
if (method.isAnnotationPresent(LoggerMessage.class)) {
// 获取该方法的MyAnnotation注解实例
LoggerMessage loggerMessage = method
.getAnnotation(LoggerMessage.class);
// 执行该方法
// method.invoke(annonTestA, "12345");
// 获取myAnnotation
String value1 = loggerMessage.value();
if (value1 != null) {
System.out.println(value1);
flag = true;
}
}
return flag;
}
}
application.properties配置文根
server.servlet.context-path=/spring
启动应用 调用接口
http://127.0.0.1:8080/spring/aop/testaop
控制台输出:
此时切面是有效的,当项目为spring的时候需要在application-context.xml中添加以下配置才会生效
<aop:aspectj-autoproxy />
spring还要添加切面所需的maven依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>