Spring Context in an EJB Module - java

I'm currently in the making of an EJB Module, which could not be complete without persistence.
My initial instinct led me to use Spring Boot, since it has some wild implementations of how one could use JPA and to be honest, I'm kinda spoiled.
However, I couldn't really get Spring up and running in my EJB Module. I created the usual class with the public static void main(String[]) method, but my fear, that this method never runs became reality and using a #PostConstruct method to initialize Spring sounds ridiculous.
How can I initialize Spring Boot on an EJB module? Should I make another module and somehow refer to that from my EJBs? How can I do that? Is it something that people actually do, or should I just get my lazy back-side down in front of the computer and learn "proper" JPA?
Thanks in advance

You will have to use SpringBootServletInitializer to get Spring online inside container.
https://www.baeldung.com/spring-boot-war-tomcat-deploy

Related

Can we put #postconstruct annotation in osgi service?

I know that #postconstruct annotation is used when we want a method to call at the time of creation of its object after constructor. But my question is can we use it inside OSGI service?
It depends on the dependency injection framework you use. If you use declarative services the equivalent would be #Activate. A good example is the enroute microservice example.
If you use Aries CDI then indeed #PostConstruct works. Unfortunately there are not yet good examples for this as it is still quite new but it works for the most part like normal CDI in Java EE.

Have one Tomcat application get a bean from another one

Currently I have a Spring project that uses Spring JPA to work with data objects and my database. All of it's functionality can be accessed by calling a single facade bean. I want to make a web application based on this data model, but I don't want to extend this project any further. Instead I would like to separate my persistence and service layers from an actual web application layer. That said, I want to package this project into a ".war" file and deploy it on my Tomcat instance. Upon demand from any other application working on Tomcat I would like to have this other application to be injected with the facade bean from my ".war".
I'm sort of new to Tomcat, and googling doesn't really help me much. So here are problems and questions that I have with this concept:
Is this the right way to do what I want? What I mean is I want behavior provided by my current Spring application to be accessible and reusable by different web applications working on one server. This might be a common case and I would like to know if this is ok as a solution.
If I have this facade bean in XML context or in annotation context of my project, how can I make this bean visible to any other application working on the same Tomcat instance upon being deployed? What should I write in my web applications to have them wired with this bean? If I want this bean to be a singleton and have all calls to it's functionality synchronized, should I do this through my code/context, or can I have Tomcat somehow take care of this for me?
Thanks in advance.
You might want to consider a REST API approach. You can't do "cross application injection" with Spring and JNDI can be cumbersome to use.
Webapps (the things that run in a servlet container like tomcat) are isolated from each other, by design. Sharing may well be a bad idea. However, to share, you can use JNDI. Setting up JNDI in tomcat 7 is described here. You will need a custom resource factory.

Having a common SpringApplicationContextProvider to be used by all bean spring-managed and otherwise

I have an ApplicationContextProvider class, which can used to access Spring ApplicationContext from beans not managed by Spring. Something like mentioned here
For the spring managed beans however I can make them ApplicationContextAware, so they can get access to ApplicationContext.
My question is, is it a good idea to use the common ApplicationContextProvider to get spring application context from the spring-managed beans as well or should I continue to use ApplicationContextAware?
Using a singleton with static reference to application context is asking for trouble. It is almost never a good idea to use such utility class in any case. When dealing with legacy web application use WebApplicationContextUtils instead.
I once reinvented this solution in a project during migration from EJB 2.1 to Spring - but after migration we get rid of it and could sleep again.
Back to your question - using such utility for Spring managed beans has no sense at all. To be honest, I rarely need ApplicationContextAware interface as well. Spring is suppose to inject dependencies, you shouldn't ask Spring for them all the time!
Can you show us some use case when you need quoted utility class (both for managed- and umanaged beans)? Looks like you are refusing to accept support Spring gives you.

What does application context in Spring do?

I asked a question yesterday ( Using Spring in standalone apps ) on how you would use Spring in a standalone application. From that I learned that you only create the application context object once. So now the question is (even though it was partially answered in a comment) what happens when you create the application context?
Does Spring create the beans and wire them together when you say
new ClassPathXmlApplicationContext("some.xml") ?
I am not sure if I understand the boot strapping, and why it is like that.
The idea behind the ApplicationContext in Spring is that in order to properly inject objects where they are needed, some thing needs to be aware of the configuration the user specifies and inject dependencies based on this configuration.
The ApplicationContext is the thing that understands the user's wishes in terms of where and what should be injected (as well as other things such as AOP pointcuts and such) based on the configuration a user provides, either through an xml file or annotations.
Yes it will parse the bean definition file , it will create the beans , give them the dependencies,
The easiest way to debug is to go with the output print statements,
Put the statements in constructor & setter methods and try different possibilities to track the flow

java web applicaton layout, please explain some design principles/patterns

I'm looking at this java web application that is using hibernate, jsp's, and spring framework. (from what I can tell!)
the file layout is like this:
classes/com/example/project1
inside project1
/dao
_entity_Dao.java
/dao/hibernate/
_entity_DaoHibernate.java
/factory
DaoFactory.java
DaoFactoryImpl.java
/managers
_entity_Manager.java
/managers/impl
_entity_ManagerImpl.java
/model
_entity_.java
/service
_xxxx_Service.java
/service/impl/
_xxxx_ServiceImpl.java
Have you guys read about this sort of layout somewhere? Is it considered best-practice?
What is the difference between a Factory and a Manager and a Service? (high level)
For typical layout of an application built with Spring I'd look at the example web applications that ship with it (meaning Spring).
Using things like DaoFactory is definitely not be a best-practice, the Daos should get injected instead. In general you should not need factories with Spring except for some unusual cases. Injecting is done when the web application starts up, spring reads the configuration information and constructs all the objects and plugs them in according to configuration xml and/or annotations (this is assuming singleton-scope for your objects, which is usual for stateless things like daos and services, things scoped as prototypes get new copies created as the application asks for them).
In Spring applications a service is similar to a Stateless Session Bean, it is a transactional layer encompassing application logic for a use case. So if your user takes an action that has the effect of causing several different tables to get updated you can inject the daos into that service, have a method on that service do the updates on the daos, and configure Spring to wrap that service in a proxy that makes that method transactional.
I've seen manager used as another name for what I described as service. Here I don't know what they're doing with it.
I don't like the idea of combining your interfaces and impls in one project. Just because you want to consume the interface doesn't mean you want to consume the impl and it's cumbersome transitive dependencies. The main reason is because there will be more than one impl (hypothetically, i.e. JPA/JDBC/Hibernate, or Axis2/CXF, etc.). The interfaces should not be bound to the implementation, otherwise the point is lost. This also allows for easy dependency injection as the impls simply reside on the classpath, then something like a Proxy or Spring (e.g.) can inject the implementations.
In all likelihood, all you need is a:
Interface Project
dao
EntityDao
types
Entity
HibernateImpl Project
dao
EntityHibernateDao
src/main/resources/
EntityMapping.cfg.xml

Categories

Resources