首页 > 其他分享 >aop操作-环绕通知获取数据的案例

aop操作-环绕通知获取数据的案例

时间:2023-06-04 16:44:40浏览次数:39  
标签:String args class 获取数据 aop 环绕 password ResourcesService public

添加ResourcesService,ResourcesServiceImpl,ResourcesDao和ResourcesDaoImpl类

public interface ResourcesDao {
    boolean readResources(String url, String password);
}
@Repository
public class ResourcesDaoImpl implements ResourcesDao {
    public boolean readResources(String url, String password) {
        //模拟校验
        return password.equals("root");
    }
}
public interface ResourcesService {
    public boolean openURL(String url ,String password);
}
@Service
public class ResourcesServiceImpl implements ResourcesService {
    @Autowired
    private ResourcesDao resourcesDao;

    public boolean openURL(String url, String password) {
        return resourcesDao.readResources(url,password);
    }
}

创建Spring的配置类

@Configuration
@ComponentScan("com.itheima")
@EnableAspectJAutoProxy
public class SpringConfig {
}

编写通知类并添加环绕通知

@Component
@Aspect
public class MyAdvice {
    @Pointcut("execution(* com.itheima.service.ResourcesService.openURL(String, String))")
    private void servicePt(){}

    @Around("servicePt()")
    public Object trimStr(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();
        System.out.println("去除空格前参数信息:" + Arrays.toString(args));
        // 去除参数两端的多余空格
        for(int i = 0; i < args.length; i++){
            // 判断是否为字符串类型
            if(args[i].getClass().equals(String.class)){
                args[i] = args[i].toString().trim();
            }
        }
        System.out.println("去除空格后参数信息:" + Arrays.toString(args));

        // 一定要方改为后的数组args,如果不把args传入的话,运行原始方法使用的还是原来的参数
        Object ret = pjp.proceed(args);
        return ret;
    }
}

运行程序

public class App {
    public static void main(String[] args) {
        ApplicationContext act = new AnnotationConfigApplicationContext(SpringConfig.class);
        ResourcesService resourcesService = act.getBean(ResourcesService.class);
        boolean flag = resourcesService.openURL("http://pan.baidu.com/haha", " root ");
        System.out.println(flag);
    }
}

 


 

标签:String,args,class,获取数据,aop,环绕,password,ResourcesService,public
From: https://www.cnblogs.com/ixtao/p/17455879.html

相关文章

  • springAOP
    一,AOP1,面向切面编程AspectOrientedProgramming2,编程思想的发展路程①Logicjava:java逻辑编程②OOP:面向对象编程③OIP:interface面向接口编程④面向配置文件编程以上的思想,都是逐步升级的概念⑤AOP在OOP的基础上,增强了OOP的功能3,实现方式①基于配置(xml......
  • Spring的AOP复习
     连接点:所有业务方法切入点:被挖掉共性功能的业务方法 通知:共性功能构成的方法通知类型:前面还是后面  切面:描述切入点和通知的关系 目标对象 织入:将共性功能放回去 代理引入......
  • Spring AOP 使用介绍,从前世到今生
    SpringAOP使用介绍,从前世到今生 https://www.javadoop.com/post/spring-aop-intro @Before("execution(*com.javadoop.dao.*.*(..))")Tips:上面匹配中,通常"."代表一个包名,".."代表包及其子包,方法参数任意匹配使用两个点".."。......
  • Java实战-基于JDK的LRU算法实现、优雅的实现代码耗时统计(Spring AOP、AutoCloseable
    场景Java中基于JDK的LRU算法实现LRU算法-缓存淘汰算法-Leastrecentlyused,最近最少使用算法根据数据的历史访问记录来进行淘汰数据,其核心思想是:如果有数据最近被访问过,那么将来被访问的几率也更高在Java中可以利用LinkedHashMap容器简单实现LRU算法LinkedHashMap底层就是用......
  • Spring AOP错误:org.springframework.beans.factory.BeanNotOfRequiredTypeException:
    org.springframework.beans.factory.BeanNotOfRequiredTypeException:Beannamed'myCalculator'isexpectedtobeoftype'com.mashibing.service.MyCalculator'butwasactuallyoftype'com.sun.proxy.$Proxy19'atorg.springframew......
  • 从 C# chart 中获取数据-2
    新增功能:Excel_EA1.将归档数据从WINCC的归档目录中拷贝出来,改名存储  2.使用WinCCArchiveConnector建立WINCC数据库的连接  2.1添加  2.2连接 改名后,连接是空的,名字必须默认:  原来的数据库文件拷贝过来后,显示文件,连接正常。 2.3添加不同的......
  • 从 C# chart 中获取数据-1
    已经可以从WINCC的归档数据库获取数据,并且以图形的方式显示出来了。现在要实现一个功能,就是建一个相当于WINCC历史趋势的标尺,移动后,下面显示数据,点击按钮,记录这组数据。获取的数据,可以进行后面的处理,比如作为神经网络的输入数据。下面对各个例子记录实现过程。 1.在excel中......
  • AopContext.currentProxy();
    获取代理对象的方法:AopContext.currentProxy();在同一个类中,非事务方法A()调用事务方法B(),事务失效,得采用((xxxObj)AopContext.currentProxy()).B()来进行调用,事务才能生效。B方法被A调用,对B方法的切入失效,但加上AopContext.currentProxy()创建了代理类,在代理类中调用该方......
  • 基于注解的Spring AOP的配置和使用
    AOP是OOP的延续,是AspectOrientedProgramming的缩写,意思是面向切面编程。可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术。AOP实际是GoF设计模式的延续,设计模式孜孜不倦追求的是调用者和被调用者之间的解耦,AOP可以说也是这种目标......
  • spring aop
    <?xmlversion="1.0"encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.or......