I’m going to start writing my application for athletes based on Spring 2.5.6 and Java 6 (as I’ve read that Java 7 is less compatibile with this Spring version). I’ve couple of questions as I’m not familiar with such an old versions. (I need to write it for my Engineer’s Thesis – comparing app written in old version of Spring and in newest version of Sping Boot)
I know already I need web.xml, but do I need servlets to handle http requests or there is some other common way to do it? I won’t have any front sides (like jsp files) – just backend.
I mean, I want to build the most common app based on Spring 2.5 and java 6 and to do it I need these two things to do any request, right?
What about declaring beans in these versions. Do I need to do it in xml like …. or I can annotate class with for example #Component (as #Service propably does not exist in this version). Which way should I go? I would like the differences to be most visible - beetwen spring boot and old spring, so declaring beans in xml would be fine (as in SpringBoot I have annotation like #Service, #Repository etc) - but it’s hard to explain in my Engineer's Thesis why I’ve chosen for example declaring beans in xml when I could do it easily with annotations.
Related
I'm learning Java SE and Spring Boot for a half year now, and watched different courses, and they teaching different ways, and I'm just confused which one does what?
In one course, we're using Eclipse, Spring MVC and Hibernate with MySQL, and writing everything like Servlet, Hibernate configure file, factory, session, and it's just a bit complicated how to do a query for example. In the other course, we're using Spring Initializr, Maven, REST API with PostgreSQL, and it's so much easier, we implementing CRUD repository, and just one line, we can do the query.
And I'm lost at this point. These what I just mention, what exactly we use them for? Why we don't use the simple way in the first course? What we done in the second that I don't have to create a factory and a session to do a simple query?
Is there any post, video or anything about this, for me to understand it?
There are always different ways to solve the same problem. Spring Boot offers you a lot of features to simplify your development. But you don't have to use them. You can always try to implement stuff by yourself. But most of the time, the built in features like the CRUD repository are sufficient to solve your problem.
I can't tell you the exact reasons, why the author of the first course did it this way. Maybe he or she wanted to show the principles, that are hidden beneath the features. Maybe it is just an older course or it is for Spring and not Spring Boot. Spring Boot simplified the setup for Spring and made many advancements.
Spring Framework has been there for a really long time, the ways you have seen are both valid ways, and as far as I understood by your statements is, one way is working with Spring MVC and the second is working with spring boot and spring boot makes things really easy.
You need to understand the difference between the spring MVC framework and spring-boot.
In spring MVC Framework we manage things with configuration files, like XML files and we also fire queries by opening a session first, and then only we can query. But in Spring-boot these things happens behind the scene and that is why it becomes so easy to work with spring-boot but anyhow we still need to understand all this stuff to be able to work properly with this framework.
Spring MVC is a complete HTTP-oriented MVC framework managed by the Spring Framework and based in Servlets. It would be equivalent to JSF in the JavaEE stack. The most popular elements in it are classes annotated with #Controller, where you implement methods you can access using different HTTP requests. It has an equivalent #RestController to implement REST-based APIs.
Spring boot is a utility for setting up applications quickly, offering an out-of-the-box configuration in order to build Spring-powered applications. As you may know, Spring integrates a wide range of different modules under its umbrella, as spring-core, spring-data, spring-web (which includes Spring MVC, by the way), and so on. With this tool, you can tell Spring how many of them to use and you'll get a fast setup for them (you are allowed to change it by yourself later on).
Spring boot is just an auto-configuration tool. Spring MVC is a web framework
Spring boot = Spring MVC + Auto Configuration(Don't need to write xml file for configurations) + Server(You can have embedded server).
Java EE, Spring and Springboot are not the same.
Spring is based on Java EE.
Spring boot is an 'extension' of Spring, especially with auto-configuration.
There are multiple frameworks or libraries which comes with its own advantage and disadvantages, however you need to choose the TechStack that suits your particular application's requirements.
So if you need to build a web app you can use Java Servlet ,but you have to handles multiple concerns yourself and it involves lot of configuration , but there are many frameworks like Spring,Struts,etc which makes the take easy
Similar way you can manually manage dependencies or you can use Maven or gradle to handle the dependencies and building process
Similar way if you need to connect to a Database you can directly use JDBC but there ar multiple ORM(Object Relational Mappers) available which will make the task easier like Hibernate, Jooq, etc
Regarding your question there is Spring framework and also SpringBoot , main motto of Springboot is that it prefers "conventions over configuration" meaning you only need to write very little code to get started and it comes with many starter-packs which basically are pretty much preconfigured, so you can build application easily
Different frameworks and libraries comes with its own learning curve but they reduce the time required for configuration and troubleshooting
Read about Spring MVC and Spring-boot framework. What you have mentioned is first is spring mvc and other is spring boot framework. make you understanding with the questions like, what problem spring boot solves ?? that was or is there in spring mvc.
JPA: The Java Persistence API (JPA) is one possible approach to ORM. Via JPA the developer can map, store, update and retrieve data from relational databases to Java objects and vice versa.
Hibernate: Hibernate is an open-source object-relational mapping(ORM) tool for Java. It provides a framework for mapping an object-oriented domain model to a traditional relational database.
MVC: The Model-View-Controller (MVC) is an architectural pattern that separates an application into three main logical components: the model, the view, and the controller.
JAVA: One of the most widely used programming languages, Java is used as the server-side language for most back-end development projects, including those involving big data and Android development.
Springboot: Spring Boot is an open-source micro-framework. Spring Boot helps developers create applications that just run. Springboot is a JAVA framework.
REST: Representational state transfer (REST) is a software architectural style that defines a set of constraints to be used for creating Web services.
I have two application. 1 application is old version of spring that is based on xml configuration. There are no annotations used. From This application stored procedures are used for CRUD operations. For Poc I have developed a simple CRUD application using spring boot, I have exposed them a restful services.
Now I have to consume this new rest service in my old application. How to do it? I am looking to use restful template which is not available in spring 2.5.
As you already mentioned, RestTemplate is only available as of spring version 3.0 or later.
So, the basic options I see are:
update your spring version from 2.5.x to at least 3.x
use an external lib offering help in consuming rest services
make up your "own"
If the first is an option, go for it. I can not really comment on the second option, but I'm pretty sure there is no widely used, actively maintained library using something comparable to spring's template pattern.
So I'd go for the third option. The quotes around "own" are there because I'd make use of the spring RestTemplate code (as of spring version 3.0). As spring is using an Apache 2.0 license you may use and repackage part of the code.
Start from a stripped down version of RestOperations (versions later than spring 3.0 add methods to this interface, e.g. using ResponseEntity, so really start from 3.0). Continuing with the code from RestTemplate you may get frustrated at first, as spring is dragging in quite a few classes introduced in spring 3.0 (MessageConverter stuff, ...). But this is due to the modularity of spring, not due to large amounts of code.
Just make sure you have a canonical way of mapping the spring packages to your own name space, so not to get confused.
Incorporating spring's source code using patterns you use in other projects (like the template mechanism) is imho a great way of getting a deeper understanding of the code base you usually just consume; another benefit.
I'm new to Spring.
The goal is to learn Spring, to use Spring as a production application as it is industry standard.
The requirements of the app:
Hibernate, Security, MVC, RESTful, DI, etc.
The other Spring frameworks might be added in future.
I'm reading "Spring in Action. Third Edition." by Craig Walls.
He gave the examples how to use annotations, but anyway .xml is used.
I'm wonder if I can write the application using only java classes to configure all modules in the application.
I found Spring Boot gives ability to develop not using xml files. However I read the article http://steveperkins.com/use-spring-boot-next-project/ and author said Boot is not ready to be used for production applications.
As far as I understood Boot hides all config work from me. Also my concern is that in future java-developers who knows Spring won't be able to deal with Spring Boot and I wouldn't find proper engineers for the project.
Based upon this I have the following questions:
Is it possible to avoid using xml in Spring or better to mix xml files and annotations?
Is it easy for Spring developers to work with Spring Boot?
Am I able to learn Spring using Spring Boot?
Is Spring Boot is mature enough to use it in production?
Is it possible to avoid using xml in Spring or better to mix xml files and annotations?
Yes, it is. Spring now promotes Java configuration, and it's perfectly doable (I'm doing it) and even easy to only use Java to configure your Spring app. Even without using Boot.
Is it east for Spring developers to work with Spring Boot?
Why wouldn't it? It's well documented, and is based on Spring best practices.
Am I able to learn Spring using Spring Boot?
How could I answer that. Try doing it, and you'll see if you're able or not.
Is Spring Boot is mature enough to use it in production?
Yes, it is. The articleyou linked to is one year old. Spring developers have worked a lot on Boot since then. And Spring uses Boot internally to host their own spring.io web application. See https://github.com/spring-io/sagan
JB Nizet answered 3 answers very clearly. Just an addition about production readiness from my side. We are currently using Spring Boot for an application which we intend to move to production. There has not been any issue till now in prototyping and testing phase. It is very convenient and avoids boilerplate and gives production ready, standalone jar file with embedded server. You can also chose to build war file if you prefer.
"Am I able to learn Spring using Spring Boot?"
As you mentioned that you are new to Spring, it would probably be easier for you to pick up Spring Boot quickly.
To get started, if you are interested, following is the link to a webinar by Josh Long which gives you a really good insight of how easy it is to pick up Spring Boot:
https://www.youtube.com/watch?v=eCos5VTtZoI
I don't know much about Spring Boot but I know pretty much about spring.
First of all you can use both annotations and xml configuration file/s in the same project. That is the most common way as far as I know.
There is also JavaConfig configuration option in which you don't use any xml files instead you use ordinary java class with #Configuration annotation. I didn't use and not saw much usage also.
You can make a spring webapp without any xml, although spring security was ugly to configure last time I looked at that. For a webapp you need to implement WebApplicationInitializer, create an application context and register your #Configuration file(s) with the context. Then you register the dispatcher servlet and you're all set!
I was nearly in the same boat four months ago when I started working on my web app & chose Spring as the platform after evaluating many choices. I also started with Spring in Action but got frustrated when the examples provided by the author didn't work (Spring basic MVC app not working). Since I was just starting, I was looking for some very basic but working examples. But unfortunately, most of the examples which came along with Spring text books, didn't work straight out of the box.
I would like to suggest few Spring resources which I found useful for starters:
http://springbyexample.org/
http://www.petrikainulainen.net/tutorials/
http://www.mkyong.com/tutorials/spring-tutorials/
Pro Spring 4th Edition
Spring Documentation (must read, but take your time to understand the concepts)
Now, to answer your questions, although a bit differently:
Is it possible to avoid using xml in Spring or better to mix xml files and annotations
Now a days, you would find Annotations a lot in Spring code available on net/SO along with XML configuration. However, you can certainly avoid XML if you wish.
Is it easy for Spring developers to work with Spring Boot?
Am I able to learn Spring using Spring Boot?
Is Spring Boot is mature enough to use it in production?
My personal opinion would be to go with Spring Boot only if you believe it offers you certain advantages which are not possible to achieve otherwise. Remember, you may save time now but later on, it would be an additional dependency in your app and you may need to understand its architecture to debug it if things go wrong OR to enhance it as per your app requirements. Better to have minimal dependencies, my learning till now :)
I've been told I have to develop a Spring 2 application (no idea myself why they don't want a Spring 3 application since they use Java 5).
The problem is that all my experience is with Spring 3.
I know there's a reference manual but I find that those are a lot easier when moving forward and not backward.
So I was wondering what the major differences are between these 2.
I'm expecting to have to do a lot more configuration work in xmls.
In Spring 3 I mostly use the #RequestMapping, #ModelMapping, path binding in the jsp, automatic type conversion from String to date, hibernate-validator, ...
Any pointers would be appreciated.
Firstly, if you haven't been given a reason for using Spring 2, can't you find out what that reason is? They may not be aware of Spring 3's existence. It can't hurt to ask, especially if you tell them you're more proficient in Spring 3.
Failing that, then most of the features you've mentioned work OK in Spring 2.5.6 (including annotation-based MVC, form tags), with the notable exception of the Hibernate Validator integration. You'll have to use the other validation techniques mentioned in the ref manual instead.
Most of the Spring 3 new stuff was really under the covers, with much of the infrastructure rewritten.
I belive the REST support stuff was new in Spring 3.0. So you will not be able to bind a path variable to an method variable
//Spring 3.0
#RequestMapping("/customer/{id}/logoContent")
public void getCustomer(#PathVariable("id") final Customer customer) {...}
An additional source of the major changed between Spring 2.5 and Spring 3.0 could be the Spring Blog. This blog has a lot of posts showing the new features of Spring 3.0 (autumn of 2009 till middle of 2010). -- So this post can give you a hint what possible is not so easy in 2.5.
In my personal opinion the major different between Spring 2 and Spring 3 was the shift of configuration from Xml (Spring 2) to Annotations (Spring 3).
BTW: You should check if there is still a security/bug support for spring 2!
Spring Framework 3 seems to be right around the corner, but the GA version is 2.5.6.
If this is the first time I'm approaching the subject, should I start with the stable version, or should I start with the new version and save myself migration issues?
How different is version 3 from version 2? How near is Spring 3?
I would start with Spring 3 for various reasons:
full Java 5 support (this is main reason for adopting Spring 3 for me)
Spring MVC support is deeply change between spring 2 and 3 (notably REST support). Learning spring 2 MVC is not a far-seeing imho.
new module organization (if you start with Spring 3, you don't need to migrate packages in the future)
OSGI compabitiliy
Ivy support
You don't need to worry about bugs or incomplete documentation since you are still learning the framework concepts. In conclusion, learning Spring 3 instead Spring 2 is a far-seeing choice.
However a very good introduction to Spring 2.x is given by Spring in Action, an excellent book about the subject.
I would start with the stable version. Less bugs, more documentation, more stable and easier to find answers to issues.. Spring 3 won't be vastly different. There is a Spring 3 reference manual but it's incomplete for the changes. Also, since Spring 3 is only on a milestone release (M3), it's still subject to change.
You can read What's New in Spring 3.0 but I imagine a lot of it won't mean anything to you yet.
It's important to know required java language version:
Spring 3 needs java 1.5
Spring 2.5 needs java 1.4
Spring 2.0 and older works on java 1.3 (if you still work on legacy servers, I recently used Spring 2.0 on WAS 5.0)
Start with Spring 2 for the simple reason that if you are doing a project right now, it will be in Spring 2 and not Spring 3. I've been exploring Spring 3 for a while but working in Spring 2 and have to say that they have added a lot of nice new features in Spring 3.
This actually makes working in Spring 2 annoying because I look for the things and they aren't there. It's a little annoying - spare yourself this, I don't think you'll have any trouble learning Spring 3.
If you're learning it to start a new project that's going to be starting when Spring 3 is out learn Spring 3. If you're going to be working on an existing project that's already on Spring 2, learn that. The changes are significant enough that projects currently using 2.x are not going to jump immediately.
I wouldn't worry too much about the version; probably starting with the actual release (i.e. 2) is better than starting with one that's still in milestone releases. However, Spring 2.5, and particularly Spring MVC, can use two approaches; configuring primarily using XML, or configuring primarily using annotations. Spring is definitely moving in the direction of annotations, so if you get used to using them you should be OK.
It is possible use xml configuration and annotations in the same project.
I've made some project in 2.5 but now would like move to Spring 3.
I understand that is not easy (it is not enough only change library - need re-write code).