具有所需构造函数参数的Restlet服务器资源 [英] Restlet server resource with constructor parameters needed

查看:174
本文介绍了具有所需构造函数参数的Restlet服务器资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在restlet中出现此错误:

Getting this error in restlet:

ForwardUIApplication ; Exception while instantiating the target server resource.
java.lang.InstantiationException: me.unroll.forwardui.server.ForwardUIServer$UnsubscribeForwardUIResource

而且我确切知道为什么。这是因为我的构造函数看起来像这样:

And I know exactly why. It's because my constructor looks like this:

public UnsubscribeForwardUIResource(MySQLConnectionPool connectionPool) {

并且Restlet像这样访问资源:

And Restlet accesses the resource like so:

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

问题是我实际上需要那个ctor参数。我怎样才能访问它? (注意我没有使用任何IOC框架,只有很多ctor参数,但这实际上是一个IOC模式)。

Problem is I actually need that ctor argument. How can I make it accessible? (Note I'm not using any IOC framework, just lots of ctor arguments but this is in fact an IOC pattern).

推荐答案

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

You can use the context to pass context atributes to your resource instance.

来自ServerResource API doc


实例化后使用默认构造函数,调用最终的Resource.init(Context,Request,Response)方法,设置上下文,请求和响应。您可以通过覆盖Resource.doInit()方法来拦截它。

After instantiation using the default constructor, the final Resource.init(Context, Request, Response) method is invoked, setting the context, request and response. You can intercept this by overriding the Resource.doInit() method.

因此,在附件时间:

router.getContext().getAttributes().put(CONNECTION_POOL_KEY, connectionPool);
router.attach(Config.unsubscribeUriPattern(), UnsubscribeForwardUIResource.class);

在UnsubscribeForwardUIResource类中,您必须将初始化代码从构造函数移动到de doInit方法:

At your UnsubscribeForwardUIResource class you'll have to move the initialization code from the constructor to de doInit method:

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服务器资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