spring拦截器的一个简单实例
编程技术  /  houtizong 发布于 3年前   79
Purview接口
package aop;public interface Purview { void checkLogin();}
Purview接口的实现类PurviesImpl.java
package aop;public class PurviewImpl implements Purview { public void checkLogin() { System.out.println("This is checkLogin method!"); }}
拦截器类PurviewAdvice.java
package aop;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;public class PurviewAdvice implements MethodBeforeAdvice { public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { System.out.println("This is before method!"); }}
测试类Test.java
package aop;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {// TODO 自动生成方法存根ApplicationContext ctx = new ClassPathXmlApplicationContext("aop/applicationContext.xml");Purview purview = (Purview) ctx.getBean("purviewImpl");purview.checkLogin();}}
配置文件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-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"default-autowire="autodetect"><bean id="purviewImpl" class="aop.PurviewImpl"></bean><bean id="purviewAdvice" class="aop.PurviewAdvice"></bean><bean id="purviewAdvisor"class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"><property name="advice"><ref local="purviewAdvice" /></property><property name="patterns"><list><value>.*checkLogin.*</value></list></property></bean><bean id="autoproxyaop"class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"><property name="beanNames"><value>purviewImpl</value></property><property name="interceptorNames"><list><value>purviewAdvisor</value></list></property></bean></beans>
运行结果:
This is before method!This is checkLogin method!
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接