在业务层中使用@Context注释访问HttpServletRequest [英] Access HttpServletRequest using @Context annotaion in business layer

查看:151
本文介绍了在业务层中使用@Context注释访问HttpServletRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在我的rest服务中使用@Context注释来访问HttpServletRequest.但是无法访问存储库类中的相同对象.我不想在调用方法时将请求形式MyService传递给MyRespository.

I am able to access HttpServletRequest by using @Context annotation in my rest service. But unable to access the same in repository class.I do not want to pass the request form MyService to MyRespository while calling methods.

@Path("/someUrl")
public MyService{

@Context
private HttpServletRequest request;

@Get
public void someMethod()
{
   myRepository.someMethod();
}

}

但是相同的注释不适用于我的Repository类

But same annotation not working for my Repository class

@Repository
public MyRepository
{

@Context
private HttpServletRequest request;

public void someMethod()
{
 //need request here
}
}

它注入空请求.不知道为什么这不起作用.

it injection null request. Not sure why this is not working.

推荐答案

问题在于Jersey(及其DIem HK2框架)的集成方式是,可以将Spring组件注入Jersey( HK2)组件,反之亦然. HttpServletRequest被绑定为Jersey组件.

The problem is the way Jersey (and its DI framework HK2) is integrated, is that Spring components can be injected into Jersey (HK2) components, but not vice versa. HttpServletRequest is bound as a Jersey component.

您可以做的是创建一个HK2服务,该服务包装Spring回购和HttpServletRequest. IMO,无论如何,这是更好的设计.存储库不应与HttpServletRequest有关,而仅与数据有关.

What you can do is create an HK2 service, that wraps the Spring repo, and the HttpServletRequest. IMO, it is better design anyway. A repository shouldn't be concerned with the HttpServletRequest, it is only concerned with data.

所以你可以拥有

public class MyService {

    @Inject // or @Autowired (both work)
    private MyRepository repository;

    @Context
    private HttpServletRequest request;

}

然后将服务与HK2绑定

Then bind the service with HK2

import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.process.internal.RequestScoped;

public class AppBinder extends AbstractBinder {

    @Override
    public void configure() {
        bindAsContract(MyService.class).in(RequestScoped.class);
        // note, if `MyService` is an interface, and you have 
        // an implementation, you should use the syntax
        //
        // bind(MyServiceImpl.class).to(MyService.class).in(...);
        //
        // Then you inject `MyService`. Whatever the `to(..)` is, 
        // that is what you can inject
    }
}

并在泽西岛注册活页夹

public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        register(new AppBinder());
    }
}

然后,您可以将MyService注入资源类.

Then you can inject MyService into your resource class.

如果您不想走这条路线,则需要将MyRepository设为HK2服务,或使用HK2 Factory包装存储库并显式注入它.像

If you don't want to go this route, then you need to make MyRepository an HK2 service, or use a an HK2 Factory to wrap the repository, and explicitly inject it. Something like

import javax.inject.Inject;
import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.api.ServiceLocator;
import org.springframework.context.ApplicationContext;

public class MyRepositoryFactory implements Factory<MyRepository> {

    private final MyRepository repo;

    @Inject
    public MyRepositoryFactory(ApplicationContext ctx, ServiceLocator locator) {
        MyRepository r = ctx.getBean(MyRepository.class);
        locator.inject(r);
        this.repo = r;
    }


    @Override
    public MyRepository provide() {
        return repo;
    }

    @Override
    public void dispose(MyRepository t) {/* noop */}
}

然后注册工厂

@Override
public void configure() {
    bindFactory(MyRepositoryFactory.class).to(MyRepository.class).in(Singleton.class);
}

如果执行上述操作,则只需使用MyRepository,而不添加服务层.基本上,您需要从Spring获取回购,并显式注入HK2 ServiceLocator(它是Spring ApplicationContext的HK2类似物).

If you do the above, then you just use the MyRepository, instead of adding the service layer. Basically you need to get the repo from Spring, and explicitly inject it with the HK2 ServiceLocator (which is the HK2 analogue of the Spring ApplicationContext).

这篇关于在业务层中使用@Context注释访问HttpServletRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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