spring拦截器的一个简单实例_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > spring拦截器的一个简单实例

spring拦截器的一个简单实例

 2014/10/14 21:31:15  bijian1013  程序员俱乐部  我要评论(0)
  • 摘要:Purview接口packageaop;publicinterfacePurview{voidcheckLogin();}Purview接口的实现类PurviesImpl.javapackageaop;publicclassPurviewImplimplementsPurview{publicvoidcheckLogin(){System.out.println("ThisischeckLoginmethod!");}}拦截器类PurviewAdvice.javapackageaop
  • 标签:一个 Spring 实例

Purview接口

class="java">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!

文章来源:http://blog.csdn.net/yirentianran/article/details/3380529
  • SpringInterceptor.rar (6.3 MB)
  • 下载次数: 0
上一篇: Think in java 读书笔记(一) 下一篇: 没有下一篇了!
发表评论
用户名: 匿名