首页 > 代码库 > Guice示例
Guice示例
1.新建IWork接口:
package com.kay.interfaces;
public interface IWork {
public void work(String workname);
}
public interface IWork {
public void work(String workname);
}
2.实现IWork接口:
package com.kay.impl;
import com.kay.interfaces.IWork;
public class Workimpl implements IWork{
public void work(String workname) {
System.out.println(workname);
}
}
import com.kay.interfaces.IWork;
public class Workimpl implements IWork{
public void work(String workname) {
System.out.println(workname);
}
}
3.来一个具体的work:
package com.kay.work;
import com.kay.impl.Workimpl;
public class Cooking {
private Workimpl work ;
private String workname = "cooking";
public void work(){
work.work(workname);
}
}
import com.kay.impl.Workimpl;
public class Cooking {
private Workimpl work ;
private String workname = "cooking";
public void work(){
work.work(workname);
}
}
4.测试一下:
package com.kay.Test;
import com.kay.work.Cooking;
import junit.framework.TestCase;
public class CookingTest extends TestCase {
Cooking cook ;
protected void setUp() throws Exception {
cook = new Cooking();
}
public void testcook(){
cook.work();
}
}
import com.kay.work.Cooking;
import junit.framework.TestCase;
public class CookingTest extends TestCase {
Cooking cook ;
protected void setUp() throws Exception {
cook = new Cooking();
}
public void testcook(){
cook.work();
}
}
结果大家都知道,JUnit肯定是红条。下面使用Guice来注入:
改变Cooking类:
package com.kay.work;
import com.google.inject.Inject;
import com.kay.impl.Workimpl;
public class Cooking {
private Workimpl work ;
private String workname = "cooking";
@Inject
public void Workimpl(Workimpl workimpl){
this.work = workimpl;
}
public void work(){
work.work(workname);
}
}
import com.google.inject.Inject;
import com.kay.impl.Workimpl;
public class Cooking {
private Workimpl work ;
private String workname = "cooking";
@Inject
public void Workimpl(Workimpl workimpl){
this.work = workimpl;
}
public void work(){
work.work(workname);
}
}
创建类实现Module接口:
package com.kay.Module;
import com.google.inject.Binder;
import com.google.inject.Module;
import com.kay.impl.Workimpl;
import com.kay.interfaces.IWork;
public class MyModule implements Module {
public void configure(Binder binder) {
binder.bind(IWork.class).to(Workimpl.class);
}
}
import com.google.inject.Binder;
import com.google.inject.Module;
import com.kay.impl.Workimpl;
import com.kay.interfaces.IWork;
public class MyModule implements Module {
public void configure(Binder binder) {
binder.bind(IWork.class).to(Workimpl.class);
}
}
改变一下测试类:
package com.kay.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.kay.Module.MyModule;
import com.kay.work.Cooking;
import junit.framework.TestCase;
public class CookingTest extends TestCase {
Cooking cook ;
MyModule mm ;
protected void setUp() throws Exception {
cook = new Cooking();
mm = new MyModule();
}
public void testcook(){
Injector in = Guice.createInjector(mm);
in.injectMembers(cook);
cook.work();
}
}
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.kay.Module.MyModule;
import com.kay.work.Cooking;
import junit.framework.TestCase;
public class CookingTest extends TestCase {
Cooking cook ;
MyModule mm ;
protected void setUp() throws Exception {
cook = new Cooking();
mm = new MyModule();
}
public void testcook(){
Injector in = Guice.createInjector(mm);
in.injectMembers(cook);
cook.work();
}
}
Guice示例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。