天易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方法后,打印信息如下:
阅读全文
1 0
- 天易47----Springmvc注解方式实现aop配置简单示例
- 注解方式简单的实现AOP
- Spring AOP示例(注解方式)
- SpringMVC自定义注解的方式AOP实现 日志管理
- SpringMVC使用注解配置AOP
- SpringMvc简单注解配置
- Spring 注解方式配置aop:
- 注解方式配置spring-aop
- 注解方式实现AOP编程
- Spring-注解方式实现AOP
- 注解方式实现spring-aop
- spring 注解方式实现aop
- Spring 注解方式实现AOP
- 使用注解方式实现aop
- AspectJ注解方式实现AOP
- Atomikos+spring AOP方式实现JTA事务配置的示例
- 05 Spring Aop实例(AOP 如此简单)@Aspect、@Around 注解方式配置
- aop简单注解实现例程
- filter过滤器,处理登录session长时失效问题
- 图像识别之卷积讲解
- RecyclerView的简单使用
- HDU1205 吃糖果
- 小米推送常见问题
- 天易47----Springmvc注解方式实现aop配置简单示例
- mysql 向表中插入随机数
- 多文件及单文件上传
- HDU6053 TrickGCD【数学】
- 在eclipse中使用maven创建springMVC项目
- software list
- 单例模式成型版
- Python 3.x 学习:Python 基础代码(二)
- 列表类控件