小编dur*_*597的帖子

Guice 中的空 Multibinder/MapBinder

在使用GuiceMapBinder构建插件架构的过程中,使用Guice 3.0,我遇到了 GuiceCreationException在剥离所有模块时抛出 a 的问题,这是该应用程序中的一个可行配置。有没有办法让 Guice 注入一个空的Map?或者,通过扩展,一个空集Multibinder

例如:

interface PlugIn {
    void doStuff();
}

class PlugInRegistry {
    @Inject
    public PlugInRegistry(Map<String, PlugIn> plugins) {
        // Guice throws an exception if OptionalPlugIn is missing
    }
}

class OptionalPlugIn implements PlugIn {
    public void doStuff() {
        // do optional stuff
    }
}

class OptionalModule extends AbstractModule {
    public void configure() {
        MapBinder<String, PlugIn> mapbinder =
            MapBinder.newMapBinder(binder(), String.class, PlugIn.class);
        mapbinder.addBinding("Optional").to(OptionalPlugIn.class);
    }
}
Run Code Online (Sandbox Code Playgroud)

java plugins dependency-injection guice

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

用CSS打印出变异的元音

我正在为CSS中的响应表创建表头,如下所示:

td:nth-of-type(1):before { content: "Abfahrtszeitpunkt"; }
td:nth-of-type(2):before { content: "Startort"; }
td:nth-of-type(3):before { content: "Zielort"; }
td:nth-of-type(4):before { content: "Karte"; }
td:nth-of-type(5):before { content: "Bewertung"; }
td:nth-of-type(6):before { content: "Fahrt löschen"; }
Run Code Online (Sandbox Code Playgroud)

但是当我在我的webbrowser中加载这个表时,"ö"没有以正确的方式显示.网侧只打印出带有问号的菱形.我已经尝试用html和javascript修复它,但它不起作用.有没有一种简单的方法来解决我的问题?

html javascript css

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

Guice:注入一个字符串的ArrayList

我试图注入一个ArrayListString与吉斯的帮助秒.我想显示一个带有许多RadioButtons的面板(例如),用户可以选择一些要激活的服务.

选择后,我想获取所选服务的所有名称并将其添加到列表中,并将此列表注入负责创建服务的经理.这是一个例子:

public class UIProviderModule extends ProviderServiceModule {
    private ArrayList<String> requestedServices;

    public UIProviderModule(ArrayList<String> requestedServices) {
        this.requestedServices = requestedServices;
    }

    @Override
    protected void configure() {
        bindConstant().annotatedWith(Names.named(Constants.REQ_SERVICES)).to(requestedServices);
        bind(IParser.class).to(UIParser.class);
        super.configure();
    }

}
Run Code Online (Sandbox Code Playgroud)

我见过很多关于Multibindings和提供者的帖子,但我不明白这对我有什么帮助.我只是想检索名称,因为我不使用必须绑定到接口的类.我错过了什么吗?

注意:我知道这可能不是使用Guice好方法,因为我将列表绑定到Module.

java arraylist inject guice

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

用Guice注入Map的值

我有一个Guice托管服务,注入了一些其他服务.根据传递给我的服务方法的键值使用其他服务.所以我想制作一个Map将要使用的服务映射到相应的密钥:

@Inject
private IServiceA serviceA;

@Inject
private IServiceB serviceB;

private Map<String, IService> mapping;

private Map<String, IService> getMapping() {
    if (mapping == null) {
        mapping = new HashMap<String, IService>();
        mapping.put("keyA", serviceA);
        mapping.put("keyB", serviceB);
    }
}

@Override
public void myServiceMethod(String key) {
    IService serviceToUse = getMapping().get(key);
    // ... use some methods of the identified service
}
Run Code Online (Sandbox Code Playgroud)

这个解决方案有效,但看起来很尴尬,因为我必须对映射进行这种懒惰的初始化.我试图使用一个static块,但此时Guice尚未初始化实例成员.

我更愿意直接用Guice注入映射值,但我不知道如何实现这一点.

java dependency-injection guice

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

为什么这些构造函数按此顺序运行?

我已经知道应该创建这样的对象Class_name Object_name=new Classname().在我的程序中,我创建了一个名为的超类sup和一个名为的派生类der.在我的子类构造函数中,我创建了一个这样的对象sup obc = new der();.没有任何错误,它编译并给出如下输出:

In Superclass with object passed as reference
In Superclass with no constructor
In derived class with no constructor
Run Code Online (Sandbox Code Playgroud)

我不明白我是如何按此顺序获得输出的.为什么会这样?这是完整的代码:

class sup {

  private int a, b, c;

  sup(sup ob) {
    System.out.println("In Superclass with object passed as reference");
    a = ob.a + 9;
    b = ob.b + 9;
  }

  sup(int a, int b) {
    this.a = a;
    this.b = b;
  }

