Guice学習(四)基本属性注入(Field Inject)
これでGoogle Guiceの3つの属性注入方式は,基本属性注入,構造関数注入,Setter注入記録を含む.
1、インタフェースと実現
2、テスト
1、インタフェースと実現
/* * Creation : 2015 6 30 */
package com.guice.fieldInject;
import com.google.inject.ImplementedBy;
@ImplementedBy(ServiceImpl.class)
public interface Service {
public void execute();
}
/* * Creation : 2015 6 30 */
package com.guice.fieldInject;
public class ServiceImpl implements Service {
@Override
public void execute() {
System.out.println("Hello Guice ,this is field inject demo !");
}
}
2、テスト
/* * Creation : 2015 6 30 */
package com.guice.fieldInject;
import com.google.inject.Guice;
import com.google.inject.Inject;
public class FieldInject {
@Inject
private Service service;
public Service getService() {
return service;
}
public static void main(String[] args) {
FieldInject instance = Guice.createInjector().getInstance(FieldInject.class);
instance.getService().execute();
}
}