我正在使用Jersey 2开发REST API,我需要在启动时实例化一些类,而不仅仅是在某些资源请求触发它时.
所以我要问的是:我如何实现SomethingImpl
下面定义的实例是在服务器启动时创建的,而不仅仅是当有人点击某些资源时?在Guice我会用.asEagerSingleton()
.
应用:
public class MyApplication extends ResourceConfig {
public MyApplication() {
register(new AbstractBinder() {
@Override
protected void configure() {
bind(" else").to(String.class);
bind(SomethingImpl.class).to(Something.class).in(Singleton.class);
}
});
register(SomeResource.class);
}
}
Run Code Online (Sandbox Code Playgroud)
东西:
public interface Something {
String something();
}
public class SomethingImpl implements Something {
@Inject
public SomethingImpl(final String something) {
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println(something() + something);
try {
Thread.sleep(4000);
} catch (final InterruptedException e) {
break;
} …
Run Code Online (Sandbox Code Playgroud) 如何设置aop MethodInterceptor
以使用Jersey资源?
以下是我已经试过,以下这个文件:
第1步 - InterceptionService
public class MyInterceptionService implements InterceptionService
{
private final Provider<AuthFilter> authFilterProvider;
@Inject
public HK2MethodInterceptionService(Provider<AuthFilter> authFilterProvider)
{
this.authFilterProvider = authFilterProvider;
}
/**
* Match any class.
*/
@Override
public Filter getDescriptorFilter()
{
return BuilderHelper.allFilter();
}
/**
* Intercept all Jersey resource methods for security.
*/
@Override
@Nullable
public List<MethodInterceptor> getMethodInterceptors(final Method method)
{
// don't intercept methods with PermitAll
if (method.isAnnotationPresent(PermitAll.class))
{
return null;
}
return Collections.singletonList(new MethodInterceptor()
{
@Override
public Object invoke(MethodInvocation …
Run Code Online (Sandbox Code Playgroud) 跟我上一个问题的后续行动.我正在尝试使用JSR-330标准注释和与泽西捆绑的HK2框架注入应用程序配置数据.
理想情况下,我想InjectionResolver
为Named
注释创建一个自定义,它将在一个Map
或Properties
对象中查找所需的值,我将从其他地方读取的数据中填充.在我的第一次尝试中,我创建了一个Application
像
public class MyApplication extends ResourceConfig {
...
packages(MY_PACKAGES);
property(MY_CONFIG_PROPERTY, someValue);
register(new AbstractBinder() {
@Override
protected void configure() {
bind(ConfigurationInjectionResolver.class)
.to(new TypeLiteral<InjectionResolver<Named>>(){})
.in(Singleton.class)
}
});
}
Run Code Online (Sandbox Code Playgroud)
然后我的InjectionResolver
样子
public class ConfigurationInjectionResolver implements InjectionResolver<Named> {
@Context Application application;
@Override
public Object resolve(Injectee injectee, ServiceHandle<?> serviceHandle) {
// lookup data in application.getProperties();
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题application.getProperties()
是空的.知道什么是错的吗?另外,我可以绑定我的Injector实例而不是绑定类吗?这样我就可以构造将我的Map
数据作为参数传递的实例.
我有一个与此处提出的类似的问题:如何让我的Jersey 2端点在启动时急切地初始化?
但稍微下线了.我可以立即加载我的资源,但是当我尝试通过调用REST URL来使用它时,我得到以下堆栈跟踪.
java.lang.IllegalStateException: Could not find an active context for org.glassfish.hk2.api.Immediate
2. java.lang.IllegalStateException: While attempting to create a service for
SystemDescriptor(
implementation=com.service.MyResource
contracts={com.service.MyResource}
scope=org.glassfish.hk2.api.Immediate
qualifiers={}
descriptorType=CLASS
descriptorVisibility=NORMAL
metadata=
rank=0
loader=null
proxiable=null
proxyForSameScope=null
analysisName=null
id=150
locatorId=0
identityHashCode=1249600275
reified=true) in scope org.glassfish.hk2.api.Immediate an error occured while locating the context
Run Code Online (Sandbox Code Playgroud)
我的TResource类因此被注释:
@Immediate
@Path("/test/v1")
public class TResource {
Run Code Online (Sandbox Code Playgroud)
我的基于Grizzly的服务器设置如下:
ResourceConfig rc = new ResourceConfig()
.packages(true,
"com.mystuff"
)
.property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, "true");
HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(URI.create(base_uri), rc);
ApplicationHandler handler = new ApplicationHandler(rc);
ServiceLocatorUtilities.enableImmediateScope(handler.getServiceLocator());
Run Code Online (Sandbox Code Playgroud)
任何指导将非常感谢!欢呼,菲尔
在使用由依赖注入框架(比如HK2)管理的服务时,使用方法链是否可以接受?
我不确定是否允许"缓存"实例,即使它仅在注入范围内.
示例服务创建披萨:
@Service
public class PizzaService {
private boolean peperoni = false;
private boolean cheese = false;
private boolean bacon = false;
public PizzaService withPeperoni() {
peperoni = true;
return this;
}
public PizzaService withCheese() {
cheese = true;
return this;
}
public PizzaService withBacon() {
bacon = true;
return this;
}
public Pizza bake() {
// create the instance and return it
}
}
Run Code Online (Sandbox Code Playgroud)
这里将服务注入JAX-RS资源:
@Path('pizza')
public class PizzaResource {
@Inject
PizzaService pizzaService;
@GET
public Response …
Run Code Online (Sandbox Code Playgroud) 当前,我们使用以下框架开发了jersey 2.0 restful应用程序:
该应用程序可在tomcat 8.5
&Weblogic 12.1.x
服务器上完美运行
但是,当我们尝试将其部署到 Weblogic 12.2.1.3
<Oct 6, 2017 10:32:23,020 AM HKT> <Error> <Deployer> <BEA-149265> <Failure occurred in the execution of deployment request with ID "214569275552728" for task "1" on [partition-name: DOMAIN]. Error is: "weblogic.application.ModuleException: org.glassfish.jersey.internal.ServiceConfigurationError: org.glassfish.jersey.server.spi.ComponentProvider: The class org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider implementing the provider interface org.glassfish.jersey.server.spi.ComponentProvider is not found. The provider implementation is ignored."
weblogic.application.ModuleException: org.glassfish.jersey.internal.ServiceConfigurationError: org.glassfish.jersey.server.spi.ComponentProvider: The class org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider implementing the provider interface org.glassfish.jersey.server.spi.ComponentProvider is not found. The provider implementation …
Run Code Online (Sandbox Code Playgroud) 由于迁移到 jersey 2,我需要从 guice 迁移到 HK2。我有一个针对我的一些依赖项的辅助注入方法,但我无法在 HK2 中实现它。看起来应该通过自定义注入解析器来解决,但我真的不知道如何解决。这些例子对我来说还不够清楚..
Guice 上的外观如下:
public interface MyFactory {
public MyClass createMyClass(@Assisted String dynamicParameter);
public HisClass createHisClass(@Assisted String dynamicParameter);
...
}
binder.install(new FactoryModuleBuilder().build(MyFactory.class));
public class MyClass {
...
@Inject
public MyClass(@Assisted String dynamicParameter, SomeService someOtherServiceInjectedAutomatically){
...
}
}
Run Code Online (Sandbox Code Playgroud)
我如何在 HK2 上实施此操作?
我试图按照此处的示例创建一个工厂,以便注入我的 HttpSession。不幸的是,无论我尝试什么,它都不起作用。不确定可能是什么问题。
我尝试仅注入 HttpServletRequest 和提供程序。这是我使用提供商的示例。该错误是尝试在提供方法中访问提供程序时出现空指针异常。如果我尝试注入 HttpServletRequest,则没有可用于注入的对象。我使用 JerseyTest 在 GrizzlyTestContainer 内运行它。为了绑定 HttpServletRequest,我需要添加一些东西到我的活页夹中吗?我似乎找不到例子。
public class HttpSessionFactory implements Factory<HttpSession> {
private final HttpServletRequest request;
@Inject
public HttpSessionFactory(Provider<HttpServletRequest> requestProvider) {
this.request = requestProvider.get();
}
@Override
public HttpSession provide() {
return request.getSession();
}
@Override
public void dispose(HttpSession t) {
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用 jersey 在不同位置注入 POJO。这是我的配置:
register(new AbstractBinder() {
@Override
protected void configure() {
bind(Bar.class).to(Bar.class).in(Singleton.class);
bindFactory(FooFactory.class).to(Foo.class).in(Singleton.class);
...
}
});
Run Code Online (Sandbox Code Playgroud)
福工厂:
public class FooFactory implements Factory<Foo> {
@Override
public Foo provide() {
return Foo.newInstance();
}
}
Run Code Online (Sandbox Code Playgroud)
注入资源的工作原理:
@Path("/myresource")
public class MyResource{
@Inject
protected Bar instance;
}
Run Code Online (Sandbox Code Playgroud)
但
public class Foo {
@Inject
protected Bar instance;
}
Run Code Online (Sandbox Code Playgroud)
才不是。Foo.instance
一片空白。为什么?以及如何让它发挥作用?
我正在努力在 J2EE jersey 项目中实现构造函数的依赖注入。我正在使用HK2。我创建一个类
class MyServiceImpl implements MyService{
@Inject
public MyServiceImpl(String test){
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我的问题是,当我通过扩展 AbstractBinder 在dependencybinder 类中注册此依赖项注入时,将依赖项绑定为简单的“bind”与“bindAsContract”之间有什么区别?
hk2 ×10
java ×8
jersey-2.0 ×6
jersey ×4
aop ×1
bind ×1
dependencies ×1
guice ×1
inject ×1
java-7 ×1
rest ×1
weblogic12c ×1