Injecting Spring managed beans in EJBs and injecting EJBs in Spring Controllers - spring-boot

I am getting NullpointerException when calling EJB
Issue relates more to ways of configuring EJBs so that they can be injected in spring Controllers and Injecting Service classes into EJBs.

Make sure all beans defined are known to spring application context
A work around to injecting beans in EJBs is to use #PostContruct, this is the only way I could get everything working but a better example of a working setup could be more appreciated.

Related

How many types of Containers are there in spring framework?

How many types of containers are there in spring framework?
In Servlets, we know containers in any web or application server. likewise, where are Spring Framework containers?
The heart of Spring (as far as DI is concerned) is the ApplicationContext. It is responsible for loading bean definitions and resolving dependencies. It will create all required beans and inject them accordingly (wire in Spring speak). It will also manage the lifecycle of the singleton beans. There can be more than one context and they can be hierarchical, such as in SpringMVC where the web context is aware of it's parent context (this is uni-directional).
There are two types of Containers for Spring.
BeanFactory Container
ApplicationContext Container
For detailed description please refer this

Does Spring Framework come with its own beans?

This text is from the book called Core Java Server Faces:
It is a historical accident that there are two separate mechanisms,
CDI beans and JSF managed beans, for beans that can be used in JSF
pages. We suggest that you use CDI beans unless your application must
work on a plain servlet runner such as Tomcat. The source code for the
book comes in two versions, one with CDI beans (for Java EE 6
application servers) and one with JSF managed beans (for servlet
runners without CDI support).
My question is:
If I use the Spring Framework, and a Tomcat Server, will I need to use one of the beans mentioned above, or does Spring Framework come with its own bean implementations?
As far as I know, Spring Framework supports Dependency Injection. Does it support it if I run the application on Tomcat? Does it mean that I will be using neither the CDI beans nor the JSF Managed means mentioned in this book?
Thank you.
talking about container is more correct than bean implementation. Yes Spring comes with its own container. In fact you can see spring frameworks as a kind of alternative to the full Java EE stack.
Using Spring DI and CDI together has about no interest but you still can use JSF with spring on tomcat although if i would advise you to switch to a Java EE 6 web profile server instead of spring in this case.
Spring comes with is own view framework implementation named spring mvc.
All of this can run perfectly on any servlet container (jetty tomcat etc...) on condition that you provide associated dependencies ofc.

Spring bean not injected in CXF Restful Service on JBoss6

I am writing a CXF Restful service with Spring on JBoss 6. The Spring bean is not injected. Instead of showing gobs of code here, I have shared a sample project here. My understanding is that the spring context is not creating the rest service class and hence the spring bean (CoreService in the project) is not injected. However the same works well in TomCat. I want to get it working on JBoss6.
Please share your thoughts and help me resolve the issue. Your help highly appreciated. Thanks.

can spring inject EJBs into annotated fields of servlet in a Java SE webapp?

Spring has support for injecting javax.ejb.EJB annotations, much like it injects #Autowired and other jsr-220 injection annotations, thanks to the CommonAnnotationBeanPostProcessor class.
However, injection doesn't work for servlets, since the servlet isn't created by spring.
This article - Spring injects servlets too - doesn't give an example using servlets, but claims it's possible using compile-time weaving of aspects. Unfortunately, compile-time weaving is not an option for us. Is it possible to do this at runtime? It's ok to introduce a subclass to the servlet if that helps, but I want to keep EJB annotations so the servlets can still deployed in a Java EE container.
EDIT: The app will be deployed to a Java EE container in production, but I was thinking of using spring for running functional tests and for local deployment for development to take advantage of hot JSP loading in Tomcat.
You will need Java EE container like in Glassfish that supports injection of EJBs and beware that injection works on managed classes like servlets, managed beans..etc (classes that are managed by containers) so ejb injection in normal classes would require you to use lookups instead.

Manage beans via Spring or via JSF

In my JSF Webapp I am using Spring vor DI etc.
At the moment I have my beans managed by Spring.
(SpringBeanFacesELResolver defined in faces-config.xml)
But I'm not sure, if this is the right way...
For example: A bean defined in Spring cannot use the new JSF 2 view scope, right?
My question is not, if I should use Spring or not... Just HOW to manage my beans?!
You should in any case stick with one DI in your project. For me, Spring is clearly much more powerful that JSF managed beans. So I would opt to Spring in this situation.
You can hanlde view scope in Spring using custom scopes, it is quite straigtforward actually. Here's a blog post on this question.

Resources