Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 需要构造函数参数的Restlet服务器资源_Java_Inversion Of Control_Restlet - Fatal编程技术网

Java 需要构造函数参数的Restlet服务器资源

Java 需要构造函数参数的Restlet服务器资源,java,inversion-of-control,restlet,Java,Inversion Of Control,Restlet,在restlet中获取此错误: ForwardUIApplication ; Exception while instantiating the target server resource. java.lang.InstantiationException: me.unroll.forwardui.server.ForwardUIServer$UnsubscribeForwardUIResource 我知道确切的原因。这是因为我的构造函数如下所示: public UnsubscribeFor

在restlet中获取此错误:

ForwardUIApplication ; Exception while instantiating the target server resource.
java.lang.InstantiationException: me.unroll.forwardui.server.ForwardUIServer$UnsubscribeForwardUIResource
我知道确切的原因。这是因为我的构造函数如下所示:

public UnsubscribeForwardUIResource(MySQLConnectionPool connectionPool) {
Restlet访问资源的方式如下:

router.attach(Config.unsubscribeUriPattern(), UnsubscribeForwardUIResource.class);

问题是我真的需要那个参数。我怎样才能让它方便?(注意,我没有使用任何IOC框架,只是使用了大量的ctor参数,但这实际上是一种IOC模式)。

如果你没有使用IOC,你应该手动操作,例如,你可以附加Restlet实例而不是类。您可以使用来检索属性


但是,利用IoC容器可能更有意义,它将简化它并减少样板代码,例如,这是针对Spring的:

您可以使用上下文将上下文属性传递给您的资源实例

从:

在使用默认构造函数实例化之后,调用最终的Resource.init(Context,Request,Response)方法,设置上下文,请求和响应。您可以通过重写Resource.doInit()方法来截获此消息

因此,在附件时间:

router.getContext().getAttributes().put(CONNECTION_POOL_KEY, connectionPool);
router.attach(Config.unsubscribeUriPattern(), UnsubscribeForwardUIResource.class);
在UnsubscribeForwardUIResource类中,您必须将初始化代码从构造函数移动到de doInit方法:

public UnsubscribeForwardUIResource() {
    //default constructor can be empty
}

protected void doInit() throws ResourceException {

     MySQLConnectionPool connectionPool = (MySQLConnectionPool) getContext().getAttributes().get(CONNECTION_POOL_KEY);

    // initialization code goes here
}

根据文档,不可能附加实例。哦,你说得对。您可以附加
Restlet
实例,但不能附加
ServerResource
。什么时候可以使用
Restlet
?如果没有,那么只需使用
doInit
上下文连接连接即可。您是否尝试过使用筛选器(org.restlet.routing.Filter)附加连接?