简体   繁体   English

如何将对象设置为上下文,以便我可以使用@Context在应用程序中的任何位置获取它

[英]How to set an object to context so that i can get it anywhere in the application with @Context

I want to set MyObject class instance to the application context so that I can use it anywhere with the following: 我想将MyObject类实例设置为应用程序上下文,以便可以在以下任何地方使用它:

@Context MyObject object

I used Jedis which gives me access for the jedis through the above approach. 我使用Jedis ,可以通过上述方法访问jedis

Please help in setting the context. 请帮助设置上下文。

I am using dropwizard (jetty,jersery and jackson) . 我正在使用dropwizard (jetty,jersery and jackson)

I had some time and wrote up the way to do it (jersey only, no other DI framework used). 我花了一些时间并写下了实现的方法(仅球衣,未使用其他DI框架)。

Jersey is compliant with javax.inject annotations. Jersey符合javax.inject注释。 The reason you do NOT use a context annotation is because (by the sound of it) your MyObject class is not a context object (eg it doesn't change with each request like eg HttpServletRequest which is injectable). 您不使用上下文注释的原因是(从它的声音来看)您的MyObject类不是上下文对象(例如,它不会随着可注入的HttpServletRequest之类的每个请求而改变)。 So we need to bind your object. 因此,我们需要绑定您的对象。

Consider my implementation of MyObject: 考虑一下我的MyObject实现:

public class MyObject {

    String get() {
        return "I am an object"; 
    }
}

This object needs to be available in my jersey classes (resource, filter etc). 该对象需要在我的球衣类中可用(资源,过滤器等)。 I wrote a little resource using this bean: 我使用此bean编写了一些资源:

@Path("context")
public class ContextResource {

    @Inject
    MyObject o;

    @GET
    public String get() {
        return o.get();
    }

}

Note that I am using the javax.inject.Inject annotation for this case to tell jersey I want this particular bean injected. 请注意,在这种情况下,我使用javax.inject.Inject批注来告诉jersey我想要注入此特定的bean。 All I need to do now is to tell jersey about this bean. 我现在需要做的就是告诉球衣这个豆子。 In my DW application I do: 在我的DW应用程序中,我这样做:

public class Application extends io.dropwizard.Application<Configuration>{  

    @Override
    public void run(Configuration configuration, Environment environment) throws Exception {

        environment.jersey().register(ContextResource.class);
        environment.jersey().register(new AbstractBinder() {

            @Override
            protected void configure() {
                bind(MyObject.class).to(MyObject.class);
            }
        });
    }

    public static void main(String[] args) throws Exception {
        new Application().run("server", "/home/artur/dev/repo/sandbox/src/main/resources/config/test.yaml");
    }
}

Note that I am using a binder to bind my bean. 请注意,我使用的是绑定器来绑定我的bean。 The syntax looks funky, but essentially it is doing a "bind the type to the implementation". 该语法看起来很时髦,但实际上它是在“将类型绑定到实现”。 Since my type is my implementation (I am not using an interface for MyObject), this looks like: 由于我的类型是我的实现(我没有为MyObject使用接口),因此如下所示:

bind(MyObject.class).to(MyObject.class) 

Now jersey knows about my bean and will happily inject it. 现在,球衣知道我的豆子,会很高兴地注入它。

Running all my code prints: 运行我所有的代码打印:

artur@pandaadb:~/dev/vpn$ curl localhost:9085/api/context
I am an object

Hope that brings some insights on how to use injection without a framework. 希望能带来一些有关如何在没有框架的情况下使用注入的见解。 Personally I would recommend using guice with dropwizard (google: dropwizard-guicey) which makes these kind of things very easy. 我个人建议将guice与dropwizard(google:dropwizard-guicey)结合使用,这会使这类事情变得非常容易。

Regards, 问候,

Artur 阿图尔

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

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