Guice学习(一)

Guice是Google开发的一个轻量级依赖注入框架(IOC)。Guice非常小而且快,功能类似与Spring,但效率上网上文档显示是它的100倍,而且还提供对Servlet,AOP,Struts等框架的支持;这里是简单代码实现,首先要下载Guice包,http://code.google.com/p/google-guice/这里可以下载;程序结构如下

这里导入了guice包与inject包,最简单的实现;下面是相关代码:

UserDao实现

package com.wf.dao;
 
public class UserDao {
 
    public boolean saveUser(){
        System.out.println("user have save");
        return true;
    }
}

UserService实现

package com.wf.services;
 
import javax.inject.Inject;
 
import com.wf.dao.UserDao;
 
public class UserService {
 
    private UserDao userDao;
 
    @Inject
    public void setUser(UserDao userDao) {
        this.userDao = userDao;
    }
     
    public boolean saveUser(){
        boolean result = userDao.saveUser();
        System.out.println(result);
        return result;
    }
}

  下面是相当与Spring中xml的一个类MyModule

package com.wf.util;
 
import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.Scopes;
import com.wf.services.UserService;
 
public class MyModule implements Module {
 
    @Override
    public void configure(Binder binder) {
        binder.bind(UserService.class).in(Scopes.SINGLETON);
    }
 
}

  这三个类创建完毕后就可以测试了,下面是单元测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.wf.test;
 
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.wf.services.UserService;
import com.wf.util.MyModule;
 
import junit.framework.TestCase;
 
public class Test extends TestCase {
 
    private UserService userService;
    protected void setUp() throws Exception {
        userService = new UserService();
    }
 
    public void testUserService(){
        Module module = new MyModule();
        Injector in = Guice.createInjector(module);
        in.injectMembers(userService);
        assertTrue(userService.saveUser());
    }
}

  以上是注入的简单实现,Guice还有与其它框架的结合使用,可能现在主流还是Spring,不过这个应该也会渐渐被大家注意;感谢Google

Guice学习(一)的更多相关文章

  1. Google Guice学习

    学习动力:公司项目使用 官方文档:https://github.com/google/guice/wiki/Motivation 学习阶段:入门 主要部份: 简介 Bindings方式 Scopes设 ...

  2. Guice 学习(六)使用Provider注入服务( Provider Inject Service)

    1.定义接口 package com.guice.providerInject; import com.google.inject.ProvidedBy; public interface Servi ...

  3. Guice 学习(五)多接口的实现( Many Interface Implementation)

    1.接口 /* * Creation : 2015年6月30日 */ package com.guice.InterfaceManyImpl; public interface Service { p ...

  4. Guice 学习(七)常量和属性的注入( Constant and Property Inject)

    1.常量注入方式 package com.guice.ConstantInjectDemo; import com.google.inject.Binder; import com.google.in ...

  5. Guice 学习

    Guice: 是一个轻量级的DI框架. 不需要繁琐的配置,只需要定义一个Module来表述接口和实现类,以及父类和子类之间的关联关系的绑定,如下是一个例子. http://blog.csdn.net/ ...

  6. Guice 学习(八)AOP (面向切面的编程)

    Guice的AOP还是非常弱的.眼下只支持方法级别上的,另外灵活性也不是非常高. 看例如以下演示样例: Guice支持AOP的条件是: 类必须是public或者package (default) 类不 ...

  7. guice基本学习,guice的学习资料(十)

    这个是我前面几篇的参考. guice的学习资料下载:http://pan.baidu.com/s/1bDEPem 路途遥远,但是人确在走.不忘初心,方得始终.

  8. java轻量级IOC框架Guice

    Google-Guice入门介绍(较为清晰的说明了流程):http://blog.csdn.net/derekjiang/article/details/7231490 使用Guice,需要添加第三方 ...

  9. Java Web 学习路线

    实际上,如果时间安排合理的话,大概需要六个月左右,有些基础好,自学能力强的朋友,甚至在四个月左右就开始找工作了.大三的时候,我萌生了放弃本专业的念头,断断续续学 Java Web 累计一年半左右,总算 ...

随机推荐

  1. Struts2里如何取得request,session,application

    第一种:取得MAP类型的request,session,application在java文件里写 package com.xjtu.st; import java.util.Map; import c ...

  2. Android之fragment点击切换和滑动切换结合

    学了一小段时间的Android,主要接触的是UI设计,打交道最多莫过于fragment了吧.在Android3.0引入了fragment的概念后,几乎在所以的Android的应用中都可以看见其身影,已 ...

  3. Kill Processes in Linux

    Step 1: find processes to kill ps -ef | grep java Step 2: Kill the process based on process id kill ...

  4. Python datetime time 常用操作

    测试版本: Python 2.7 获取当前时间的两种方法 import datetime,time now = time.strftime("%Y-%m-%d %H:%M:%S") ...

  5. 02_ Windows与Linux双重引导

    1. Grub2引导window. ---------------------步骤1--------------------------------- vim /etc/grub.d/40_custo ...

  6. 我牵头,你做事——C#委托实践

     我牵头,你做事——C#委托实践一 2007-09-05 23:54:54 标签:委托 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http ...

  7. JQUERY1.9学习笔记 之基本过滤器(十) 非选择器

    非选择器jQuery( ":not(selector)" ) 例:找出所有input标签为非"checked"的,并且高亮其邻居元素span. <!DOC ...

  8. 关于json.ajax ,php的那点事

    $.ajax({ type:'post'/'get'  两者选其一 url:    地址 data: "newdata="+newdata+"&olddata=& ...

  9. C语言初学 数组 打印菱形

    #include<stdio.h> #include<stdlib.h> int main() { int n,i,j; printf("---开始打印符号--\n& ...

  10. Python异常的使用

    伪代码: try: 出错部分的代码...... except Exception as e: print '404网页' #Exception是所有错误类型的父类,包括所有出错信息 finally: ...