软件152 苏锐
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();
}
}