SpringFuse - Where to start guide

You have tried the Getting Started Tutorial but you are overwhelmed by all the technologies used in the generated code and you wonder where to start looking? This small guide is for you.

There is no magic

There is no magic, you will have to learn and understand all the technologies used by the generated source code to fully benefit from SpringFuse. There are 2 good news though:

- These technologies are highly demanded by the industry, you won't loose your time.

- The code generated by SpringFuse can be seen as a real application example, you can learn faster by studying it, running it and modifying it.

How come it runs?

You have no database, no web server installed on your computer and you are able to run the project from the tutorial! How can this project run?

The generated project uses Maven2 to compile and to run the web application. The pom.xml at the root of the project is for Maven what build.xml is for Ant or what a Makefile is for gcc.

Jetty starts a web server

There are hundreds of Maven plugins that can be added to your build process. One of them is the Jetty plugin. The jetty plugin starts an http server, a servlet container and run the generated project. It knows where are the JSP, the configurations files, the java classes etc. because the generated code follows Maven's project structure conventions. For example to start the web application it uses the file src/main/web/WEB-INF/web.xml

H2 Database for the tutorial

The tutorial uses the H2 database to keep things simpler. Thanks to H2, there is no need to install any Database server. Please refer to H2 quickstart guide to know more about H2 Database. During development it can be pretty convenient to work with H2 and to switch to a 'real' database later.

Maven 2

Thanks to Maven 2 and the pom.xml file at the root of your project, all the jar dependencies are automatically downloaded from Maven central repository.

Maven is a bit cumbersome at first. It takes time to get into it, so for the moment we recommend you to do not spend too much time on it. It works, you can learn it later. If you are really impatient, then check these Maven resources.

The Spring Framework

The generated code heavily relies on the Spring Framework which is a really powerful framework. This framework allows to instanciate beans (i.e. components) and resolve their dependencies using an Inversion of Control design pattern.

Inversion of Control

You may want to read a little bit about Spring before getting into the code. Check the Spring Reference Documentation (you may start with Chapter 3. The IoC container)

You should also know that Spring uses Java annotations to simplify the configuration. An annotation in Java start with the @ character.

For example, you can declare beans using an annotation such as @Service, @Repository or even @Controller and tell Spring to resolve the various dependencies thanks to the @Autowired annotation.

You can search these annotations in the generated code to understand how it works and how simple it is compare to verbose configuration files.

Once you have scratched the surface and you get the concept, you should watch this video on configuring Spring. Do not resign yet, it takes time to digest Spring and to see the light. It is normal if you suffer here.

Take the time you need to understand how Spring wires together all the various 'beans'.

Spring MVC

SpringFuse generates some code that uses the Spring Web MVC framework. Check Chapter 13. Web MVC framework. You can also check the Spring Web MVC configuration files which are under src/main/resources/spring and also the famous src/main/webapp/WEB-INF/web.xml file. Search the generated code for the various generated controller under <root package>/web/controller/domain, they use the @Controller, @RequestMapping, etc. annotations.

Declarative Transaction Management

The generated code uses the @Transactional annotation to demarc transaction boundaries. There is a lot of magic behind it and even if at the beginning you can take it for granted, it is important to understand the ins-and-outs. It illustrates how Spring can provide a simple and elegant solution that addresses a complex problem.

Declarative transaction management is possible thanks to the use of AOP. You may check the Spring Reference documentation about AOP ( Chapter 6. Aspect Oriented Programming with Spring) but it is a little bit complex.

To know more about transaction management you should watch this great presentation: Transaction Management Strategies in Mission Critical Applications. Do not be scared by the title. Juergen Hoeller, one of the Spring Framework founders, is pretty pedagogic. The video last 1h30 and worth gold. You should take the time to watch it and understand some of the concepts Juergen covers. Listen carefully to the part that starts around 00:13:45 (hour:minutes:seconds) about Transaction Demarcation, pay attention to what he says about the read-only flag around 00:25:00. He mention the @Transactional annotation around 00:35:35. Also around 00:43:20 he goes into pragmatic details that can help understand what it really does when the JDBC transaction manager is used.

Open Session In View

The generated code uses the Spring's OpenSessionInViewFilter that implements the Open Session In View pattern. Take the time to understand it, it is closely related to transaction management in a web environment.

Hibernate

The generated code uses Hibernate. We have grouped together the Hibernate dependent code in the <root package>/hibernate package. The code is relatively simple but keep in mind that Hibernate is complex. The Java classes that map the database tables are in the <root package>/domain package. The Hibernate configuration files are in are the src/main/resources/hibernate folder. Take also a look at the src/main/resources/spring/applicationContext-hibernate.xml configuration.

You can do very complex things with Hibernate. We try to do not overuse it. For example we do not use inheritance or complex mappings. You have to dig into the Hibernate Reference Documentation. Again it will take time, you will suffer. This is normal. We hope SpringFuse helps.