  sup() {
    System.out.println("In Superclass with …
Run Code Online (Sandbox Code Playgroud)

java constructor object

3
推荐指数
2
解决办法
75
查看次数

如果我安装新版本的JDK,运行Java进程会发生什么?

我想知道如果我在Debian机器上更新JDK,运行Java进程会发生什么.对于其他系统,答案可能也是一样的,我只想更清楚地了解我的环境.

使用更新,我的意思是较新的JDK将安装在现有JDK旁边,并且符号链接/usr/lib/jvm/default-java将更改为新JDK的文件夹.

更新后的文件夹结构:

  • /usr/lib/jvm/default-java - > /usr/lib/jvm/jdk1.7.0_79
  • /usr/lib/jvm/jdk1.7.0_21 # old version
  • /usr/lib/jvm/jdk1.7.0_79 # new version

运行过程是否会受到此更改的影响?

java linux jvm

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

如何使用工厂模式的匿名对象?

我有一个方法如下:

public class FooFactory {
    public Foo createNewFoo(){
        return new foo();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我这样做:

FooFactory fooFactory = new FooFactory();
Foo foo = FooFactory.createNewFoo();
Run Code Online (Sandbox Code Playgroud)

它会完美地运作.但是,如果我尝试这样做:

new Foo() = FooFactory.createNewFoo();
Run Code Online (Sandbox Code Playgroud)

它似乎根本不起作用.它说"变量预期".

我理解,new Foo()它本身会创建一个新的Foo对象,但即使我使用工厂,它也应该用新Foo对象覆盖匿名对象.

我也试过创造一个ArrayList拥有Foo和做的东西

arrayList.add(new Foo());
arrayList.get(0) = FooFactory.createNewFoo();
Run Code Online (Sandbox Code Playgroud)

它仍然说"变量预期".为什么这么说?

Foo foo = new Foo();
Foo otherFoo = foo;
Run Code Online (Sandbox Code Playgroud)

这非常好用,所以我不明白为什么我不能让工厂使用匿名对象.

我尝试在网上搜索这个,但我没有搜索结果,这告诉我,我可能犯了一些荒谬的错误/使用工厂模式错了.

java anonymous-types factory-pattern

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

JPanel在不同的课堂上,是不是我做得不好?为什么?

我创建了一个新类,扩展了JFrame和扩展JPanel以创建swing GUI的新类.很棒,我喜欢这个,因为它易于阅读.

然而,当涉及到事件处理时,事情变得越来越复杂.我所做的确实不是一个解决方案; 就像打破好习惯,让事情发挥作用一样.我该如何正常工作?

这是我的JFrame类

public class MainFrame extends JFrame{
    private JTextArea textArea;

    public MainFrame(String title){
        super(title);

        //set layout
        setLayout(new BorderLayout());

        //create components
        JButton buttonOne = new JButton("click me");
        textArea = new JTextArea();
        JPanel detailedPanel = new leftPanel();

        //add to panel
        Container c = getContentPane();

        c.add(buttonOne, BorderLayout.SOUTH);
        c.add(textArea, BorderLayout.CENTER);
        c.add(detailedPanel, BorderLayout.WEST);

        //Event Listening
        leftPanel.buttonAdd.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                textArea.setText(textArea.getText() + " " + leftPanel.fieldName.getText() + " : " + leftPanel.fieldOccupation.getText());
            }

        });

    }

}
Run Code Online (Sandbox Code Playgroud)

这是我的JPanel

public class leftPanel …
Run Code Online (Sandbox Code Playgroud)

java user-interface swing event-handling jpanel

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

为什么Guice不能绑定中间依赖?

这是我的代码:

// Groovy
interface MyMapper {
    Buzz toBuzz(Fizz fizz);
}

class MyMapperImpl implements MyMapper {
    @Named("SIMPLE_FOOBAR")
    Foobar foobar;

    MyMapperImpl(Foobar foobar) {
        super();
        this.foobar = foobar;
    }

    @Override
    Buzz toBuzz(Fizz fizz) {
        // ...etc.
    }
}

class Whistlefeather {
    MyMapper mapper;

    Whistlefeather(MyMapper mapper) {
        super();

        this.mapper = mapper;
    }

    void doSomething(Fink fink) {
        Fizz fizz = getSomehow(fink);
        Buzz buzz = mapper.toBuzz(fizz);

        // Do something with 'buzz'...
    }
}

class ApplicationMain {
    Whistlefeather whistlefeather;

    @Inject
    ApplicationMain(Whistlefeather whistlefeather) {
        super();

        this.whistlefeather = whistlefeather; …
Run Code Online (Sandbox Code Playgroud)

java dependency-injection guice

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

为什么这个grep命令没有回来?(击)

我有一个文本文件'samp',我想要grep只有以大写元音开头和结尾的行.

使用" grep ^[AEIOU] samp"有效.
使用" grep [AEIOU]$ samp"也可以.

但是当试图将它们组合成" grep ^[AEIOU]$ samp"时,它什么都不返回.

3件事:

  • 是的,我的行以samp中的大写元音开头和结尾.
  • 是的,我尝试了我能想到的每一个引号组合.什么都没有.
  • 是的,我是unix的新手.

regex bash grep

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