单场类的现场注入

时间:2015-05-19 14:46:05

标签: java singleton guice

我有界面:

public interface DataStorage {
    boolean setData(String dataName);

    boolean unsetData(String dataName);

    Map getData();
}

他的实施:

public class DataStorageImpl implements DataStorage {

    private Map dataMap = new HashMap<>();

    public synchronized boolean setData(String dataName) {
        dataMap.put(dataName, true);

        return true;
    }

    public synchronized boolean unsetData(String dataName) {
        dataMap.put(dataName, false);

        return true;
    }

    public synchronized Map getData() {
        return dataMap;
    }
}

Guice模块实施:

public class DataStorageModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(DataStorage.class).annotatedWith(Names.named("DataStorage")).to(DataStorageImpl.class).in(Singleton.class);
    }

}

我正在尝试将DataStorage注入我的Netty请求处理程序。请求处理程序代码:

public class DataMethodsHandler {
    @Inject @Named("DataStorage")
    private DataStorage dataStorage;

    public String version() {
        return "0.0.1";
    }

    public Map list_data() {
        return dataStorage.getData();
    }

    public boolean set_data(String dataName) {
        return dataStorage.setData(dataName);
    }

    public boolean unset_data(String dataName) {
        return dataStorage.unsetData(dataName);
    }
}

负责将请求处理程序绑定到Netty的代码:

    PropertyHandlerMapping phm = new CustomPropertyHandlerMapping();
    phm.setVoidMethodEnabled(true);
    try {
        phm.addHandler("", dataMethodsHandler.class);
    } catch (XmlRpcException e) {
        e.printStackTrace();
    }

当我调用XMLRPC方法时,“set_data”请求失败,因为DataMethodsHandler中的dataStorage字段为null。我做错了什么?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。首先,我们应该使用以下代码替换默认的RequestProcessorFactoryFactory方法getRequestProcessorFactory,该方法调用Class.newInstance():

public class XmlRpcServerClientRequestProcessorFactoryFactory implements RequestProcessorFactoryFactory {

    @Inject
    private Injector injector;
    @Inject
    public XmlRpcServerClientRequestProcessorFactoryFactory(Injector injector) {
        this.injector = injector;
    }

    @Override
    public RequestProcessorFactory getRequestProcessorFactory(final Class pClass)
            throws XmlRpcException {
        return new RequestProcessorFactory() {
            @SuppressWarnings("unchecked")
            @Override
            public Object getRequestProcessor(XmlRpcRequest pRequest)
                    throws XmlRpcException {
                return injector.getInstance(pClass);
            }
        };
    }
}

为Guice创建模块:

public class NettyXmlRpcModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(NettyXmlRpcModule.class);
        bind(XmlRpcServerClientRequestProcessorFactoryFactory.class).in(Singleton.class);
        bind(Integer.TYPE).annotatedWith(Names.named("xmlrpc.port")).toInstance(3133);
    }


    @Provides
    @Singleton
    Log providesLog(){
        return LogFactory.getLog(NettyXmlRpcWebServer.class);
    }

    @Provides
    InetAddress providesInetAddress() throws UnknownHostException {
        return InetAddress.getLocalHost();
    }
}

而不是为处理程序设置新工厂:

PropertyHandlerMapping phm = new CustomPropertyHandlerMapping();
        phm.setRequestProcessorFactoryFactory(xmlRpcServerClientRequestProcessorFactoryFactory);

phm.setVoidMethodEnabled(true);
try {
    phm.addHandler("", methodsHandler);
} catch (XmlRpcException e) {
    e.printStackTrace();
}
xmlRpcServer.setHandlerMapping(phm);

现在处理程序将捕获所有请求,所有带注释的字段都将被注入。