You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First my set up is: Java 17, Jetty 11, and Jersey 3.0.4, and jersey-hk2. This is about as bare bones of a setup as you can get right now. My project has 2 classes in it, a resource class that injects a service class, and the service class itself. No other DI frameworks (like spring or guice).
I have a webapp deployed in Jetty using Jersey 2. I was unable to get the "auto scanning" to work (this is another issue i will open), so i wound up manually registering my service classes in the jersey application class as follows:
`
//setup packages where services live
packages(true, "com.mypackage.services");
//load DI container
register(new AbstractBinder() {
@Override
protected void configure() {
//register service classes here
bind(TestService.class).to(TestService.class);
}
});`
In my TestService class I have the @service annotation on it, and it implements the PostConstruct and PreDestroy HK2 interfaces. The postConstruct method is called correctly. However, the preDestroy method is never called.
I should mention that implementing PostConstruct and PreDestroy doesn't seem to be triggering anything cuz if I remove those from the implements clause, postConstruct still gets called.
@service
public class TestService implements PostConstruct, PreDestroy
{
private static final Logger LOGGER = LoggerFactory.getLogger(TestService.class);
public TestService()
{
}
public String getValue()
{
return "got value";
}
public void postConstruct()
{
LOGGER.info("postConstruct");
}
public void preDestroy()
{
LOGGER.info("preDestroy:");
}
}
`
Also I tried implementing an interface as a @contract and having the concrete TestService class implement that, and then have the bind be: bind(TestService.class).to(TestInterface.class) but that no effect.
Am I missing something? Or am I doing something wrong?
Thanks,
Joe
The text was updated successfully, but these errors were encountered:
Hi,
First my set up is: Java 17, Jetty 11, and Jersey 3.0.4, and jersey-hk2. This is about as bare bones of a setup as you can get right now. My project has 2 classes in it, a resource class that injects a service class, and the service class itself. No other DI frameworks (like spring or guice).
I have a webapp deployed in Jetty using Jersey 2. I was unable to get the "auto scanning" to work (this is another issue i will open), so i wound up manually registering my service classes in the jersey application class as follows:
`
//setup packages where services live
packages(true, "com.mypackage.services");
In my TestService class I have the @service annotation on it, and it implements the PostConstruct and PreDestroy HK2 interfaces. The postConstruct method is called correctly. However, the preDestroy method is never called.
I should mention that implementing PostConstruct and PreDestroy doesn't seem to be triggering anything cuz if I remove those from the implements clause, postConstruct still gets called.
`TestService class:
import org.jvnet.hk2.annotations.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@service
public class TestService implements PostConstruct, PreDestroy
{
private static final Logger LOGGER = LoggerFactory.getLogger(TestService.class);
}
`
Also I tried implementing an interface as a @contract and having the concrete TestService class implement that, and then have the bind be: bind(TestService.class).to(TestInterface.class) but that no effect.
Am I missing something? Or am I doing something wrong?
Thanks,
Joe
The text was updated successfully, but these errors were encountered: