这篇文章主要介绍“Java面向切面编程AOP怎么实现”,在日常操作中,相信很多人在Java面向切面编程AOP怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Java面向切面编程AOP怎么实现”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
一:背景
Spring的AOP的存在目的是为了解耦。AOP可以让一组类共享相同的行为。在OOP中只能通过继承类和实现接口,来是代码的耦合度增强,且类继承只能为单继承,阻碍更多行为添加到一组类上,AOP弥补了OPP的不足。
二:概述 Spring支持AspectJ的注解方式切面编程
1.使用@Aspect 声明一个切面。
2.使用@After,@Before,@Around 定义建言(advice),可直接将拦截规则(切点)作为参数。
3.其中@After,@Before,@Around参数的拦截规则为切点(PointCut) ,为了使切点复用,可使用@PointCut 专门定义拦截规则,然后在@After,@Before,@Around的参数中调用。
4.其中符合条件的每一个被拦截处为连接点(JoinPoint)
三:代码实例
1.pom.xml
点击(此处)折叠或打开
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
2.拦截规则的注解
点击(此处)折叠或打开
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
String name();
}
2.注解的被拦截类
点击(此处)折叠或打开
@Service
public class DemoAnnotationService {
@Action(name = "注解式拦截的add操作")
public void add() {
System.out.println("======DemoAnnotationService方法add()=========");
}
}
3.方法规则被拦截类
点击(此处)折叠或打开
@Service
public class DemoMethodService {
public String add() throws Exception{
System.out.println("======DemoMethodService方法add()=========");
int i=100/0;
return "SUCCESS";
}
}
4.编写切面
点击(此处)折叠或打开
@Aspect
@Component
public class LogAspect {
@Pointcut("@annotation(com.gemdale.gmap.spring.boot.demo.Action)")
public void annotationPointCut() {
}
@After("annotationPointCut()")
public void after(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Action action = method.getAnnotation(Action.class);
System.out.println("注解式拦截 " + action.name());
}
@Before("execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))")
public void methodBefore(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("before方法规则式拦截 " + method.getName());
}
@After("execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))")
public void methodAfter(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("after方法规则式拦截 " + method.getName());
}
@AfterReturning(pointcut = "execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))", returning = "result")
public void methodAfterResult(JoinPoint joinPoint, Object result) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("after result方法规则式拦截 " + method.getName() + "result=" + result);
}
@AfterThrowing(pointcut = "execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))", throwing = "e")
public void methodAfterException(JoinPoint joinPoint, Exception e) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("after exception方法规则式拦截 " + method.getName() + " e=" + e.getMessage());
}
}
5.配置类
点击(此处)折叠或打开
@Configuration
@ComponentScan("com.gemdale")
@EnableAspectJAutoProxy
public class AppliactionConfig {
}
6.执行类
点击(此处)折叠或打开
public class Start {
public static void main(String[] args) throws Exception{
AnnotationConfigApplicationContext configApplicationContext = new AnnotationConfigApplicationContext(
AppliactionConfig.class);
// UseFunctionService
// useFunctionService=configApplicationContext.getBean(UseFunctionService.class);
// System.out.println(useFunctionService.sayHello("Gengchong"));
DemoAnnotationService demoAnnotationService = configApplicationContext.getBean(DemoAnnotationService.class);
DemoMethodService demoMethodService = configApplicationContext.getBean(DemoMethodService.class);
demoAnnotationService.add();
demoMethodService.add();
configApplicationContext.close();
}
}
到此,关于“Java面向切面编程AOP怎么实现”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。