标签: guice

Roboguice应用程序@InjectView

我想在我的Android应用程序中使用@InjectViews,但这似乎不起作用.这是它的代码.我已将库和jar包括在我的Android应用程序中,并直接从RoboActivity类扩展.

@InjectView(R.id.username)
EditText username;

@InjectView(R.id.firstname)
EditText firstname;

@InjectView(R.id.lastname)
EditText lastname;

@InjectView(R.id.email)
EditText email;

@InjectView(R.id.password)
EditText password;

@InjectView(R.id.confirmpassword)
EditText confirmpassword;

@InjectView(R.id.btnSubmit)
ImageButton submitbutton;

@InjectView(R.id.btnSignin)
ImageButton signinbutton;

String loginAvailabilityURL;
String createMemberURL;
Run Code Online (Sandbox Code Playgroud)

这段代码对我不起作用.有什么不对?如果需要,我可以在pastie.org上分享.java.谢谢.

android ioc-container guice android-layout roboguice

1
推荐指数
1
解决办法
2847
查看次数

使用GWT的Gin问题 - 每次调用GWT.create(someClass.class)返回不同的实例

这是我的问题.我在一个gwt项目中使用Gin,我使用GWT.create(SomeClass.class)来获取实例,但问题是我需要signleton实例,为此我将app模块中的该类绑定为singleton.每个tome我执行GWT.create(TemplatePanel.class)它返回不同的实例..为什么?这是我的代码片段.模

public class AppClientModule extends AbstractGinModule
{
protected void configure()
{
  bind(MainPanel.class).in(Singleton.class);
  bind(TemplatePanel.class).in(Singleton.class);
}
} 
Run Code Online (Sandbox Code Playgroud)

注射器

@GinModules(AppClientModule.class)
public interface AppInjector extends Ginjector
{
  MainPanel getMainForm();
  TemplatePanel getTemplateForm();
}
Run Code Online (Sandbox Code Playgroud)

TemplatePanel

public class TemplatePanel extends VerticalPanel
@Inject
public TemplatePanel()
{
  this.add(initHeader());
  this.add(initContent());
}
..
Run Code Online (Sandbox Code Playgroud)

mainPanel中

public void onSuccess(List<MyUser> result)
    {
.......
  TemplatePanel temp = GWT.create(TemplatePanel.class);

.......
}
Run Code Online (Sandbox Code Playgroud)

和切入点

private final AppInjector injector = GWT.create(AppInjector.class);
public void onModuleLoad()
{
  MainPanel mf = injector.getMainForm();
  TemplatePanel template = injector.getTemplateForm();
  template.setContent(mf);
  RootPanel.get().add(template);
}
Run Code Online (Sandbox Code Playgroud)

java gwt gwt-gin guice

1
推荐指数
1
解决办法
1773
查看次数

使用MockEndpoint测试骆驼路线

为什么此测试失败,并显示“ java.lang.AssertionError:mock:// destino收到的消息计数。应为:<12>,但为:<0>”?我只是想测试骆驼是否真的可以重新排序邮件。

进口:

import java.util.ArrayList;
import java.util.List;

import org.apache.camel.CamelContext;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.guice.CamelModuleWithRouteTypes;
import org.jukito.JukitoModule;
import org.jukito.JukitoRunner;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.google.inject.Inject;
Run Code Online (Sandbox Code Playgroud)

考试:

@RunWith(JukitoRunner.class) public class ResequenceTest {

    @Inject protected CamelContext context;
    @Produce protected ProducerTemplate template;

    public static class Module extends JukitoModule {
        @SuppressWarnings("unchecked") protected void configureTest() {
            install(new CamelModuleWithRouteTypes(OrderingTestRouteBuilder.class));
        }
    }

