加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 使用构造函数所需的Restlet服务器资源

发布时间:2020-12-14 16:27:17 所属栏目:Java 来源:网络整理
导读:在restlet中获取此错误: ForwardUIApplication ; Exception while instantiating the target server resource.java.lang.InstantiationException: me.unroll.forwardui.server.ForwardUIServer$UnsubscribeForwardUIResource 我知道为什么.这是因为我的构造
在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);

问题是我真的需要这个ctor参数.如何使其可访问? (注意,我没有使用任何IOC框架,只是很多ctor参数,但实际上这是一个IOC模式).

解决方法

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

从ServerResource API doc:

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方法:

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
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读