首页 > 代码库 > Spring Framework Ecosystem – Introduction to Spring Projects

Spring Framework Ecosystem – Introduction to Spring Projects

来自于:http://springtutorials.com/spring-ecosystem/

 

Hello and Welcome to Spring Tutorials Blog!

Is it fair to assume you have at least heard of Spring Framework official website – spring.io?

If not, I would recommend that you check it out. There are some noticeable differences if you haven’t been to its official site in a while. This site used to be springsource.org up until two years ago. Before that, it used to be springframework.org.

It is important to understand how Spring Framework can help you in your applications. I discussed that in Twelve reasons to use Spring Framework in your projects.

In my post, I pointed out different Spring projects and Spring modules that have emerged now. I pointed out some of them that you can use in your application. I gave examples of how I had used them in my projects.

But I left out a key thing.


 

I left out WHERE these projects would fit inside your application.

You may be an architect trying to introduce them in your projects.

It is important for you to know different layers where these spring projects would come into play.

A comprehensive view listing them all out would benefit you.

By the end of this post, you will have gone through spring tutorial on spring projects like:

spring boot, spring data, spring security, spring batch, spring mobile, spring XD etc

 

Actually think of this as your first spring framework tutorial on Spring Projects. I will discuss them all today.

 

The code that powers spring.io website uses Spring Framework and Spring Boot. The site code is actually open source and on GitHub under the name Project Sagan.

I go to spring.io for their short guides and I believe they are great.

But I feel like there is something missing there, though – the big picture.

I will try and explain the issue first. I want to go over why I feel there is a void. And I plan to fill that missing void today with this post.

The end result of this discussion would be an overview of the Spring Ecosystem as the title suggests. The focus here would be on Spring Projects.

 

I want to begin with a quick story…..

Few years ago, I used Groovy in one of my Spring Framework based projects to do XML parsing.

XMLSlurper is pure groovy in my opinion. Pun intended.

If you are a python developer – think lxml.

I turned the XML Parsing into a service and exposed the method as an interface to a Spring Service layer.

Here is how it affected my team:

To the web application,

it was a service implementation. It didn’t matter to the controller or other services how I implemented it. Spring did the code weaving via the interface.

To other developers,

If they called it via exposed method and it gave them the POJO they needed, they were good.

To the architect,

it was a jar file like any other. It could have been a Java-based XML parser implementation for all that mattered. If it deployed as a war and performed well, there was no problem.

To the managers,

the project had a deadline. If the project was ready in time and on the budget, there was no issue.

To the testers,

the feature worked as expected in their test cases. There was no complaint.

Long story short – the project was successful.

I was at AOL meetup several years ago. This was before the ‘XML Parsing with groovy’ project above came into being. One of the speakers there demonstrated what Groovy was capable of.

Two important things came out of that demo:

1. Groovy is excellent at parsing XML as well as HTML.

2. Groovy is excellent at interacting with Microsoft tools like Excel.

Groovy actually has several other cool features. These are just the ones I was thinking of at the time.

If you want to learn Groovy, check out the videos of Scott Davis.

I never practiced Groovy when I came home. But I never forgot the demo. I knew XML Parsing was easy to do in groovy than in Java. And I had learned where to bring it in.  Thanks to the speaker.

So when I saw that our project needed XML parsing, Groovy fit the bill.

Groovy is not one of the Spring Projects. I mention it to prove a point:

I did not know Groovy well, but I knew WHERE I could bring it in if needed.

I want you to be able to do that with Spring Projects. I want you to be able to bring Spring Projects into your application – whenever or wherever you saw fit.

Spring Projects

It is sometimes hard for a newcomer to get a birds eye view of different Spring projects and where they fit in.

You may have to go into individual projects to understand what they mean first.

The truth is: Learning all Spring Projects could get cumbersome and overwhelming.

I have worked with a lot of Spring projects and yet I am having to reevaluate things any time a new project comes out.

