Mixing spring 3 and 4 - spring

I have a legacy application that uses Spring 3.x. It's built using Maven and creates a war file. It's big, perhaps what would be called a monolith nowadays.
I'm slowly moving to a micro services architecture and as a first stage I'd like to create Maven sub-projects built as JAR files (within the web application) that contain Spring controllers that use the newer #RestController annotations of Spring 4.
Will there likely be problems do you think of mixing version 3 and 4 of Spring? I'd like to scan the package structure of the jar file from the existing Spring 3 app in order to detect the Spring 4 annotations.
Thanks in advance.

Related

Spring boot 3 - Jakarta and Javax

In the new Spring boot 3 Release notes, They tells that this version is going to use Jakarta EE 9 (libs named as jakarta) instead of JEE (libs named as javax).
They advise developers to replace imported javax with jakarta in this article.
If I have a spring boot app with both, javax and jakarta libs, will the app work and be able to be deployed in a Jakarta compatible server (e.g. Tomcat 10)?
Thanks a lot.
The answer will really depend on which specific libraries you're using and how they interact with each other, but generally speaking trying to mix Java EE and Jakarta EE them would be a bad idea.
As an example, if you're writing a Spring MVC application then you'll be using the DispatcherServlet. In Spring Framework 6 this will require the Jakarta Servlet API. There's not going to be a way to make it work with the javax.servlet For other APIs, if you're using them directly and you're not making use of Spring abstractions that build on them, you may get away with having them on your classpath. I still wouldn't recommend it.
You could try to put the web app instead of in webapps into webapps-javaee like described in https://tomcat.apache.org/migration-10.html#Specification_APIs
Then TC10 will create a new war in webapps and unpack it as usual in webapps. I tried it with some of our pure TC8/9 Apps and it was working.

Integrate New Spring MVC framework for coming module and struts 1 and hibernate 2 based existing framework in single web application web.xml

Currently I am working on struts 1,spring 2 and hibernate 2 based web application.now we are going to add some more module in our application but the requirement is that we have to integrate new module with spring MVC framework without doing any changes for existing module.
So how can i integrate new spring MVC framework and existing framework in single application together ?
What changes i have to do in web.xml file ?
So is it possible to use (hibernate 2 and hibernate 4) jars and (spring 2 and spring 4) jars in single application ?
If it is possible then what about hibernate session factory , how this is going to be handled ?
Its quite bad idea I think. Especially packing different versions into one jar may cause several compile and runtime problems. You shouldn't do it.
Two different MVC frameworks for one web application is probably not impossible but also very bad decision.
If you cannot drop existing code/project, you should start with clean new seperate project and try to configure them to run side by side on production. But no mix up.

Spring: Injecting services from a different project

In the project our team's working on, we currently have 3 separate Spring projects which utilizes the same services. To avoid redunancy and code copy-pasting, we're planning to create a "common" project wherein all the three projects would be dependent on the common project. In this instance, is it possible to inject these services (perhaps using the #Service annotation) to the Controllers of the Spring projects?
EDIT:
I tried implementing this on my own and what I basically did was I configured the pom.xml to get the Spring Context 3.1.1 dependency (which are also being used by my Spring projects) for my "common" project. With that, I was able to annotate my service with #Service. Afterwards, on my Spring project, I set the component-scan to a level wherein my two projects would meet. On my Spring controller, I #Autowired the service from the "common" project. I ran the Spring project and apparently it worked. Is this the best way to do this?
That's absolutely fine, and standard. Spring (unlike CDI) couldn't care less whether your beans come from the current project or from an imported jar.

Maven 3 Archetype for Project With Spring, Spring MVC, Hibernate, JPA

I'm trying to use Maven 3 to create a project which uses Spring 3, Spring MVC, Hibernate 4, and JPA. However, when I execute:
mvn archetype:generate
Non of the archetypes listed include all of these; and even those which are close seem to be special projects such as projects with Flex. I want to avoid having extra modules such as Flex that would crowd the project and configuration files. So, is there an archetype for Maven 3 that I can use to create such a project?
A great Spring MVC quickstart archetype is available on GitHub, courtesy of kolorobot. Good instructions are provided on how to install it to your local Maven repo and use it to create a new Spring MVC project. He’s even helpfully included the Tomcat 7 Maven plugin in the archetypical project so that the newly created Spring MVC can be run from the command line without having to manually deploy it to an application server.
Kolorobot’s example application includes the following:
No-xml Spring MVC 3.2 web application for Servlet 3.0 environment
Apache Tiles with configuration in place,
Bootstrap
JPA 2.0 (Hibernate/HSQLDB)
JUnit/Mockito
Spring Security 3.1
Possible duplicate: Is there a maven 2 archetype for spring 3 MVC applications?
That said, I would encourage you to think about making your own archetype. The reason is, no matter what you end up getting from someone else's, you can do better in not that much time, and a decent sized Java project is going to end up making a lot of jar projects.
Take a look at http://start.spring.io/ it basically gives you a kick starter with either maven or gradle build.
Note: This is a Spring Boot based archetype.
With appFuse framework, you can create an Spring MVC archetype with jpa support, etc ...
Take a look at it's quickStart guide to see how to create an archetype based on this Framework.
Foundational frameworks in AppFuse:
Bootstrap and jQuery
Maven, Hibernate, Spring and Spring Security
Java 7, Annotations, JSP 2.1, Servlet 3.0
Web Frameworks: JSF, Struts 2, Spring MVC, Tapestry 5, Wicket
JPA Support
For example to create an appFuse light archetype :
mvn archetype:generate -B -DarchetypeGroupId=org.appfuse.archetypes
-DarchetypeArtifactId=appfuse-light-struts-archetype -DarchetypeVersion=2.2.1
-DgroupId=com.mycompany -DartifactId=myproject

How to setup a Spring multimodule project?

I'm new to Spring and try to setup a project which is split into 3 submodules. As build tool I'm using maven. My problem is, that I don't know where to add Springs "magic".
My 3 submodules are "ORM" (holds all the hibernate staff to access the database) "BusinessLogic" (which should hold the complete logic) and "WebApp" (adds as the only "client" to the app / logic).
I want to use SpringMVC for the WebApp which seems to be no problem. As "BusinessLogic" should hold the complete logic I thought of adding the Spring related stuff (Bean definition / DI) in that module. But then I don't know how to setup Spring when accessing the module form the webapp.
The hole project is being ported from a JavaEE / JBoss app where "ORM" and "BusinessLogic" (implemented as EJBs) where put into one .ear archive and the webapp into a seperate one (.war). JNDI was used to access the beans from the webapp, but I completely want to decouple the application from JBoss and deploy it on a Tomcat webserver.
At the moment I've created all three modules as separate Maven projects ("ORM" and "BusinessLogic" as .jar, "WebApp" as .war packaging), linked by a "parent" project.
Thanks for any hints on project setup :).
Greetings
Ben
you could configure spring context in your web.xml and you can perform import of Spring sub-modules context. You can add import's configuration of sub-modules in your webApp application context.

Resources