How to inject spring beans from external war? - java

I'd like to create a decoupled frontend (vaadin) and backend (spring). Both should run on the same tomcat application server, but each is a single war so I can redeploy the frontend without having to restart the backend.
I want to minimize the remoting code between both applications to exchange data. Therefore I thought I could maybe inject the service beans from backend into the frontend war. But it that possible? How would I share the service declaration between both war files / java projects?
And what type of remoting would be apropriate here? Ideally I could imagine to have a mechanism where I could just "use" the backend service classes also in frontend, and spring clues the proxies together. But how?

You want to decouple the backend and the frontend, nice till there. You want to put them in separate wars on same tomcat, still possible but it has implications.
But now you want to inject beans from backend into frontend. If they are in separate wars it is no longer possible. Even on same same tomcat, each war if fully independant of the other and they should only communicate through the network (normally through web services).
You have two decoupling levels available :
one single war for both. The frontend would here consist on the view and controller layers, the backend of service and persistence layer. The coupling is provided by the service interfaces that you inject in your controllers. This is a single web application
each in its own war as 2 separate web applications. The frontend will have same view and controller layers than in previous case, and a thin service layer that would send REST requests to the backend. And the backend will have same service and persistence layer that in previous case, no view layer and REST controllers to process requests from the frontend.
For low to medium load, first solution will use a little less ressources, but under really huge load, the second one would be more scalable with farms of servers for frontend and backend (not speaking of reverse proxies before frontend and database servers behind backend)

If I understood your question correct, you want to separate your front end code from your back end.
What you could do, is create a .jar file of your back end implementation and in your front end instantiate the beans from an XML or Java application context.
If you don't want to redeploy your app for each change in the configuration I would prefer to use XML configuration.
In your front end code you have to include your back end jar and you can call these services in the regular Spring way.

yes you can load an external spring-context.xml from a jar / war in order to get the beans, check this answer here

You can bind your spring beans to JNDI and receive this objects in your frontend.
http://docs.spring.io/autorepo/docs/spring/3.2.3.RELEASE/javadoc-api/org/springframework/jndi/JndiTemplate.html#bind%28java.lang.String,%20java.lang.Object%29
But in this solution the decoupling of both wars is not really succeeded. I didn't see any benefit to separate frontend and backend in two war files when this must be deployed in the same container.
I would prefer to deploy one war file or communicate with REST between frontend and backend.

Related

Spring and SOA application

I'm new to WEB SOA applications and i have several questions about how to implements this architecture.
I would like to make a SOA based application involving multiple services using spring restfull api.
I'm aware of how to build each service itself.
i've already made a maven based project exposing a restful service using spring boot and secured it using spring security... my problem is to implement several services:
I don't know if i have to make a project for each one or there's a better solution... i want them to communicate through XML/Json so they won't be in same project in my point of view.
All secured by same service which makes use of spring security, i don't know how to link between the security service and the other ones. i don't want to write same security config classes on each project and then the user would be asked for sign in each time he accesses one of the services.
Share some resources which are used by all most services such as domain model classes, since i don't want to copy paste them (make duplicates), if i would change anything i would have to make changes in all services ... horrible :/
Thanks in advance.
1- Secure them all (the entire application) using one service which
make use of spring security and which will be asked for whenever a
client access one of the services.
This link will help you : https://www.youtube.com/watch?v=3s2lSD50-JI
2- Share some resources which are used by all most services such as
domain model classes.
Here it is about how to organize your project in your IDE or in your architecture. Using packages, shared libraries, shared projects or maven modules can help you.
3- Deploy all services inside one " application container ".
Your IDE or Maven should be able to help you deploy you application in a container. In the case of Spring Boot, there is an embedded Tomcat server that can run your application. Or if you have your Tomcat stand alone installation, you can deploy it by your self.
Reading you post, I guess your are new in Spring Boot development. The learning path I can suggest is the following (in my point of view) :
N-Tier Pattern for application architecture, and the purpose of the layers
The architecture of the Web and HTTP protocol
SOA and REST Services
Maven to build and compile your projects
Spring Boot, in mostly moderns architectures used to implement Backends my exposing REST Services
You can find by Googling tones of well explained documentations and blogs concerning those subjects.

Java application server to use or not if my program has no user interface?

