Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PreDestroy method not being called in binded service classes #655

Open
jmihalich opened this issue Apr 21, 2022 · 1 comment
Open

PreDestroy method not being called in binded service classes #655

jmihalich opened this issue Apr 21, 2022 · 1 comment

Comments

@jmihalich
Copy link

jmihalich commented Apr 21, 2022

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");

    //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.

`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);

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

@jmihalich
Copy link
Author

I was able to get PreDestroy to be called by changing the bind line to this:

bind(TestService.class).to(TestService.class).in(Singleton.class);

When i define it as singleton scope preDestroy is called. Not sure why it's not called in other scoped modes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant