天易47----Springmvc注解方式实现aop配置简单示例

来源:互联网 发布:2017格里芬体测数据 编辑:程序博客网 时间:2024/05/29 10:11

一:目录结构截图


二:Springmvc所在的配置文件,我这里在web.xml中配置的是在application-servlet.xml文件中,所以aop也相对放在该文件下(Springmvc的配置在这里就不多说了)

1.截图如下:


2.由于我这里使用了注解方式所以在application-servlet.xml配置文件中添加如下代码是aop注解生效:

<!-- aop注解方式生效 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>

3.在application-servlet.xml配置文件中使用aop标签还需要在该文件的文件头上添加如下信息:

xmlns:aop="http://www.springframework.org/schema/aop"

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

截图如下:


三:java代码片段

1.aop配置类

package com.jy.app.controller.mobile.aop;import java.lang.reflect.Method;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.AfterReturning;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.core.annotation.Order;import org.springframework.stereotype.Component;//声明这是一个切面Bean@Aspect//设置切面级别,数字越小,级别越高@Order(1)//声明这是一个组件@Componentpublic class AopHandler {//配置切入点,该方法无方法体,主要为方便同类中其他方法使用此处配置的切入点@Pointcut("execution(* com.jy.app.controller.mobile.aop.service.*.*(..))")//对com.jy.app.controller.mobile.aop.service包下的所有类起作用public void poicut(){};/** * 配置前置通知,使用在方法poicut()上注册的切入点 * 同时接受JoinPoint切入点对象,可以没有该参数 */@Before("poicut()")public void excuteBefore(JoinPoint joinPoint){System.out.println("aop执行之前==="+joinPoint);MethodSignature ms=(MethodSignature) joinPoint.getSignature();//获取连接点的方法签名对象        Method method=ms.getMethod();//获取切面拦截的方法体        System.out.println("方法:"+method.getName());//获取切面拦截的方法名称        Object[] args = joinPoint.getArgs();//返回目标方法的参数列表        User user=(User) args[0];//获取目标方法的参数列表中的第一个参数        System.out.println(args+"======="+args[0]+"==username=="+user.getName()+"===age==="+user.getAge()+"===="+args[1]);}/** * 配置后置返回通知,使用在方法poicut()上注册的切入点 * @param joinPoint */@AfterReturning("poicut()")public void excuteAfter(JoinPoint joinPoint){System.out.println("aop执行之后==="+joinPoint);MethodSignature ms=(MethodSignature) joinPoint.getSignature();        Method method=ms.getMethod();        System.out.println("方法:"+method.getName());        Object[] args=joinPoint.getArgs();        User user=(User) args[0];        System.out.println(args[0]+"==username=="+user.getName()+"===age==="+user.getAge()+"===="+args[1]);}}


2.访问的controller类

package com.jy.app.controller.mobile.aop;import java.util.HashMap;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import com.jy.app.controller.mobile.aop.service.AopService;import com.jy.app.controller.mobile.aop.service.ExcuteService;@Controller@RequestMapping("/aop")public class AopConfig {@Autowiredprivate AopService aopService;@Autowiredprivate ExcuteService excuteService;@RequestMapping(value="/aopAdvice",method = RequestMethod.GET)@ResponseBodypublic Map<String,Object> aopAdvice(HttpServletRequest request,HttpServletResponse response,User user,String name){Map<String,Object> map=new HashMap<String,Object>();map.put("name", name);System.out.println("====name===="+name+"===="+request.getParameter("name"));aopService.excuteAopService(user,name);excuteService.excuteAopServiceTest(user, name);return map;}}

3.User实体类

package com.jy.app.controller.mobile.aop;public class User {private String name;private String age;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}}

4.service以及实现类

1)AopService接口

package com.jy.app.controller.mobile.aop.service;import com.jy.app.controller.mobile.aop.User;
public interface AopService {public void excuteAopService(User user,String name);}

2)AopService接口实现类

package com.jy.app.controller.mobile.aop.service;import org.springframework.stereotype.Service;import com.jy.app.controller.mobile.aop.User;@Servicepublic class AopServiceImpl implements AopService {@Overridepublic void excuteAopService(User user,String name) {// TODO Auto-generated method stubSystem.out.println("执行aop Service方法");}}

3)ExcuteService接口

package com.jy.app.controller.mobile.aop.service;import com.jy.app.controller.mobile.aop.User;public interface ExcuteService {public void excuteAopServiceTest(User user,String name);}
4)ExcuteService接口实现类
package com.jy.app.controller.mobile.aop.service;import org.springframework.stereotype.Service;import com.jy.app.controller.mobile.aop.User;@Servicepublic class ExcuteServiceImpl implements ExcuteService {@Overridepublic void excuteAopServiceTest(User user,String name) {// TODO Auto-generated method stubSystem.out.println("执行excuteAopService方法");}}

5.输入地址:http://localhost:8082/StanClaimProd-app/aop/aopAdvice?name=hy&age=28访问controller方法后,打印信息如下:





原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 农商银行app登录密码忘了怎么办 网址上的重庆时时彩不能提现怎么办 天天中彩票自己账户登录不了怎么办 天猫上卖王者荣耀的兑换码是怎么办 身份证借别人开淘宝永久封店怎么办 天猫店铺被扣12分怎么办 天猫法人变更之前的天猫贷款怎么办 在日本旅游遇到精日导游怎么办 银行资金交易异常卡被冻结怎么办 如果淘宝被盗了店铺乱上东西怎么办 快递不送货直接代售点签收怎么办 淘宝快递没收到却显示已签收怎么办 支付宝余额未满16受限怎么办 未满16岁支付宝余额受限怎么办 手机天猫购物买的数量大怎么办 扣扣游戏领礼包出现账号异常怎么办 美容院转让给别人客人要退钱怎么办 卖家毁约中介费担保费怎么办 天猫买东西店家不开增值税票怎么办 天猫专卖店品牌不授权了怎么办 临时京东账号被冻结买的东西怎么办 天猫强行退款给买家商家怎么办 淘宝店铺没交保证金被释放了怎么办 沭阳县地段生过了报名时间怎么办 淘宝买东西地址和收件人填错怎么办 才装修的房子马上要住怎么办 淘宝发货显示无效的发货人怎么办 微信位置和所在地位置不一样怎么办 qq号被冻结了短信发不出去怎么办 商户刷自己信息卡被冻结怎么办 天猫超市买的东西坏了怎么办 天猫超市买东西地址填错了怎么办 天猫超市地址填错了怎么办 天猫超市退货达不到包邮条件怎么办 保千里达令手机锁屏密码忘记怎么办 淘宝买的东西给的发票不见了怎么办 在天猫超市买到发霉怎么办 支付宝红包券金额消费不完怎么办 支付宝向对方发起收款不付怎么办 支付宝收款别人少付了怎么办 红牛领到50元加油优惠券怎么办用