I'm doing a project, splitting the whole application into frontend and backend (skipping database part first) and use jms as frontend and backend communicating platform. Frontend will be web-based, so we write jsps and servlets, packaging as war and deploy to application server(oracle weblogic). But for backend, since it has no user interface (just pick message from jms, new a thread to do db operation using JPA and do business logics and send result to jms), should I use application server to run my code?
I've tried putting code into regular application server, wrapping backend thread starting code in servlet context listener, storing started thread into servlet context, and everything seems to be fine. Spring part seems to work too. Is this the right way?
If I choose not to use the servlet way, how should i package my code and deploy to application server? The entry point of the code will be what class?
Please refer Spring components for handling JMS.
In short, you can define JMS Listeners and use Message-Driven POJOs. This is the simplest way I can think for a project which is already using Spring.
EDIT:
You can define you own start up class which will start on the start up of the server e.g. documentation for Weblogic Startup Class.

Does my application missing app tier?

Am new to programming. I have created a Spring MVC web application.
It has JSPs for the frontend, Spring MVC as the middleware component and for data part a request is made to webservice to fetch the data. The middleware component does not have too much business logic since the application itself and exists just for fetching data.
My friend says that my application is missing the application tier. Is this correct?
Currently I deploy my application in Tomcat and DB is Teradata.
Am not using any app server(only web server).
So does this mean my application is missing the app tier? Please elaborate.
If your friend is thinking of a 3-tier application, then yours doesn't have the Web Tier, or in fact, you have combined the Web and App tiers.
It depends on the logic implemented in your controllers. Usually JSPs an controllers are part of the view layer, business logic is in business layer an persistance layer is basically a database. This is called three tier application.
The logic in controllers should only care about how the data are presented to the user and should't do any operation on the data itself. Then you would have clean view layer.
There is no problem in having all business logic on the side of the webservice. Actually I would say this is quite common.

Notification Mechanism Using Spring

I have currently two wars files in which one war has to send notification to other war file using spring.Both of the wars are implemented using spring and web service.
My requirement is first war has to send notifications to other war file.
Could you please provide some pointers to implement the same using spring ?
I do not know exactly your requirements but I'd suggest you to use RestFull web service for this notification. Spring has a perfect support of this kind of services.
Internally the first application will send HTTP POST (or GET) request like http://thehost/webapp2/mynotification
Other way is to communicate using JMS. This way is good if you have to make the communication asynchronous. Spring supports JMS using JMS templates.
You can use:
JMS
a webservice (or spring http invoker) in the target app and invoke it from the notifier
You can use RMI to export your beans and make them visible from other modules, better than other alternatives in this case because:
JMS is asynchronous and needs a middleware.
Webservice are less efficient (since it is mostly conceived to communicate heterogeneous platforms).
Take a look here on how to do it:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/remoting.html#remoting-rmi
But I would first of all review the architecture you are using, in case you can refactor it for a better integration of business logic.

How to load multiple jax-ws web services inside a single spring container

We are working on a SOA Project consisting of multiple web services.
Each web service is based on Metro JAX-WS Framework and internally uses spring and hibernate.Each web service loads as a web-application inside a separate spring container.
This means that our deployment architecture consists of multiple web-applications each running a different service.
However we are facing some performance issues because each web-application loads its own spring container/hibernate session factory.
Possible alternatives:
Single web-application single spring context
Multiple web-applications single spring context
All our web services will always run together on a single server. What will be the best architecture for our case? And how to achieve the same i.e. how to use a single spring context with multiple web services?
The "best" in "the best architecture" is quite subjective, as it depends on your current architecture, which you haven't defined very well.
However, I have some suggestions:
Consider the impact on maintenance that consolidating all the business logic related to your services in a single web app will have, not to mention all the spring-configured-beans that you will have to inter-mangle to re-factor all you services. In that sense, keeping separate web apps tends to be better according to my experience.
Consider to move the hibernate session factory away from each service and to a centralized location, for instance, in JBoss, you could create some type of "hibernate interface" in the way of a .HAR file, each service will only have to "ask" for a hibernate session. Maybe you could do something like that in your particular application server. There are several patterns to efficiently retrieve a Hibernate Session that apply here.
Most web services performance issues are also associated with the way they are used, sync or async. We solved one such issue at my company by implementing a messaging systems under the covers to handle the web service requests asynchronously, whenever a request comes in you place them in a Queue, there are other services waiting for certain types of requests to process them. This can be easily implemented with Spring and ActiveMQ.
That's enough for the moment.
Regards.

Categories

Resources