`
夏莹_合肥
  • 浏览: 179375 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类
最新评论

Guice中的aop

阅读更多

1. Google的Guice是一个超轻量级的IOC和AOP容器。首先我们在工程中添加aopalliance.jar。


2. 我们创建一个自己的拦截器,实现MethodInterceptor接口。示例代码:

 

package com.yingxia.server.common;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class ServiceImplInterceptor implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation mi) throws Throwable {
		System.out.println();
		System.out.println("start: " + mi.getMethod().getDeclaringClass().getName() + "." + mi.getMethod().getName());
		Object obj = null;
		try {
			obj = mi.proceed();
		} catch(Throwable t) {
			t.printStackTrace();
			throw t;
		}
		System.out.println("end");
		System.out.println();
		return obj;
	}

}

 

3.  然后我们创建Injector,请注意示例代码中的这句,静态导入。


import static com.google.inject.matcher.Matchers.*;

 

所以我们可以直接使用com.google.inject.matcher.Matchers类中的静态方法。

 

package com.yingxia.server.common;

import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.server.rpc.RPC;
import com.google.gwt.user.server.rpc.RPCRequest;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;

import static com.google.inject.matcher.Matchers.*;

@SuppressWarnings("serial")
public class GuiceRemoteServiceServlet extends RemoteServiceServlet {

	private static final Injector injector = Guice.createInjector(new Module() {

		@Override
		public void configure(Binder binder) {
			binder.bindInterceptor(
				subclassesOf(GuiceRemoteServiceServlet.class), 
				any(),
				new ServiceImplInterceptor()
			);
		}
	});

	@Override
	public String processCall(String payload) throws SerializationException {
		RPCRequest rpcRequest = RPC.decodeRequest(payload);
		Class<?> serviceClass = rpcRequest.getMethod().getDeclaringClass();
		RemoteService service = (RemoteService) injector.getInstance(serviceClass);
		return RPC.invokeAndEncodeResponse(service, rpcRequest.getMethod(), rpcRequest.getParameters(), rpcRequest.getSerializationPolicy());
	}
	
}
 

请注意这句

 

			binder.bindInterceptor(
				subclassesOf(GuiceRemoteServiceServlet.class), 
				any(),
				new ServiceImplInterceptor()
			);
 

方法的第一个参数是匹配类,第二个参数是匹配方法,第三个参数是自定义拦截器。

 

分享到:
评论

相关推荐

    sisu-guice-3.1.3-no_aop.jar

    sisu-guice-3.1.3-no_aop.jar

    guice-2.0-no_aop.jar

    guice-2.0-no_aop.jar

    guice-3.0.rar

    - Guice支持AOP(面向切面编程),可以通过编写拦截器(Interceptors)来添加横切关注点。 - 支持自定义绑定策略,如绑定到特定类实例、单例、多例等。 7. **实战应用**: - 在Guice-3.0中,开发者可以通过创建...

    licenceCheck:Guice AOP示例项目

    Guice是Java中一个轻量级的依赖注入框架,而AOP则是一种编程范式,用于处理系统中的横切关注点,如日志、事务管理等。 **描述解析:** "许可证检查" 描述了项目的核心功能,即检查软件许可证。在软件开发中,确保...

    Guice用户指南翻译

    7. **AOP(面向切面编程)支持:** 虽然Guice本身不直接支持AOP,但可以与AspectJ等库结合使用,实现面向切面的编程,比如日志记录、事务管理等。 8. **绑定到具体类型:** Guice允许你将接口绑定到具体的类,也...

    Robojuice jar 包文件下载(内含guice-2.0-no_aop.jar 和roboguice-1.1.2.jar )

    `guice-2.0-no_aop.jar`是Guice的一个版本,不包含Aspect Oriented Programming(AOP)支持,这可能是因为在Android环境中,AOP的使用可能会增加复杂性和性能开销。 接下来,我们来看RoboGuice。RoboGuice是Guice的...

    aopalliance.jar

    aopalliance-1.0-20100517.210215-13.jar, aopalliance-1.0-sources.jar, aopalliance-1.0.jar, aopalliance-alpha1.jar, com.springsource.org.aopalliance-1.0.0.jar, ...guice-aopalliance.jar

    guice超轻量级依赖注入

    7. **AOP(面向切面编程)**:虽然Guice本身不直接支持AOP,但可以通过与其他库(如AspectJ)结合,实现拦截器(Interceptors)和切面。 8. **模块继承与组合**:Guice模块可以继承其他模块,也可以通过`install()`...

    google guice 3.0源码

    6. **AOP(面向切面编程)支持**:虽然Guice不是专门的AOP框架,但可以通过注解和拦截器(Interceptors)实现类似的功能,例如日志记录、事务管理等。 7. **子Injector(Sub-Injector)**:Guice 3.0引入了子...

    Google Guice需要的jar

    Guice还支持AOP(面向切面编程)特性,通过`@Interceptor`注解和`@Aspect`注解,可以方便地添加日志、事务管理等横切关注点。此外,Guice与其它Google库如Guava的集成也非常紧密,能够更好地支持复杂的应用场景。 ...

    google guice基础例子

    Guice是Google开发的一个轻量级,基于Java5(主要运用泛型与注释特性)的依赖注入框架(IOC)。Guice非常小而且快。Guice是类型安全的,它能够对构造函数,属性,方法(包含任意个参数的任意方法,而不仅仅是setter...

    sisu-guice-3.2.3-no_aop.jar

    java运行依赖jar包

    google-guice

    4. `aopalliance.jar`:AOP Alliance是一个接口集,提供了面向切面编程(Aspect-Oriented Programming,AOP)的通用API,Guice可以与之配合,实现更灵活的拦截器和切面逻辑。 5. `guice-spring-1.0.jar`:这个扩展...

    初试Guice(转)

    7. **AOP支持**:Guice与AspectJ结合,可以实现面向切面编程,如方法拦截和事务管理。 在实际应用中,Guice常用于构建复杂的应用框架或服务。例如,`AromaRI`可能是一个使用Guice实现的后台服务,通过Guice管理其...

    Guice与Spring框架的区别.pdf

    例如,在上面的代码中,我们定义了一个MyModule文件,其中configure方法用于配置Guice的依赖注入机制。在这个方法中,我们使用了binder.bind方法将MyService接口绑定到MyServiceImpl实现类,并指定了Scopes....

    shiro和guice整合,使用权限注解

    Apache Shiro 和 Google Guice 的整合是为了解决在Java应用程序中实现权限管理的问题。Shiro 是一个强大且易用的 Java 安全框架,提供了认证、授权、加密和会话管理功能,可以非常轻松地开发出足够安全的应用。而 ...

    guice入门教程helloworld篇

    Guice还提供了AOP(面向切面编程)特性,如`@Singleton`注解可以确保一个类只有一个实例。 Guice的优势在于它的简洁性和灵活性,可以适应各种规模的项目。通过模块化的配置,我们可以轻松地组织和管理代码中的依赖...

    guice-asynchronous:异步方法的 guice 扩展

    该项目的目标是扩展 Guice 的 AOP 功能并支持异步方法调用。 要异步方法,用户只需使用@Asynchronous注释标记方法。 有了这个,我们抽象了管理异步调用的横切关注点,并减少了伴随异步调用而产生的代码膨胀量。 问题...

    Google Guice: Agile Lightweight Dependency Injection Framework

    * Learn simple annotation-driven dependency injection, scoping and AOP, and why it all works the way it works. * Be the first to familiarize yourself with concepts that are likely to be included in ...

    aop jar包.zip

    AOP(Aspect Oriented Programming,面向切面编程)是一种编程范式,主要目的是解决软件系统中的横切关注点,如日志、事务管理、权限控制等。这些关注点通常会散布在应用程序的多个类和方法中,使得代码复用困难且...

Global site tag (gtag.js) - Google Analytics