I'm building a new Java web app using Spring MVC framework and, at the same time, i'm building a mobile app using an hybrid framework (Cordova etc...). All modules are in the same project in Intellij and I'm using Maven as repository.
I would like share the business logic between the web app and the mobile app but i cannot move the spring service layer out of the Spring MVC.
I tried to create a new Java module in the project but I cannot understand how can I link the module between the 2 apps, using the right architecture.
Can I move the service layer out of the web app, in an external Java module?
If I cannot do this, how can i manage a shared business logic beetwen the web app and mobile app?
Ask me for any doubts.
Thanks!
That is a very common question people new to Java ask.
What my approach is; when I develop an application, I write the business action and controller classes in separate packages and reference them through JARs.
If you are using spring, you can add some custom functionality to controller classes by extending them (I am assuming you are not using annotations) so that they can execute your business action classes which are provided to them by using beans.
Related
Java Spring Boot MVC and CLI API in the same project
I have implemented a web app using Spring Boot MVC and I now I need to add an API whose functions can be called from a cronjob.
I would like to use Entity and Repository classes implemented in my Web app. I was thinking about creating a new main class which implements
CommandLineRunner interface and then create separate JARS for Web and CLI applications and run them independently. Is this a good approach?
Do you have any suggestion about the architecture?
Thanks
Concur with comment from #jb-nizet - cron is really just another client of your application, and using a (bash,etc.) script wrapping curl, httpie, or wget to call the controller is a good solution.
Advantages include:
can be called remotely
doesn't add complexity to the application
application boundary/interface remains the same
doesn't add a new application (except a relatively trivial script)
not tied to cron - could be almost any other application
Considerations:
authentication (if needed) and the management of credentials in the script/cron
if all methods/actions needed for cron are new and different to existing functions in the web app, do they make sense to be encoded in the webapp?
I am pretty new to spring and currently I was able to create a complete web service with spring-ws. Now I want to separate the functionalities of my web service in to two separate web services. But except the service layer there are other spring components (business layer) which are common to both of these services.
So my question is there a way to make spring web service depend on another spring project (business layer)? If you can provide such example or a tutorial where a spring web service depends on another spring project it will be really helpful.
Thanks in advance.
UPDATE
I could achieve above by building my business layer as a jar file and adding it as a dependency to the service. But now my supervisor wants me to deploy the business layer in a separate server. But I could not find any information on how to handle communication here between web service and the business layer. Any idea?
I have an existing Java application (Spring based) that currently does NOT have a web interface, nor does it run in a web container. It packages up nicely with a start program and just works.
What I need to do is add an administrative web interface for some administrative type things, retrieving real time metrics, and perhaps some graphs to give the users a warm fuzzy feeling knowing that everything is working. As we are a Spring shop, and some of our web applications already use Spring MVC it only makes sense to us, however, I'm not happy with the suggestions I've had from our internal Spring folks on how I should procede.
What would be the ideal way to bolt on this web interface?
Convert my application to a web application and have a web container launch the application. I not too keen on this approach is the web tier is really secondary to the primary application.
Create a separate project that packages as a war, embed Jetty in my existing app and have it load the war. I think I can use the context loader listener to make the root context of my application the parent to the web application spring context. This would involve breaking up my Maven project into 2 projects I believe. I can't use an existing web technology for communication between the web tier and the primary application as my primary application is not web enabled.
Embed Jetty and put the Spring MVC stuff directly in my app. While I've done this approach before, it does involve some ugliness - for instance exploding the JSP tag libs into my jar.
Any thoughts on some clean separation here?
Also of note, my current jar contains some utility applications which some shell scripts launch. Going a pure WAR route would make this not so easy, since I can't juse point java at my war file and choose a class to execute.
Thanks.
If it's true that web is just a minor addition the application, migrating it to WAR and deploying in servlet container might be an overkill. Embedding web server/servlet container looks much simpler, although it doesn't have to be Jetty or Tomcat. You can use web server built into JDK or write one on top of netty or even raw sockets. But that's a hell to maintain.
Yet another solution to springs to mymind when reading:
web interface for some administrative type things, retrieving real time metrics, and perhaps some graphs
Maybe you don't need an interface, but monitoring infrastructure instead? Check out JMX (Spring has great support for JMX) - and write a second web application that simply connects to your standalone Java app via JMX and displays the metrics in fancy way. Another approach is to expose JMX via Jolokia, which translates JMX to REST services.
This approach has several advantages:
monitoring API is universal, and you get it for free
you don't have to use web client, monitoring application is completely decoupled,
finally changes to your original application are minimal. Check out this proof of concept: 1, 2.
It really depends on the structure of your existing Java/Spring app and how much of an API it provides. I've done something similar to this and I approached it by creating a separate Spring MVC project and then specified the existing Java app as a JAR dependency.
This is easy with Maven (or Ivy, etc) and provides nice decoupling. The trick is to be able to write service classes in the Spring MVC app which then access data via your dependent Spring app via a simple DAO class. That's why I stated at the beginning, that it depends on the structure of your original Java app. It has to be able to provide an API for data access that you can then plug your DAO (impl) into.
If this is not easily done, then the next option I'd suggest is simply converting your Spring app to a Spring MVC app. I've worked on another app where we did this. Using Maven, its possible to specify that the build can create either a war file or a jar file (or both). So it can be deployed as either a webapp (via war) or a normal app (via jar). Yes, the jar version has a bit of bloat but its a worthwhile compromise.
The question of embedding Jetty or using Tomcat via a war file is a completely separate issue that has its pros and cons. It shouldn't affect the approach you take in architecting the web app in the first place.
I'm starting new application. I would like to have all business logic and domain classes separeted in standalone module (something like remote EJB). This is because of the app will have at least two (later more may be) clients - Desktop, Web (Spring MVC).
Is this possible with Spring? Or should I use EJB and Spring only for MVC in web app client?
Thank you for reply.
Yes, this can be done. You'll either put the resulting jar as a library in your full application, or host is separately and use some remoting system (hessian, soap, rmi, ...) to interface between them. Spring can help with that as well.
I am currently learning Spring. So far I have created a basic application consisting of Hibernate/JPA entities, DAOs and classes that perform business logic. This I am calling the service layer.
If I now wish to use SpringMVC to add a web front end to this application, how should I separate the two?
i.e. do I need to create a separate 'Dynamic Web' project in Eclipse for the web layer? If so, how do I then integrate the two? I presume I could simply copy the service layer source into the web project, but this doesn't seem like the best approach.
You don't need a separate project, it really depends on whether you'll be reusing your services elsewhere.
If you won't be reusing your services, add your web layer to the same project, have your controllers call your service layer, and build a WAR from it.
If you will be reusing your services, create a new project for you web layer, build a JAR for your services, and import that JAR into your web layer. Something like Maven will help here.
Are you using Maven? If so, you should create a webapp project and add your "core project" as a dependency.