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. request.getAttribute( "result");和request.setAttribute("result",username);

    request.setAttribute("result",username);在request对象中加入名为result的属性并附值为username,因为request对象是可 ...

  2. 在Global.asax文件里实现通用防SQL注入漏洞程序(适应于post/get请求)

    可使用Global.asax中的Application_BeginRequest(object sender, EventArgs e)事件来实现表单或者URL提交数据的获取,获取后传给SQLInje ...

  3. HTML5 Canvas Text文本居中实例

    1.代码: <canvas width="700" height="300" id="canvasOne" class="c ...

  4. updatepanel的属性

    updatepanel的属性 1.childrenastriggers:内容模板内的子控件的回发是否更新本模板(和updatemode的conditional有关) 2.updatemode:内容模板 ...

  5. SQL Server 2008创建定期自动备份任务

    首先需要启动SQL Server Agent服务,这个服务如果不启动是无法运行新建作业的,点击“开始”–“所有程序”–“Microsoft SQL Server 2008”–“启动SQL Server ...

  6. iOS9 集成指纹解锁

    添加依赖库 LocalAuthentication.framework #import <LocalAuthentication/LocalAuthentication.h> // 头文件 ...

  7. _Obj* __STL_VOLATILE* __my_free_list

    今天在读<STL源码剖析>空间配置器第二级时看到了这句,有点不解,于是查阅后知: obj后面是个指针 STL_VOLATILE也应该是个类型定义的吧,程序中应该有define来对它定义.所 ...

  8. 逆向iOS SDK -- _UIImageAtPath 的实现(SDK 5.1)

    注释过的反汇编代码:http://pan.baidu.com/share/link?shareid=3491166579&uk=537224442 伪代码(不精确,仅供参考): NSStrin ...

  9. Spring MVC PageNotFound.noHandlerFound No mapping found for HTTP request with URI

    首先骂人,干他娘的,弄了两个小时原来的包倒错了!!唉TMD. 注意用IDEA倒包的时候一定要注意ModelAndView是 原因是import出错了!!应该是import org.springfram ...

  10. span宽度高度设置

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...