Search code examples
springejbrepositoryautowiredinject

Spring: Autowired is null in ejb class


I have the following situation:

    @Controller
    public class myController {
        @Autowired
        private IProxy service;

        public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
          List<String> list = service.getName();
        }
    } 

Then my Service is define as follow:

public interface IProxy {
    public List<String> getName();
}

Proxy class is responsible for the lookup to the remote bean

@Service("service")
public class Proxy implements IProxy {
...
public List<String> getName() {
   return myClass.getName();
}

And the implementation is the following:

@Interceptors(interceptor.class)
@Stateless
@Resource(name = "java:/db")
@Remote(MyClassRemote.class)
public class MyClassImpl extends MyEjb implements MyClassRemote{

    @PersistenceContext(unitName = "db")
    private EntityManager em;
    @Resource
    private SessionContext sctx;
    @Autowired
    public IMyRepo myRepo;

    @Override
    public List<String> getName() {
        try {
             return myRepo.getName(em);
        }
        catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
        finally {}
    }

So, the problem is that here myRepo is null. I don't know why because IMyRepo and his implementation are always located within the path scanned by Spring.

Just one clarification: MyRepo class that implements IMyRepo is annotated with @Repository.

Any idea?


Solution

  • Spring beans and EJB are two different things, you can't just inject a Spring bean in an EJB, because that EJB is no Spring bean, so Spring doesn't know there is a field which should be injected by Spring (unless you use some fancy AOP stuff, which can enable injection into non-Spring-managed beans).