Spring面向切面编程——Spring实现AOP方式——通过Spring API实现
来源:互联网 发布:虚拟固话软件 编辑:程序博客网 时间:2024/06/07 04:45
Log.java:
package log;import org.springframework.aop.MethodBeforeAdvice;import java.lang.reflect.Method;/** * Description: 使用spring实现aop---使用spring API来实现 前置通知 */public class Log implements MethodBeforeAdvice { //注意实现的这个接口,如果这个方法报错,那么导入aopalliance.jar和aspectweaver.jar两个jar包 /** * * @param method 被调用方法的对象,切入点的方法对象 * @param args 被调用的方法的参数 * @param target 被调用法方法的目标对象 * @throws Throwable */ public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println(target.getClass().getName()+"的"+method.getName()+"方法被执行了"); }}
AfterLog.class:
package log;import org.springframework.aop.AfterReturningAdvice;import java.lang.reflect.Method;/** * Description:后置通知 */public class AfterLog implements AfterReturningAdvice { //注意实现的这个接口,如果这个方法报错,那么导入aopalliance.jar和aspectweaver.jar两个jar包 /** * 目标方法执行后执行的通知 * * @param returnValue 返回值 * @param method 被调用的方法 * @param args 被调用方法对象的参数 * @param target 被调用方法对象的目标对象 * @throws Throwable */ public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.print(target.getClass().getName() + "的" + method.getName() + "被成功执行,返回值是:" + returnValue); }}
UserService.class:
package service;public interface UserService { public void add(); public void delete(); public void search(); public void update();}
UserServiceImp.class:
package service;public class UserServiceImp implements UserService { public void add() { System.out.println("增加用户"); } public void delete() { System.out.println("删除用户"); } public void search() { System.out.println("查找用户"); } public void update() { System.out.println("修改用户"); }}
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userService" class="service.UserServiceImp"/> <bean id="log" class="log.Log"/> <bean id="afterLog" class="log.AfterLog"/> <aop:config> <!--切入点 *:表示所有返回值--> <!--()代表无参 (..)代表所有参数--> <aop:pointcut id="pointcut" expression="execution(* service.UserServiceImp.*(..))"/> <aop:advisor advice-ref="log" pointcut-ref="pointcut"/> <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/> </aop:config></beans>
Test.java:
package test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import service.UserService;/** * Description: */public class Test { public static void main(String args[]){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService) ac.getBean("userService"); userService.add(); }}
测试结果:
service.UserServiceImp的add方法被执行了增加用户service.UserServiceImp的add被成功执行,返回值是:nullProcess finished with exit code 0
可以加我微信一起学习:
阅读全文
0 0
- Spring面向切面编程——Spring实现AOP方式——通过Spring API实现
- Spring面向切面编程——Spring实现AOP方式——通过注解实现
- Spring面向切面编程——Spring实现AOP方式——自定义类实现
- (九)Spring详解——AOP面向切面编程实现
- Spring——使用Spring的注解方式实现面向切面技术AOP
- Spring学习笔记 —— AOP(面向切面编程) 之使用ProxyFactoryBean实现AOP
- spring AOP面向切面编程的四种实现方式
- Spring之AOP实现面向切面编程
- Spring 通过注解方式实现AOP切面编程
- Spring AOP实现:面向切面编程——动态代理原理
- Spring——aop(面向切面编程)
- Spring AOP——面向切面编程(上)
- Spring AOP——面向切面编程(下)
- Spring框架——AOP面向切面编程
- Spring——AOP(面向切面编程)@AspectJ注解方式
- Spring面向切面编程——什么是面向切面编程
- 黑马程序员--Spring Aop 面向切面编程,实现前置通知
- Spring之AOP面向切面编程实现(一)
- 基于Angular+express的定时爬虫信息综合application
- [DevExpress使用随笔]之RibbonReportDesigner控件
- HDFS SHELL命令大全
- 【SqlMap】SQL注入之初体验
- React中调用百度地图
- Spring面向切面编程——Spring实现AOP方式——通过Spring API实现
- (转)Windows平台使用Gitblit搭建Git服务器图文教程
- 适配器模式 Adapter
- 第五章 css选择器 背景
- codevs树状数组基础练习
- HTML(九) div和span
- const 常量与Readonly的区别
- 设计模式-单例模式
- 【TOMsInsight】互联网式平等:体制化游戏规则