Spring?切面(@Pointcut)

2024-05-16 09:32
文章标签 java spring 切面 pointcut

本文主要是介绍Spring?切面(@Pointcut),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录结构
—-这里写图片描述

package OA.Dao;import OA.Proxy.Action;public interface IUserDao<T> {@Action(name="注解式Add操作")public void Add(T T);public void Delete(int id);
}
package OA.Dao;import org.springframework.stereotype.Repository;import OA.Proxy.Action;
import OA.Entity.User;@Repository("mDao")
public class UserDao implements IUserDao<User> {@Override@Action(name="注解式Add操作")public void Add(User T) {System.out.println("数据层:添加动作" + T.getName() + T.getId());}@Overridepublic void Delete(int id) {System.out.println("数据层:删除动作");}}
package OA.Entity;public class User {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
package OA.Proxy;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Action {String name();
}
package OA.Service;public interface IUserService<T> {public void Add(T T);public void Delete(int id);
}
package OA.Service;import javax.annotation.Resource;import org.springframework.stereotype.Service;
import OA.Dao.IUserDao;
import OA.Entity.User;@Service("mService")
public class UserService implements IUserService<User> {private IUserDao<User> IUserDao;public IUserDao<User> getIUserDao() {return IUserDao;}@Resourcepublic void setIUserDao(IUserDao<User> iUserDao) {IUserDao = iUserDao;}@Overridepublic void Add(User T) {System.out.println("service:添加" + T.getName() + T.getId());IUserDao.Add(T);}@Overridepublic void Delete(int id) {System.out.println("service:删除");IUserDao.Delete(id);}}
package OA.Proxy;import java.lang.annotation.Annotation;
import java.lang.reflect.Method;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;import OA.Entity.Logger;@Component("logAspect")
@Aspect
public class LogerAspect {// 第一个* 表示任意返回类型// 任意类// 以add开头的任意方法// ..代表任意参数// /**// * 在程序运行之前运行// */// @Before("execution(* OA.Dao.*.Add*(..))||execution(*// OA.Service.*.Delete*(..))")// public void Before() {// Logger.info("前:日志");// }// /**// * 在程序运行之后运行// */// @After("execution(* OA.Service.*.Delete*(..))")// public void After() {// Logger.info("后:日志");// }// @Around("execution( * OA.Dao.*.Add*(..))")@Pointcut("@annotation(OA.Proxy.Action)")public void annotationPointcut() {}@Before("annotationPointcut()")public void Before(JoinPoint JoinPoint) {System.out.println("运行");MethodSignature methodSignature = (MethodSignature) JoinPoint.getSignature();Method method = methodSignature.getMethod();Annotation[] totp=method.getDeclaredAnnotations();System.out.println(totp.length);for (Annotation annotation : totp) {System.out.println(annotation);}if (method.isAnnotationPresent(Action.class)) {System.out.println("进入IF");System.out.println(method.getAnnotation(Action.class));}System.out.println("方法名:"+method.getName());}@After("execution( * OA.Dao.*.Add*(..))")public void After() {Logger.info("后:日志");}public void logAround(ProceedingJoinPoint pj) throws Throwable {Logger.info("前:日志");pj.proceed();System.out.println(pj.getTarget());System.out.println(pj.getSignature().getName());Logger.info("后:日志");}
}
package OA.Service;import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import OA.Action.UserAction;
import OA.Dao.UserDao;
import OA.Entity.User;public class Test {public static void main(String[] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();User u = new User();u.setId(1);u.setName("name");/* 读取文件 */BeanFactory factory = new ClassPathXmlApplicationContext("beas.xml");/* 读取mAction Id的节点 */// UserAction UserAction = (UserAction) factory.getBean(UserAction.class, "mAction");UserAction UserAction = (UserAction) factory.getBean(UserAction.class);UserAction.setUser(u);UserAction.Add();}
}
/*console*/
action:添加name1
service:添加name1
运行
1
@OA.Proxy.Action(name=注解式Add操作)
进入IF
@OA.Proxy.Action(name=注解式Add操作)
方法名:Add
数据层:添加动作name1
后:日志

这篇关于Spring?切面(@Pointcut)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/994528

相关文章

Java利用docx4j+Freemarker生成word文档

《Java利用docx4j+Freemarker生成word文档》这篇文章主要为大家详细介绍了Java如何利用docx4j+Freemarker生成word文档,文中的示例代码讲解详细,感兴趣的小伙伴... 目录技术方案maven依赖创建模板文件实现代码技术方案Java 1.8 + docx4j + Fr

SpringBoot首笔交易慢问题排查与优化方案

《SpringBoot首笔交易慢问题排查与优化方案》在我们的微服务项目中,遇到这样的问题:应用启动后,第一笔交易响应耗时高达4、5秒,而后续请求均能在毫秒级完成,这不仅触发监控告警,也极大影响了用户体... 目录问题背景排查步骤1. 日志分析2. 性能工具定位优化方案:提前预热各种资源1. Flowable

基于SpringBoot+Mybatis实现Mysql分表

《基于SpringBoot+Mybatis实现Mysql分表》这篇文章主要为大家详细介绍了基于SpringBoot+Mybatis实现Mysql分表的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录基本思路定义注解创建ThreadLocal创建拦截器业务处理基本思路1.根据创建时间字段按年进

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法