The Spring website currently lists 24 Spring Projects.

Those are easy to spot.

There is a big ‘Projects’ tab on the top right section of the spring.io website. Click it and you will see items split into three columns. Yes, those are all Spring Projects.

Once you pass the Spring Projects boundary, things get little murky.

Some Spring Projects have subprojects.

spring.io shows a green label strip with a number on the top right section for some Spring Projects.  That number indicates the number of subprojects.

技术分享

 

Spring Projects with subprojects

So, Spring Cloud has 7 subprojects. Spring Data has 9 subprojects. Spring Security has 4 subprojects. And Spring Social has 4 subprojects.

Try going into these projects and see if you can list out these subprojects.

Here’s a related question:

Is Spring JDBC a project or subproject?

What about Spring MVC? Is that a project or subproject?

I won’t waste your time. So here it is.

Spring JDBC and Spring MVC fall under a third category called Spring modules.

For instance, there are about 20 Spring Core modules. You won’t see them unless you get into a Project named well ….’Spring Framework’.

There are several other non-core modules that show up in different Spring Projects. But please allow me to skip that conversation for now. I want to discuss Spring modules in my next post.

Spring Projects is my focus for today.

Let me say one more thing about Spring modules before diving into Spring Projects.

A Spring module is a complete and independent feature with minimal dependency on othersOthers here could mean other projects or other external libraries.

Think of Spring module as a unit of work.

I think it is safe to say that all Spring Projects contain one or more modules.

A Spring Project is different from a spring module. A Spring project packs everything you need to get started with it.

This means you could pick any Spring Project and include them in your application. And they will bring all their dependencies with it.

This involves some explanation. And I want to elaborate on this point going from a simple project to a more advanced one.

But I want to share an image first.

As a Java / Spring developer, I am always looking for the best ways to fit these projects into my app.

I want to know if a certain project will fit in front-end, middle or the back-end layer. I have to ask myself:

If I were starting out with Spring Framework, where would I start? Which Projects would I bring in and WHERE?

So, I took Spring Projects and arranged them in the order a Java / Spring Developer would like to see them.

技术分享

 

Spring Ecosystem – Spring Projects

The image splits the Spring Framework Ecosystem into five parts. I hope it is intuitive but let me go over it just to be sure.

You will see three arrows going from left to right. The three arrows split the Projects into four parts:

1) Web layer, 2) Common layer, 3) Service layer and 4) Data layer. 

The three projects at the bottom of the page comprise the fifth part.

They are 5) Foundation projects and can span many layers.

1) Spring Projects on the Web layer

The following Spring projects help you with your front-end development in one way or the other.  Let me list them out first.

  • Spring HATEOAS
  • Spring Mobile
  • Spring Web Flow
  • Spring Session
  • Spring Web Services
  • Spring Social
  • Spring for Android
  • Spring Security

I show Spring Security at the center of all these projects for a reason.

It does not mean these projects surrounding Spring Security are dependent on it. It just implies that all web layer projects can make use of Spring Security.

In the production environment, you will want to see these Spring Projects leveraging Spring Security.

I will go over these projects one by one and share few sentences on what they do. Later, I will identify how you can bring it in your project.

If you can use it with other Spring Projects, I will bring that up as well.

If a project pulls other external libraries, I will mention that as well.

In my next post, You will see ALL web layer projects pull some common modules. I will provide details of Spring modules then.

For now, please note that I am skimming over these topics. I will have a detailed post in future with examples for each project. The goal here is for you to be familiar with them all.

Let’s start with Spring Session.

a) Spring Session allows you to handle web sessions in your application.

You shouldn’t store anything in HttpSession but let’s say you have to. What do you do? Spring Session Project can help you out here.

Use Spring Session with Spring Data Redis and you can store any session data into Redis datastore. This makes it short-lived, fast and temporary. Also, it can solve your clustering problem.

Spring Session is a Web layer project. Spring Data is a Data layer project. And they work great together.

Spring Data Redis subproject will pull java Redis client – Jedis by default. It will also pull apache commons-pool as a result.

To use it in your project, you just need to setup Redis Connection Factory. Spring Session will do all the magic.

You don’t need to write any more code here. So, it is easy to introduce or pull Spring Session out of your project with minimal effort.

b) Spring Web Services helps you build a contract-first SOAP-based web service. It also helps you consume an SOAP-based web service with Spring.

Contract-first based web service means you write WSDL first. WSDL stands for Web Services Description Language. It is the language of the SOAP web service. You need it to explain to your client what they can consume. It defines the contract.

In my opinion, a Java developer shouldn’t have to worry about description language. He should write a service in java and then a project / framework  would take care of exposing it as a web service. In my projects, I used Axis2 with Spring to achieve this.

I am not a big fan of contract-first approach to web service development. I plan to address this in a future post. For now, Let’s move on.

Spring WS pulls in Spring OXM module for converting XML into java and vice versa. This is also called Marshalling / Unmarshalling.

Spring WS code to publish a web service involves annotating your POJOs. The configuration includes generating classes from XML schema and writing logic to populate them.

It may not be easy to introduce or pull out of the Project as some code dependencies get introduced.

c) Spring Social lets facebook, LinkedIn, twitter and other users log into your site. This eliminates the need for a user to create a new account for your application.

Including it in your application involves setting up configurations based on providers. It is a standard set of code that you write once.

There is some initial setup needed to get this working. But swapping the facebook login to your home-grown login is easy. Thanks to Spring Security.

d) Spring for Android helps you build native android applications.

This is not invasive by any means. The bigger effort here is learning android SDK and getting used to Android Studio.

In my opinion, Mobile-first Javascript MVC frameworks are a much better option. They are easy to build and debug.

Spring Android team, if you are reading this, I’d love to know otherwise.

e) Spring HATEOAS helps your create REST representations that follow HATEOAS principle.

Here is another effort to explain what it means:

Your web page provides links to other pages.

With HATEOAS, your REST services can return metadata about other discoverable services or representations.

This is the premise behind semantic web. And I know one person who can explain it well: Brian Sletten.

Check out Brian Sletten’s article on HATEOAS.  Here is a video of him going over REST. If you would like to understand the R in REST, I suggest you check out this post from him as well.

Spring HATEOAS sits on top of Spring web-MVC. It has no other dependency which makes it easy to introduce or pull it out of your projects.

f) Spring Mobile helps you build mobile friendly pages. It can detect if your requests are coming from a mobile device.

You can then render pages based on this information as you see fit.

It is easy to add to your view renderer. In my case, I was using tiles to send users to appropriate pages based on the device.

So you write two sets of web pages, one that caters to mobile device audience and the other one for regular web.

I don’t know if this is a good approach. Once again, I think there is a better way in javascript MVC frameworks like Angular JS to achieve this.

AngularJS provides a responsive design. You may not need two sets of pages as a result.

It is easy to introduce Spring Mobile and pull it out if needed.

g) Spring Web Flow splits data into many screens for easy navigation.

Some of Spring Projects are frameworks. They take some time to incorporate into your project. Once they are in, they can provide for a lot of features. Once they are in, they are hard to replace as time goes by.

Spring Web Flow fits in here. If you decide to replace Spring Web Flow, you have to think about how to handle things that were a given.

It many not be easy to replace. And for a good reason.

All those hidden variables that Spring Web Flow handled for you – You may have to handle them on your own now.

h) Spring Security is at the heart of this layer. If you are writing a web application,  you will need to interact with a robot or a user at some point. Spring Security makes your application secure. It controls who can interact with your application and at what levels.

Spring Security is almost irreplaceable in my opinion.

I have heard good things about Apache Shiro but have not tried it yet. If I skip Apache Shiro for a few seconds, I can’t imagine replacing Spring Security Framework. Any custom solution just wouldn’t cut it.

The separation of concern and level of isolation Spring Security provides is excellent. Try implementing it yourself with filters and you will appreciate its power more.

Spring Security bring its own database schema. If you are not overriding the schema, You may need database team approval.

Spring Security can include Spring LDAP for authentication and/or authorization.

2) Common layer – Spring Framework Project

All these projects in different layers have one thing in common:

They all leverage Spring Framework.

A Spring Framework has 20 modules as I mentioned earlier. These twenty modules fall under six categories.

1) Spring Web

2) Spring Test

3) Spring Data Access/Integration

4) Spring AOP and Instrumentation

5) Spring Messaging

6) Spring Core Container

When I say they all leverage Spring Framework, I mean:

All Spring Projects make use of one or more of these modules.

That’s all I want to say about modules today. More to come soon.

3) Service layer Projects

In my little experience, the following projects would make great services.

This is debatable, though, but I have a reason to include them into service layer and am willing to take my chances.

Spring Integration

A file poller scenario I gave in my previous post is a good example.

If you think about it, file polling might fit into data layer as well. After all, you are dealing with data here.

But then again, You are polling which is not a true data manipulation. It is an event.

You want to capture this event on a service layer. The process is more visible that way. You can take some action based on this event now.

And these actions could mean data manipulation for sure.  Data manipulation belongs in the data layer. The event – not so much.

Spring Integration is perfect for handling event-based actions. You can plug it on top of any Spring Data layer projects.

Spring Cloud

I am new to Spring Cloud.

I was thinking Spring Cloud should be a foundation layer project at first. I will discuss foundation layer projects soon. With so many subprojects under it, I can see Spring Cloud Project as a toolkit.

And it IS a toolkit for building distributed applications. Let me explain.

Let’s say you were building a service that you would want to distribute and scale at some point. What are some of the things that you would have to take into account?

  • I would want the service to scale. If the calls grow, the service should handle it.
  • I would want discoverability. If few nodes die, the service shouldn’t die. It should know to at least reconfigure and remove the dead nodes from its network.
  • I should have a way to see the nodes and their status.
  • I would want it to be distributed.  Regardless of nodes dying, It should finish the job.
  • I would love for it to be testable. I want to be able to simulate all the aspects of distributed app.

I see subprojects under Spring Cloud that handle log tracing, configuration management, and security.

I see subprojects under Spring Cloud that handle discovery. I see that I can upload my service to the cloud foundry for scalability.

I see that Spring Cloud lets me write a service once that I can deploy in any infrastructure.

But I do realize that as a Developer, I would expose parts of my application to this distribution. Not the entire application.

So, I would expose my services to Spring Cloud. And I decided to add it to Service layer.

The best way to include Spring Cloud projects in your application would be via the release train.

Each release train will know to pull right dependencies for specific subprojects for you.

These dependencies are complex. These dependencies are many projects weaved together for compliance.

In maven convention, release trains are BOM – Bill of Materials.

 

This needs some explanation….

 

A POM in maven is a Project Object Model. It contains all the libraries your application needs. If you use maven in your application, this is your pom.xml.

Spring Cloud projects could have intricate dependencies. Instead of letting Spring developers meddle with versions, Spring Cloud put together a BOM.

A BOM has in it defined all the libraries and their versions that a developer will need to pull.

A project leveraging Spring Cloud would bring this BOM into dependencyManagement in its POM.

Now you could bring a Spring Cloud subproject into your application. And the BOM would take care of the versions.

First I heard of BOM was in regards to JBoss AS7 application server.

I don’t know how easy it would be to bring Spring Cloud into your projects or pull it out yet. I am still learning this one.

4) Data layer Projects

The projects in this layer deal with data handling and manipulation. Let’s discuss them one by one below.

Spring AMQP

Spring AMQP abstracts out dealing with sending and receiving messages. It provides a low-level template to interact with data. It provides a connection factory. It provides a pre-built implementation in RabbitMQ.

