AOP切点切面内容
一、实现接口MethodBeforeAdvice该拦截器会在调用方法前执行
实现接口 AfterReturningAdvice该拦截器会在调用方法后执行
实现接口 MethodInterceptor该拦截器会在调用方法前后都执行,实现环绕结果。
- package com.ly.model;
- import java.lang.reflect.Method;
- import org.aopalliance.intercept.MethodInterceptor;
- import org.aopalliance.intercept.MethodInvocation;
- import org.springframework.aop.AfterReturningAdvice;
- import org.springframework.aop.MethodBeforeAdvice;
- public class Advice implements MethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor{
- @Override
- public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
- saveBeforeMessage();
- }
- @Override
- public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
- saveAfterMessage();
- }
- public void saveBeforeMessage(){
- System.out.println("调用BeforeAdvice成功");
- }
- public void saveAfterMessage(){
- System.out.println("调用AfterAdvice成功");
- }
- @Override
- public Object invoke(MethodInvocation arg0) throws Throwable {
- System.out.println("调用RoundService之前成功");
- Object result=arg0.proceed();
- System.out.println("调用RoundService之后成功");
- return result;
- }
- }
以下为AOPservice的实现
- package com.ly.service.impl;
- import com.ly.service.AOPService;
- public class AOPServiceImpl implements AOPService{
- @Override
- public void print(String message) {
- System.out.println(message);
- }
- @Override
- public void save() {
- System.out.println("保存信息成功");
- }
- }
以下为配置文件信息
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd">
- <!--目标对象 -->
- <bean id="AOPservice" class="com.ly.service.impl.AOPServiceImpl">
- </bean>
- <!-- advice通知 -->
- <bean id="adviceMessage" class="com.ly.model.Advice"></bean>
- <!-- 切入点adviser -->
- <bean id="adviser" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
- <property name="advice" ref="adviceMessage"></property>
- <!-- pattern的值使用正则表达式精确指定切入点 ,将print方法设为切入点 -->
- <property name="pattern"
- value="com\.ly\.service\.impl\.AOPServiceImpl\.print"></property>
- </bean>
- <!-- 代理对象 返回实例是目标对象 target属性指定的AOPservice对象-->
- <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
- <!-- 设置目标(target)bean为 AOPservice,
- 多个bean可以设置list集合如
- <property name="interceptorNames">
- <list>
- <value>advisor</value>
- <value>advisor1</value>
- </list>
- </property>-->
- <property name="target">
- <ref bean="AOPservice" />
- </property>
- <!--源码内固定的属性private String[] interceptorNames; -->
- <property name="interceptorNames">
- <value>adviser</value>
- </property>
- </bean>
- </beans>
以下为测试类
- package com.ly.control;
- import java.io.IOException;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.core.io.ClassPathResource;
- import org.springframework.core.io.FileSystemResource;
- import org.springframework.core.io.Resource;
- import org.springframework.core.io.UrlResource;
- import com.ly.service.AOPService;
- public class SpringTest {
- public static void main(String[] args) throws IOException {
- ApplicationContext applicationContext=new ClassPathXmlApplicationContext(new String[]{"springBeans.xml"});
- AOPService aopservice= (AOPService) applicationContext.getBean("proxyService");
- //由于在springBeans.xml中只配置了 value="com\.ly\.service\.impl\.AOPServiceImpl\.print"适配print方法,没有适配save方法,故只有调用print方法时才会执行advice通知
- aopservice.print("调用print成功==========》");
- aopservice.save();
- }
- }
二、在web应用中调用时需要用WebApplicationContext这个来得到Spring配置中的bean类
- public class UserAction extends ActionSupport {
- private static final Logger log = Logger.getLogger(UserAction.class);
- private UserService userService;
- public UserService getUserService(){
- if(userService == null){
- WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getRequest().getServletContext());
- userService = (UserService) applicationContext.getBean("proxyService");
- // userService = new UserServiceImpl();
- }
- return userService;
- }
AOP切点切面内容的更多相关文章
- 【原创】Android AOP面向切面编程AspectJ
一.背景: 在项目开发中,对 App 客户端重构后,发现用于统计用户行为的友盟统计代码和用户行为日志记录代码分散在各业务模块中,比如在视频模块,要想实现对用户对监控点的实时预览和远程回放行为进行统计, ...
- 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~
简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...
- AOP 日志切面
AOP把软件的功能模块分为两个部分:核心关注点和横切关注点.业务处理的主要功能为核心关注点,而非核心.需要拓展的功能为横切关注点.AOP的作用在于分离系统中的各种关注点,将核心关注点和横切关注点进行分 ...
- AOP面向切面编程的四种实现
一.AOP(面向切面编程)的四种实现分别为最原始的经典AOP.代理工厂bean(ProxyFacteryBean)和默认自动代理DefaultAdvisorAutoProxyCreator以及Bea ...
- 如何通过自定义注解实现AOP切点定义
面向切面编程(Aspect Oriented Programming, AOP)是面向对象编程(Object Oriented Programming,OOP)的强大补充,通过横切面注入的方式引入其他 ...
- Spring AOP 创建切面
增强被织入到目标类的所有方法中,但是如果需要有选择性的织入到目标类某些特定的方法中时,就需要使用切点进行目标连接点的定位.增强提供了连接点方位信息:如织入到方法前面.后面等,而切点进一步描述织 ...
- 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之十 || AOP面向切面编程浅解析:简单日志记录 + 服务切面缓存
代码已上传Github+Gitee,文末有地址 上回<从壹开始前后端分离[ .NET Core2.0 Api + Vue 2.0 + AOP + 分布式]框架之九 || 依赖注入IoC学习 + ...
- Spring:AOP面向切面编程
AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果. AOP是软件开发思想阶段性的产物,我们比较熟悉面向过程O ...
- Spring AOP 切点(pointcut)表达式
这遍文章将介绍Spring AOP切点表达式(下称表达式)语言,首先介绍两个面向切面编程中使用到的术语. 连接点(Joint Point):广义上来讲,方法.异常处理块.字段这些程序调用过程中可以抽像 ...
随机推荐
- 如何正确实现 IDisposable 接口
MSDN建议按照下面的模式实现IDisposable接口: public class Foo: IDisposable { public void Dispose() { Dispose(true); ...
- csharp: Request.Form,Request.QueryString,Request.Params,Request.Cookies
/// <summary> /// Request.Form,Request.QueryString,Request.Params /// http://msdn.microsoft.co ...
- AngularJS之控制器
控制器在Angularjs中的作用是增强视图,它实际就是一个函数,用来向视图中的作用域添加额外的功能,我们用它来给作用域对象设置初始状态,并添加自定义行为. 当我们在页面上创建一个控制器时,Angul ...
- CSS 2D转换
转换是使元素改变形状.尺寸和位置的一种效果.通过 CSS3 转换,我们能够对元素进行移动.缩放.转动.拉长或拉伸,可以大致分为2D转换和3D转换.下面介绍的是2D转换的相关知识点. 首先,CSS中2D ...
- easyui grid 增加合计行
一.首先,easyui grid 的 showfooter 属性设置为 true $aplgrid.datagrid({ data: globalExpenseClaimForm.ExpenseCl ...
- DUANG~ 万网轻云服务器,大促狂欢,不仅仅免单!
DUANG~ 万网轻云服务器,大促狂欢,不仅仅免单! 当老板第一次知道我们要做活动的时候,其实是拒绝的.DUANG~ 打折.降价.挥泪甩卖…太俗套.客户看到一定骂我们,根本没有诚意. 所以轻云大促 ...
- 【阿里云产品公测】在ACE上部署WP测试体验
ACE服务其实已经有很多类似的服务提供商了,无论收费的还是免费的, 但是到现在为止还没有体验过,正好借着这次机会,来体验一下阿里云的ACE服务. ' !2NSv /IQ$[WR cx B ...
- Appium 常用方法
锁定 锁定屏幕 # python driver.lock(5) 将 app 置于后台 把当前应用放到后台去 # python driver.background_app(5) 收起键盘 收起键盘 # ...
- 【Leetcode】【Medium】Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- XISE菜刀V13.0 官网版 XISE菜刀VIP破解版 XISE官网
诠释: 1. 破解VIP登陆限制 2.去后门 (自查) 下载地址 :https://pan.baidu.com/s/1eR2rUOM 查毒地址:http://a.virscan.org/a3983f3 ...