这篇文章主要介绍“Java面向切面编程aop怎么实现”,在日常操作中,相信很多人在Java面向切面编程AOP怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Java面向切面编程AOP怎么实现”的疑惑有所
这篇文章主要介绍“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>
拦截规则的注解
点击(此处)折叠或打开
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
String name();
}
注解的被拦截类
点击(此处)折叠或打开
@Service
public class DemoAnnotationService {
@Action(name = "注解式拦截的add操作")
public void add() {
System.out.println("======DemoAnnotationService方法add()=========");
}
}
方法规则被拦截类
点击(此处)折叠或打开
@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());
}
}
配置类
点击(此处)折叠或打开
@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怎么实现”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!
--结束END--
本文标题: Java面向切面编程AOP怎么实现
本文链接: https://lsjlt.com/news/238213.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0