单例的字段注入class

Field injection of singleton class

我有接口:

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;
    }
}

指导模块实现:

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 字段为空。我做错了什么?

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

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);

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