简体   繁体   English

单身类的现场注入

[英]Field injection of singleton class

I have interface: 我有界面:

public interface DataStorage {
    boolean setData(String dataName);

    boolean unsetData(String dataName);

    Map getData();
}

And his implementation: 和他的实现:

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 module implementation: Guice模块的实现:

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

}

I'm trying to inject DataStorage to my Netty request handler. 我正在尝试将DataStorage注入我的Netty请求处理程序。 Request handler code: 请求处理程序代码:

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

And code responsible for binding request handler to Netty: 和负责将请求处理程序绑定到Netty的代码:

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

When I'm calling XMLRPC method "set_data" request fails, because dataStorage field in DataMethodsHandler is null. 当我调用XMLRPC方法时,“ set_data”请求失败,因为DataMethodsHandler中的dataStorage字段为null。 What I'm doing wrong? 我做错了什么?

I found solution. 我找到了解决方案。 First of all we should replace default RequestProcessorFactoryFactory method getRequestProcessorFactory which call Class.newInstance() with this code: 首先,我们应该使用以下代码替换默认的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);
            }
        };
    }
}

Create module for Guice: 为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();
    }
}

And than set new factory for handler: 然后为处理程序设置新工厂:

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

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

Now handler will catch all requests, all fields with annotations will be injected. 现在处理程序将捕获所有请求,所有带有注释的字段将被注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM