Difference between spring modules and spring starter modules - java

What is the difference between spring modules (e.g. spring-boot, spring-web and etc) and spring starter modules (e.g. spring-boot-starter, spring-web-starter and etc)?
And which one should I use?

Spring Boot Starters were built to address the issue of dependency management. Dependency management can become very complex over time and you can spend a long time managing them in your project versus focusing on issues that really matter, like your business logic and bug fixes. Spring boot starters are a set of dependency descriptors that you include in your service.
Take the MongoDB starter for instance:
https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-starters/spring-boot-starter-data-mongodb/pom.xml
It has all the dependencies you need to connect to a MongoDB instance, all you need to do is add the starter to your service pom.xml and some configuration.
Spring modules, on the other hand, are part of the core spring runtime framework. They provide the fundamental parts of the framework such as IoC and Dependency Injection.

Related

Run Spring Boot jar from standard Java application

I have a large application with a database, Swing UI etc. Now I want to add a REST API for this application. Spring Boot allows easy generation of a REST API with useful features such as OpenApi documentation and authentication.
However when I run the Spring Boot application from within the large non Spring Boot application the Spring Boot application gets confused by the dependencies of the parent application and fails to run.
So my requirement is this: run a Spring Boot application from withing a non Spring Boot application without dependency interference from the parent application. I am currently running the Spring Boot application by adding the executable jar as a dependency and then calling
org.springframework.boot.loader.JarLauncher.main(new String[0]);
to run the Spring Boot application. I am not set on this way of doing it and any suggestions will be greatly appreciated.
Spring is a huge collection of highly configurable software libraries that can be used to setup (among other things) REST API endpoints and OpenAPI documentation and UI.
Spring Boot is a project to simplify the process of using these libraries by applying an opinionated view of how to run them within a standalone process.
By asking how to run a Spring Boot application within a larger application you are trying to get the benefit of the opinionated setup while violating the assumptions that the setup is based on. I guess in theory it might be possible using some sort of handrolled classloader isolation, but once you've solved the dependency problem you'll probably end up with class version conflicts, issues with configuration locations, etc. In short if it is possible at all the effort of doing so would negate the benefit.
There are two ways of resolving the issue.
Use Spring Boot to build your API as a standalone process. Configure the new process to talk to the same database as the existing application. If neccessary factor out any code common to both the existing application and the API (JPA entities, DAO classes etc) into a shared library. If you go with this option you will have the overhead of having to manage multiple kinds of process in your production environment, which is more complex - but has advantages in terms of decoupling scaling, release cycles, restart times. See the debate on microservices (https://martinfowler.com/articles/microservices.html).
Use the Spring libraries that provide REST and OpenAPI features as part of your existing application, without using Spring Boot. You'll need to have SpringMVC set up in order to use #RestController annotated classes. If your existing application is a web application that's not too bad (https://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/html/mvc.html). If it's not run in a webserver already you'll have to launch the SpringMVC framework in an embedded webserver (https://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat). There's a good article on adding OpenAPI to an MVC application here: https://www.baeldung.com/spring-rest-openapi-documentation.
you can simply exclude auto-configure dependencies. Here is an informative link https://www.marcobehler.com/guides/spring-boot
Here is a code snippet of how to exclude when applications get started.
#SpringBootApplication(exclude = {DataSourceAutoConfiguration.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(PayPalApplication.class, args);
}
}
or the other ways
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
I want to share another Spring Framework dependencies https://www.marcobehler.com/guides/spring-framework

Java Dependency Injection framework with Convention over Configuration feature

In this blog post Mark Seemann explains the different approaches to configure dependencies with a dependency injection framework. Convention over Configuration is one of this approaches. In the .NET world there are multiple DI frameworks which offers this feature (e.g. NInject, MEF 2). I'm searching for a DI framework with Convention over Configuration in the Java world. I looked at Spring and Guice but neither of them seems to offer this feature.
I would argue that hk2 has a convention over configuration like feature with automatic service population. It requires that you use the hk2-inhabitant-generator at build time to put some metadata into your built jar files. However, after that it becomes very simple to populate hk2 with your services. This is how GlassFish uses DI, where classloading at the start of the server had to be kept to a minimum in order to increase boot performance.
There's one for JavaFX by using afterburner.fx is JavaFX MVP framework based on Convention over Configuration and Dependency Injection.

Good project with spring-mvc and hibernate

Please advise me a project where I can see how to use Spring MVC and Hibernate together in the right way. I'm new to these frameworks.
Appfuse has a Maven archetype for this stack.
Strongly suggest you have a peak.
Alternatively the book Spring in Action was a great resource for me in learning spring and hibernate/spring interaction - use Annotation driven transaction management as well and you have a really great start.
see here: http://appfuse.org/display/APF/Using+Spring+MVC
appfuse-basic-spring is the basic project archetype.
Using maven modules is a better practice. ie)
appfuse-modular-spring
If you install m2e (maven 2 eclipse plugin) creating a new maven project will allow you to select the archetype.
better answer:
If I was going to recommend HOW to learn these technologies, I would say learn how to build a hibernate/maven/xml project first by following this:
http://www.mkyong.com/hibernate/quick-start-maven-hibernate-mysql-example/
Then learn how to use spring for dependency injection with maven/hibernate and xml:
http://www.mkyong.com/spring/maven-spring-hibernate-mysql-example/
Take special note of layering of the bo/dao pattern and how you use interfaces but wire in implementations with spring di - this is crucial for writing quality code
Next check hibernate annotations and annotation driven transactions:
http://www.springbyexample.org/examples/hibernate-transaction-annotation-config.html
http://www.springbyexample.org/examples/hibernate-transaction-annotation-config-code-example.html
http://www.mkyong.com/hibernate/maven-hibernate-annonation-mysql-example/
Finally, you can learn your mvc framework of choice. I actually recommend struts2 but it won't matter much. use spring DI by implementing interfaces and wiring implementations at runtime for you actions/controller. If you don't understand this how, you will after the hibernate/spring tutorials. Spring in action will be a good reference to keep on your desk but will take a few weeks to digest as you learn. Good luck! You can do it - I did and am now working as a java dev!
the petclinic app includes these, and many more things
spring-roo can create a skeleton project using these technologies for you.
If you are new to Spring MVC and Hibernate, I advice you to follow the Spring MVC step by step guide. The only problem with this is the version, the guide uses version 2.5 and the current release is 3.1.x, but it's a very good starting point to accumulate knowledge.
Spring MVC quickstart archetype provide simple spring-mvc and hibernate configuration.

Basic Spring 3 configuration for IOC

Can anybody tell me a basic configuration to use String dependency injection? What are the minimum required jars?
At the moment I'd like to use only Inversion Of Controll, maybe later I'll integrate ORM.
Thanks
Try Spring roo, there is nothing simpler than that to get a full fledged Spring based web application working, with all the dependencies wired together.
Spring STS (an Eclipse distribution customized for Spring available for free from SpringSource) contains several Spring project wizards that produce IoC enabled projects that you could use as examples.
Almost all Spring examples use Maven to define dependencies and download jars automatically from repositories available on the internet.

How to avoid Spring Roo GWT support?

I am experimenting with Spring Roo in a new GWT application. The Spring Roo GWT support is some way off ready for real use just yet, so I want to build the GWT stuff by hand using as much of the GWT 2.1 MVP stuff as possible. The problem I have is that Spring Roo "notices" my MVP-related classes and generates a whole lot of extra (broken) stuff for my entities.
How can I get Spring Roo to ignore the GWT side of my project?
"I want to build the GWT stuff by hand using as much of the GWT 2.1 MVP stuff as possible"
If you're building GWT by hand, it sounds like you're interested in using Spring Roo to generate your data model code — but don't want (or need) any of Spring Roo's web controller code. If that's the case then you can separate your project into two modules:
A module containing model and persistence code that is created by Spring Roo
A GWT web application that you create by hand.
Make the first (Roo) module a dependency of the second (GWT) module. Basically you're using Roo to create a JAR library that's used by your web application. As long as you don't run the controller command the Roo won't add any web application code to your module.
I renamed my gwt.scaffold package to gwt.shell and gwt.request to gwt.req and Spring Roo is leaving my stuff alone.

Categories

Resources