    @Test public void testDozenMsgsOrderByIntegerBody() throws Exception {
        // fail();
            Integer[] input = new Integer[] {12, 11, 10, 9, 8, 7, …
Run Code Online (Sandbox Code Playgroud)

guice apache-camel

1
推荐指数
1
解决办法
2744
查看次数

使用Guice的GenericDAO,使用Generics和ParameterizedType

大多数人应该熟悉为Spring + hibernate制作Generic DAO.参考资料来自http://www.ibm.com/developerworks/java/library/j-genericdao/index.html,单DAO和通用CRUD方法(JPA/Hibernate + Spring)有所改进

这种改进是对类型的检测,因为它是超类的一部分,而不是使用构造函数来判断它是哪个类

public GenericDaoJpaImpl() {
    ParameterizedType genericSuperclass = (ParameterizedType) getClass()
         .getGenericSuperclass();
    this.entityClass = (Class<T>) genericSuperclass
         .getActualTypeArguments()[0];
}
Run Code Online (Sandbox Code Playgroud)

但是,这个演员会因Guice而失败.要注入,接口和类需要绑定在这样的模块中

bind(TestDao.class).to(TestDaoImpl.class);
Run Code Online (Sandbox Code Playgroud)

通过这样做,我们的GenericDAO的构造函数将无法工作,因为以下内容:

getClass().getGenericSuperclass() = java.lang.Class
getClass().getName() = com.gwtplatform.samples.basic.server.dao.TestDaoImpl$$EnhancerByGuice$$5fe0d6fd
Run Code Online (Sandbox Code Playgroud)

与Spring + Hibernate返回的内容相反

getClass().getGenericSuperclass() = sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl
getClass().getName() =  com.gwtplatform.samples.basic.server.dao.TestDaoImpl
Run Code Online (Sandbox Code Playgroud)

我现在只是在我的扩展DAO中使用超级构造函数,但是仍然希望获得该类型而不是提供它,任何想法?

java generics hibernate guice

1
推荐指数
1
解决办法
2171
查看次数

Roboguice注射系统服务

我正在学习使用Roboguice,我在理解如何注入系统服务方面遇到了麻烦.我见过的所有例子都是注入活动,但我想将它注入POJO.我正在使用Roboguice 2.0 beta 3

我有一个PhoneNumber类,并希望注入TelephonyManager服务.

public class PhoneNumber {
    @Inject TelephonyManager mTelephonyManager;

    protected Integer getNetworkCountryPrefix() {

        // This gives a null pointer exception 
        mTelephonyManager.getNetworkCountryIso();
    }
}
Run Code Online (Sandbox Code Playgroud)

注入扩展RoboActivity的类时,一切正常.但是,是否可以在不扩展RoboActivity的类中注入TelephonyManager?

android dependency-injection guice roboguice

1
推荐指数
1
解决办法
2245
查看次数

使用Play Framework和Guice进行依赖注入会导致应用程序启动时出现NullPointerException

我有以下代码:

import javax.inject.Inject;
import models.Subject;
import models.dao.SchoolDao;
import models.dao.SubjectDao;
import play.Application;
import play.GlobalSettings;
import play.db.jpa.JPA;
import play.db.jpa.Transactional;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;


public class Global extends GlobalSettings{

private Injector injector;

@Inject
private SubjectDao subjectDao;

@Override
public void onStart(Application application) {
    injector = Guice.createInjector(new AbstractModule() {
        @Override
        protected void configure() {
            bind(SchoolDao.class);
            bind(SubjectDao.class);
        }
    });

    insertInitialData();
}

@Override
public <A> A getControllerInstance(Class<A> aClass) throws Exception {
    return injector.getInstance(aClass);
}

@Transactional
private void insertInitialData(){

    if(subjectDao.countAll() == 0){
        Subject deutsch = new …
Run Code Online (Sandbox Code Playgroud)

dependency-injection guice playframework

1
推荐指数
1
解决办法
1193
查看次数

Guice绑定绑定子类吗?

所以我有一个模块,它将接口与实现类绑定在一起.

bind(ILocalStore.class).to(LocalStore.class);
Run Code Online (Sandbox Code Playgroud)

此绑定是否还会注入以下构造函数:

@Inject
public LocalStoreUser(LocalStore localstore) {
    this.localstore = localstore
}
Run Code Online (Sandbox Code Playgroud)

guice

1
推荐指数
1
解决办法
1651
查看次数

将参数传递给guice模块是"不好的做法"

检查Guice,我喜欢它.我目前有问题,guice通过注入我需要的所有必需依赖项来解决它.但我想知道我是否以错误的方式使用Guice.我需要的是根据特定实例定义绑定.为了实现这一点,我在模块中传递了实例.

例如,考虑以下内容(有点类似于我的问题):

public class CustomerModule extends AbstractModule { 
   private Customer customer;

   public CustomerModule(Customer customer){
       this.customer = customer;
   }  

   @Override 
   public void configure() {
      bind(ReportGenerator.class).to(HtmlReportGenerator.class);
   }

   @Provides 
   Account providePurchasingAccount() { 
      return customer.getPurchasingAccount();
   }
}
Run Code Online (Sandbox Code Playgroud)

我使用此模块将Account依赖项注入到需要特定客户帐户的报表生成器类中.例如,用户选择特定客户并说,想要显示生成的报告.我有方法

public void printReport (Customer customer){
   Injector injector = Guice.createInjector(new CustomerModule(customer));
   ReportGenerator reportGenerator  = injector.getInstance(ReportGenerator.class);

   showReport(reportGenerator.generate())
}
Run Code Online (Sandbox Code Playgroud)

工作完成后,我完成了这个模块.

这是一个好用的guice吗?

dependency-injection guice

1
推荐指数
1
解决办法
835
查看次数

龙目岛和Guice注射

我对lombok和guice注入不熟悉,我可以了解一般的概念,但是由于语法原因,我遇到了一些我不理解也无法搜索的代码。以下是代码,有人可以帮助我理解吗?

import com.google.inject.Inject;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;

@AllArgsConstructor(access = AccessLevel.PRIVATE, onConstructor = @__({ @Inject }))
public class SomeClass {
...
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

dependency-injection guice lombok

1
推荐指数
1
解决办法
6750
查看次数

使用Guice,如何将单元测试中的模拟对象注入到要测试的类中

考虑以下代码:

@Singleton
public class MyServiceImpl {
    public int doSomething() {
        return 5;
    }
}

@ImplementedBy(MyServiceImpl.class)
public interface MyService {
    public int doSomething();
}

public class MyCommand {
    @Inject private MyService service;

    public boolean executeSomething() {
        return service.doSomething() > 0;
    }
}

public class MyCommandTest {
    @InjectMocks MyServiceImpl serviceMock;
    private MyCommand command;

    @Before public void beforeEach() {
        MockitoAnnotations.initMocks(this);
        command = new MyCommand();
        when(serviceMock.doSomething()).thenReturn(-1); // <- Error here
    }

    @Test public void mockInjected() {
        boolean result = command.executeSomething();
        verify(serviceMock).doSomething();
        assertThat(result, equalTo(false));
    } …
Run Code Online (Sandbox Code Playgroud)

java dependency-injection guice mockito

1
推荐指数
1
解决办法
6737
查看次数