It provides a listener container. For this reason, It almost fell into Service layer project. I had to think at least ten times before pulling it out of Service layer.

What made my decision to keep it in Data layer? I realized that with spring, a developer deals with messages via a template. Just like when he deals with the databases with jdbcTemplate.

Note: Spring Rabbit will bring RabbitMQ and Jackson libraries with it. I like RabbitMQ. You should check it out.

Spring LDAP

Some Spring projects are libraries. They can go into your project with minimal impact to the rest of the code.

Spring LDAP is a good example.

Add Spring LDAP to your project as a library. Create a service to look up users’ roles and swap it as an implementation to do authorization. If you use Spring Security, your application won’t even notice this change.

Spring LDAP is easy to introduce in your project. It is easy to pull out as well.

Spring Data

Spring Data, like Spring Cloud, is another umbrella project with many wonderful offerings.

Think of Spring Data as a common API to do CRUD operations for different databases. It has support for all the popular SQL as well as NoSQL databases.

With Spring Data, It is possible to swap your database with minimal code changes. Especially if you don’t do anything database-specific with your code.

If you use Hibernate, you could use Spring Data JPA underneath it. You will cut down your boilerplate code to a great extent.

You won’t be writing CREATE, READ, UPDATE and DELETE (CRUD) implementations now.

You won’t be implementing ANDs or FindBy methods anymore.

What will you do with all that extra time?

If you take Spring Data out of your project, you do all the boilerplate coding.

Spring Data provides a BOM for inclusion as well.

Spring Batch

Some Projects need extra time and resource investment.

Spring Batch, like Spring Web Flow, is another example of framework project. It is a framework with lots of features. And it requires commitment.

You can use Spring Batch to do any bulk processing job. These are long running queries that you need running in the background.

Spring Batch, like Spring Security, bring its own database schema. It takes time to learn the schema. I don’t know if you want to override their schema. So you will need database team approval here.

It is not easy to replace the features Spring Batch provides.

5) Foundation Projects

Some of the Spring Projects are platforms. In the image above, you see them at the bottom of the page.

These projects don’t belong to any one layer. Rather, they serve as a foundation for other Spring Projects. Let’s discuss them below.

Spring IO Platform

I am new to Spring IO.

If you were starting your application from scratch. And if you have decided to use Spring Framework, which library would you pull first?

You will need to include just one BOM: platform-bom

You could then bring in any of the Spring Projects without worrying about their versions. Or dependencies. BOM includes all the information.

Spring IO makes it easy to bring in what you need for your application.

Spring IO Platform provides Spring Boot under its belt.

You could use Spring Boot to run the application that you built using Spring IO Platform.

You are not tied to Spring IO platform. It generates the artifacts which you can decide to edit as you see fit.

Spring Boot

It is the easiest way to build and run your application. It gives you a command line tool to build an application in shortest time frame.

The tool generates the ‘same old same old’ code you write when you start an application.

I use WordPress for hosting Spring Tutorials Blog. Spring Boot is like installing a WordPress plugin for me.

The plugin gets installed and configured with default values with a click of a button.

It cuts down your getting up and running time.

Spring Boot is a facilitator of your application. You can get started with it and later decide to not leverage it.

Spring XD

I am new to Sping XD.

Spring Cloud provides a way to build and manage distributed services.

Spring XD provides a way to build and manage big data applications. Spring XD also helps you perform big data analytics on these applications.

This implies the ability to leverage big data Hadoop or other technologies with minimal effort.

I see that Spring XD is being rebuilt on top of Spring Boot and Spring Cloud.

I am sure Spring XD will simplify weaving all aspects of big data applications. Taking it out of your project may not be the best idea.

Summary

I hope I was able to provide a big picture of Spring Projects and Spring Ecosystem in general. I hope it did not confuse you.

Spring Framework Ecosystem – Introduction to Spring Projects