0

I am trying to inject a Singleton bean into my Request scoped JAX-RS resource. What I notice is that my Singleton's constructor is invoked on every request, which is obviously not the desired behavior. My Code artifacts are as below:-

JAX-RS Resource

@Path("/thing")
@ManagedBean
public class ThingResource {

    //This is desired to be Singleton
    @Inject
    @RedisThingDb
    private ThingDb thingDb;

    //This is request scoped
    @Inject
    private MyRequestScopedObj obj;

    @GET
    @Path("/{id}")
    @Produces("application/json")
    public Thing getById(@PathParam("id") String thingId) {
        return thingDb.findById(thingId);
    }
}

@RedisThingDb Annotation that marks it as a Singleton

@Qualifier
@Singleton
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER     })
public @interface RedisThingDb {

}

Producer Code that creates the RedisDb object.

@Produces
@RedisThingDb
public ThingDb getRedisDb() {
    return new RedisDb();// This should be called once, because @RedisThingDb is singleton.
}

RedisDb

public class RedisDb implements ThingDb {
    public RedisDb() {
    logger.debug("Constructing...");
    }

    public Thing findById(Long id){
     ...
    }
}

Dependency from pom.xml (Implementation is Wildfly container)

    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>

I am using javax.annotation.ManagedBean (@ManagedBean) annotation to turn the JAX-RS into a CDI managed bean. The @RedisThingDb annotation is marked as @Singleton. The JAX-RS resource is request scoped and has other request scoped beans injected into it.

VDev
  • 2,067
  • 5
  • 23
  • 27
  • Can you specify what version of JAX-RS/Java EE you're using? – John Ament Aug 04 '14 at 16:52
  • @JohnAment my maven dependency is as follows:- javax javaee-api 7.0 provided I am running on WildFly – VDev Aug 04 '14 at 19:24
  • @JohnAment sorry could not figure out how to format my comment correctly. I am using javax.javaee-api 7.0 scope is provided and on Wildfly application container – VDev Aug 04 '14 at 19:29
  • Great thanks. would you also be able to post your `beans.xml` in the question as well? – John Ament Aug 04 '14 at 23:50

2 Answers2

0

There are two issues I see with your code. For one, you're using @RedisThingDB as a @Stereotype but it's not annotated @Stereotype. You should ditch the @Qualifier on it as well.

Second, which is more of a recommendation, is that you should annotate your endpoint with a scope. RESTEasy will add a scope for you, @RequestScoped.

Third, it's completely ok for the constructor to be called multiple times. The real thing to watch is if your @PostConstruct method is repeatedly invoked. The constructor gets invoked since CDI uses proxies. If that still doesn't help - try using @ApplicationScoped rather than @Singleton.

John Ament
  • 10,860
  • 1
  • 29
  • 44
-1

@ManagedBean is from JSF and not related to CDI http://docs.oracle.com/javaee/6/api/javax/faces/bean/ManagedBean.html

replace it with @Named

http://docs.oracle.com/javaee/6/api/javax/inject/Named.html

Alex Chernyshev
  • 1,551
  • 8
  • 8