Separating service layer from web layer in a Spring application - java

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.

Related

Spring Injection between two projects

I am new to Spring and I have below query.
I have a Spring Project which just perform the Database Operations. It has Entity Class and Repository say Employee and EmployeeRepository.
I have another project which act as RestWebService (in Spring). I want to inject EmployeeRepository in the controller. Both the project are Web project and deployed on same application server.
Please suggest how to do this.
If the projects are deployed as separate applications on an application server, they cannot access each other's beans. Each application is maintaining its own Application Context.
The better way would be having just one application deployed, which has the other project as dependency (using maven for example).
Of course easier would be having just one project.

How to move business logic out of Spring MVC architecture

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.

Spring WS separately deploy web service and business layer

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?

How to structure multiple mvc projects that share a domain model using IntelilJ

I'm hoping to get some advice on how to structure/setup a project.
Currently we have a webapp that uses spring, and JPA/hibernate. We want to create an admin webapp that's going to live separately then the "user" webapp for backend stuff we need to support. We want to keep it separate from the existing project so they can be deployed independently since they have different testing and reliability requirements, as well as we want to be able to iterate more quickly on the admin stuff. These are deployed via war files built by maven.
We do want them to share the domain model though, and we would prefer to keep the existing configuration from the webapp as well. So basically we want to be able to separate out the webservices from each other, while keeping everything else the same (app-context, web config, persistence, etc...).
Is there a nice way to separate out the webservices, or will we basically have to duplicate the entire project for each war file, or keep them lock step?
Package your domain model (entity and DAO classes) in a separate jar. Use maven for dependency management. Distribute this domain model jar in a company local repository (eg: nexus). Then on your user web-app & admin web-app just declare this as a dependency.
The only thing you need to be aware when using this mechanism is when you update the domain model and distribute new version into nexus, all downstream packages dependency version has to be updated and recompiled

Spring beans (business layer) in separate module

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.

Categories