Jeenson Ephraim
Jeenson Ephraim

Reputation: 571

CDI -- @Inject not working among dependent projects

The basic architecture of my project (JBoss MSC version 1.0.2.GA-redhat-2) is like this

(VWebProj) --- >compile dependency ---> service project (QServiceProj) 

 (QServiceProj) ---->compile dependency ---> proxy project(VProxyProj)

 (VProxyProj) ---->compile dependency ---> Manager project(VQManagerProj)

Manager project(VQManagerProj)

I have a manger class inside a Manager Project (VQManagerProj) which extends a class JDAO which in turn implements a interface VDAO

@Named("qManager")
@ApplicationScoped
public class QManager extends JDAO {...}

JDAO implements VDAO

Proxy Project (VProxyProj)

I have a proxy class inside Proxy Project (VProxyProj) which implements an interface VProxy and has the manager injected to it

@Named("vProxyImpl")
@ApplicationScoped
public class VProxyImpl implements VProxy {

    @Inject @Named("qManager")  
    private VDao vdao;

}

Sevice Project (QServiceProj)

I have a service class inside Sevice Project (QServiceProj) which extends an abstract class

@Named
@ApplicationScoped
public class QService extends AbstractService {..}

Inside the abstract class I have the proxy injected

 public abstract class AbstractService{    
     @Inject @Named("vProxyImpl")
        private static VProxy proxy;
    }

and using this proxy object the service class makes calls to the manager etc

Web Project (VWebProj)

Now I have a servlet inside the web Project (VWebProj) in which the service class is injected

@Inject
private QService qService;

The problem is that except qService none of the other injections work i.e inside QService proxy instance is null

however if I add all the injections directly in the servlet class like this

@Inject @Named("qManager")  
        private VDao vdao;

@Inject @Named("vProxyImpl")
            private static VProxy proxy;

They are all initialized but if I go via QService they are null

I have put beans.xml in all the projects,

Thanking in advance Charlie

Upvotes: 0

Views: 457

Answers (1)

Mateusz Kubuszok
Mateusz Kubuszok

Reputation: 27535

As far as I know Injector can only inject objects into instances and their fields - You are trying to "inject" dependencies into static fields.

I suggest using @Singleton annotation instead - create separate instance that would hold all your current static references, and inject that singletons into Your instances instead.

@Singleton
class ProxyService {
     @Inject @Named("vProxyImpl")
     private VProxy proxy;

     public VProxy getProxy() {
         return proxy;
     }
}

public abstract class AbstractService{    
     @Inject
     private ProxyService proxyService;
}

Alternatively You can consider making VProxy singleton - I seems that what You want to obtain is just one instance of Proxy in a whole application. You need to decide yourself what is that best approach here.

Upvotes: 1

Related Questions