Answer the question
In order to leave comments, you need to log in
Why isn't the Factory.dispose() method called?
I am taking the first steps in learning the jersey framework and java ee just in general.
There was a problem implementing custom injection7
Annotation:
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
public Interface SessionInjection {
}
Factory:
public class HibernateSessionFactory extends AbstractContainerRequestValueFactory {
private SessionFactory mSessionFactory;
public HibernateSessionFactory() { ... }
@Override
public Session provide() {
return mSessionFactory.openSession();
}
@Override
public void dispose(Session session) {
session.close();
}
}
Provider:
public class HibernateSessionFactoryProvider extends AbstractValueFactoryProvider {
@Inject
public HibernateSessionFactoryProvider(
MultivaluedParameterExtractorProvider mpep, ServiceLocator injector) {
super(mpep, injector, Parameter.Source.UNKNOWN);
}
@Override
protected Factory<?> createValueFactory(Parameter parameter) {
Class<?> classType = parameter.getRawType();
if (classType == null || (!classType.equals(Session.class))) return null;
return new HibernateSessionFactory();
}
Resolver:
public class HibernateSessionInjectResolver extends ParamInjectionResolver {
public HibernateSessionInjectResolver() {
super(HibernateSessionFactoryProvider.class);
}
}
Application class:
public class MyApplication extends ResourceConfig {
public MyApplication() {
super();
register(RolesAllowedDynamicFeature.class);
register(new AbstractBinder() {
@Override
protected void configure() {
//bindFactory(HibernateSessionFactory.class).to(Session.class).in(RequestScoped.class);
bind(HibernateSessionFactoryProvider.class).to(ValueFactoryProvider.class).in(RequestScoped.class);
bind(HibernateSessionInjectResolver.class)
.to(new TypeLiteral>() {})
.in(Singleton.class);
}
});
}
}
I use it like this:
@Path("v1/")
@Stateless
public class V1 {
@Inject
TestClass test;
@GET
@Path("/say_hello")
public String hello(@SessionInjection Session session) {
return "Hello!!!";
}
}
The problem is that after the method is executed, the despose() factory method is not called.
How to make the framework call it?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question