`

Java拦截技术快速上手

    博客分类:
  • Java
阅读更多

等有空的时候再加上讲解吧,先贴上代码。

1、拦截器Handle类:

package com.shansun.interceptor.test.handler;

import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.shansun.interceptor.test.core.InterceptorAdvisor;

/**
 * @author lanbo.xj
 */
public class InterceptorHandler extends AbstractAutoProxyCreator implements ApplicationContextAware {
	private static final long serialVersionUID = 8822145972561113575L;

	private static ApplicationContext applicationContext;

	public void setApplicationContext(ApplicationContext aApplicationcontext)
			throws BeansException {
		applicationContext = aApplicationcontext;
	}

	public static Object getBean(String beanName) {
		return applicationContext.getBean(beanName);
	}
	
	@Override
	protected Object[] getAdvicesAndAdvisorsForBean(@SuppressWarnings("rawtypes") Class beanClass, String beanName,
			TargetSource targetSource) throws BeansException {
		if(beanName.equals("springLoader")) return null;
		return new InterceptorAdvisor[] { new InterceptorAdvisor(beanName) };
	}
 
}

 2、拦截器:在每个方法前后打印一条信息

public class InterceptorAdvisor implements Advisor {
	Advice advice = null;
	
	public InterceptorAdvisor(String beanName) {
		advice = new InterceptorRoundAdvice(beanName);
	}
	
	public Advice getAdvice() {
		return advice;
	}

	public boolean isPerInstance() {
		return false;
	}
}

3、方法调用包裹类

public class InterceptorRoundAdvice implements MethodInterceptor, Advice {

	/**
	 * 被拦截的BEAN的名称
	 */
	private String beanName;

	public InterceptorRoundAdvice(String aBeanName) {
		this.beanName = aBeanName;
	}

	public Object invoke(MethodInvocation invocation) throws Throwable {
		String methodName = this.beanName + "."
				+ invocation.getMethod().getName();
		// 获取当前调用方法的参数
		Object[] arguments = invocation.getArguments();
		System.out.println("参数:" + getArguments(arguments));
		System.out.println(methodName + ": Before invocation!");
		Object result = invocation.proceed();
		System.out.println(methodName + ": After invocation!");
		return result;
	}
	
	private String getArguments (Object[] arguments) {
		StringBuilder sb = new StringBuilder();
		for(Object arg : arguments) {
			sb.append("Type:" + arg.getClass().getSimpleName() + "  Value:" + arg.toString());
		}
		return sb.toString();
	}

}

4、工程POM配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>InterceptorTest</groupId>
	<artifactId>InterceptorTest</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring</artifactId>
			<version>2.5.4</version>
		</dependency>
		<dependency>
			<groupId>aopalliance</groupId>
			<artifactId>aopalliance</artifactId>
			<version>1.0</version>
		</dependency>
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>2.1_3</version>
		</dependency>
	</dependencies>
</project>

5、测试代码

public interface BusinessInterface {
	public void doSomething();
}
public class BusinessClass implements BusinessInterface {

	public void doSomething() {
		System.out.println("业务组件BusinessClass方法调用:doSomething()"); 
	}

}
public class SpringLoader implements ApplicationContextAware{
 
	private BusinessInterface businessInterface; 
	private ApplicationContext applicationContext; 
	 
	public void setBusinessInterface(BusinessInterface businessInterface) {
		this.businessInterface = businessInterface;
	}
	
	public void doSth() {
		if(businessInterface == null) {
			this.businessInterface = (BusinessInterface) applicationContext.getBean("businessInterface");
		}
		businessInterface.doSomething();
	}
 
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	} 
}
	public static void main(String[] args) {
		ApplicationContext ctx = new FileSystemXmlApplicationContext(
				"bean.xml ");
		SpringLoader sl = (SpringLoader) ctx.getBean("springLoader");
		sl.doSth();
	}

Spring配置文件:

<?xml version="1.0" encoding="GB2312"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/util
                http://www.springframework.org/schema/util/spring-util.xsd"> 
	<bean id="businessInterface"
		class="com.shansun.interceptor.test.busi.impl.BusinessClass">
	</bean>
	<bean id="interceptorHandler"
		class="com.shansun.interceptor.test.handler.InterceptorHandler"> 
	</bean>
	<bean id="springLoader"
		class="com.shansun.interceptor.test.SpringLoader"> 
		
	</bean>
</beans>

测试结果:

参数:

businessInterface.doSomething: Before invocation!

业务组件BusinessClass方法调用:doSomething()

businessInterface.doSomething: After invocation!

 

分享到:
评论

相关推荐

    无java基础快速上手ssm框架(myeclipse)

    总的来说,对于无Java基础的学习者,快速上手SSM框架需要耐心和实践,通过阅读文档、编写代码,逐步熟悉各个组件的功能和交互方式。MyEclipse作为强大的开发工具,将为你的学习之路提供便利。记住,实践是检验真理的...

    Java SpringBoot实现的过滤器(和拦截器)控制登录页面跳转

    它集成了大量常用的第三方库配置,如数据库、MVC、WebSocket等,使得开发者能够快速上手。 2. **Thymeleaf**: Thymeleaf是一个现代的服务器端Java模板引擎,它允许开发者在HTML中直接编写模板,然后在服务器端将...

    struts2快速上手(附例子)(例子已修改)

    在快速上手过程中,开发者会学习如何配置Struts2环境,创建Action类,编写struts.xml配置文件,设置拦截器,以及如何在JSP页面中展示Action返回的数据。此外,还会涉及Action的注解方式配置,这简化了XML配置,使得...

    maven整合ssm,结构清晰明了,初学者快速上手开发

    本项目标题为“maven整合ssm”,旨在为初学者提供一个清晰易懂的SSM整合示例,帮助快速上手开发。通过Eclipse集成开发环境和Maven构建工具,开发者可以更方便地管理依赖,实现项目的构建和运行。 首先,我们需要...

    Java Web整合开发实战:基于Struts 2+Hibernate+Spring(PDF)

    4章 Struts快速上手 5章 解密Struts之核心文件 6章 Struts之数据校验与GJ化 7章 详解Struts之标签库 8章 Struts之拦截器使用技巧 9章 在Struts中应用Ajax技术 10章 Struts之项目实战 3篇 持久层框架Hibernate...

    JFinal快速上手

    快速上手 JFinal 需要以下几个步骤: 1. 创建 Dynamic Web Project:首先,你需要在 Eclipse IDE 中创建一个新的动态 Web 项目。确保 Target Runtime 设置为 `&lt;None&gt;`,以便后续配置。 2. 修改 Output Folder:...

    spring mvc简介/快速上手 PPT

    3. 简化Web层开发:通过约定优于配置的理念,Spring MVC提供了易于理解的配置和代码组织方式,便于开发者快速上手和维护。 4. 开源和社区支持:Spring MVC是开源项目,拥有庞大的社区支持和丰富的资源,便于开发者...

    Apache CXF 快速上手教程.docx

    Apache CXF 是一个开源的Java框架,主要用于构建和部署Web服务。它建立在消息层之上,采用拦截器机制,提供了一种灵活的方式来扩展其功能。CXF 支持多种服务标准,如JAX-WS,使得开发人员可以方便地创建服务提供者和...

    java ee教程(电子教案)

    对于想要进入Java EE开发领域的学习者来说,这是一份宝贵的资料,可以帮助他们快速上手并提升技能。通过学习,你不仅可以理解每个技术的原理,还能学会如何在实际项目中运用它们,从而提高开发效率和软件质量。

    javaWebService 关于讲解java调用webservice的知识

    Java WebService 是一种基于开放标准(如SOAP、WSDL和UDDI)的通信协议,用于构建分布式系统中的互操作性。它允许不同平台、语言和技术的系统之间交换...PDF文档应该提供了详细的步骤和实践指导,帮助初学者快速上手。

    一个基于Java快速开发框架,支持mysql、xml、json等的基本操作。.zip

    本框架的核心是利用Java语言的特性,结合其他技术如MySQL数据库、XML配置以及JSON数据交换格式,为开发者提供一个强大而全面的平台。 首先,让我们深入了解Java作为基础的编程语言在框架中的作用。Java以其“一次...

    30天学会java web方立勋ppt(全)

    方立勋的Java Web教程可能包含了上述技术的详细讲解,以实践为导向,结合实例帮助学习者快速上手。通过30天的学习,你应该能够掌握基本的Java Web开发技能,具备独立开发简单Web应用的能力。此外,教程可能还会涉及...

    java调用webapi

    Java调用WebAPI是开发过程中常见的任务,尤其是在分布式系统或者微服务架构中,不同服务间通过API进行数据交互。...这个压缩包提供了一个实用的起点,通过示例和文档,可以帮助开发者快速上手并熟练掌握这一技能。

    shiro登录拦截校验demo

    Apache Shiro是一个强大的Java安全框架,它提供了身份验证(Authentication)、授权(Authorization...通过这个demo,开发者能够快速上手Shiro,并将其应用于自己的Java Web项目中,实现高效安全的用户管理和权限控制。

    java源码 java ee基础入门实例 2018127

    Java EE(Java Platform, Enterprise Edition)是Java编程语言在企业级应用开发中的核心框架,它提供了构建分布式、多层架构的Web应用程序所...对于初学者来说,这是一份宝贵的资源,可以帮助他们快速上手Java EE开发。

    张孝祥全 java 全集

    - **定义**:Java Web 开发是利用Java技术进行网络应用开发的过程。 - **发展历程**:自Java 2平台发布以来,Java Web 开发经历了多个阶段,从最初的Servlet API到如今广泛使用的Spring框架等。 #### 2. Java Web ...

    Java实现权限管理(已经验证过,导入可以直接使用)

    这对于开发者理解系统的工作原理和快速上手非常有帮助。 6. **MyEclipse集成**:MyEclipse作为流行的Java IDE,支持导入和运行已有的项目。导入这个权限管理系统后,开发者可以进一步调试、修改和扩展代码,以满足...

    使用truelicense进行Java程序license控制 经过扩张可以验证license 开始结束日期,验证绑定给定mac地址

    这对于开发者来说非常有价值,因为它提供了快速上手的途径,无需花费大量时间去搜索和整合各种资源。通常,这样的测试代码会展示如何生成许可证文件、如何在程序中集成Truelicense库以及如何处理许可证验证的各种...

Global site tag (gtag.js) - Google Analytics