How does spring framework assist in application development? - java

Am a bit confused here! how does spring framework assist in general development of an application? I use django framework and i can quickly explain to a layman how all parts fit together(Django, Python, templates, packages etc) to produce an excellent web application, but when i look at spring i get a bit lost! Am looking answers but not limited to the following;
Can someone please tell me how they have used spring to produce applications?
Can someone please point to me some real world applications done in spring ( iread somewhere linkedin.com is done with spring!
Can someone please tell me how this pieces come together ( Strut, javascript, glassfish/jboss, apache, etc and ofcourse spring) to produce an application?
How many separate pieces of software do you need to produce an application using spring?
How easy is it to produce application using spring framework?
I need the gory details :)
Gath

There are a number of Spring projects, but the initial Spring (POJO) project came about because of the perceived difficulties of working with J2EE. You'll find a number of projects on the SpringSource website that have grown out of this, but rather than being lumped into one framework they've taken a more modular approach. For all the products they produce see:
http://www.springsource.org/projects
Q. Can someone please tell me how they have used spring to produce applications?
Spring provides a number of features, but the most oft used one is that of dependency injection. This allows you to wire together components (e.g. Javabeans) by declaring the relationships in XML/Annotations. The Spring container then reads this information and constructs the bean hierarchy at runtime. A standard way to describe the beans in XML is the Application Context.
http://static.springframework.org/spring/docs/2.0.x/reference/beans.html
Q. Can someone please point to me some real world applications done in spring ( iread somewhere linkedin.com is done with spring!
There are lots of applications built using Spring. I'm not sure of big commercial projects, but I expect there to be many.
Q. Can someone please tell me how this pieces come together ( Strut, javascript, glassfish/jboss, apache, etc and of course spring) to produce an application?
Spring is normally integrated with other frameworks, there are various hooks into these frameworks and you need to look at each one separately in order to understand what they are all about. Struts and Spring framework integration can be found here:
http://www.ibm.com/developerworks/web/library/j-sr2.html
With Glasshfish/JBoss it's more about how you configure your application in relation to Spring rather than the application server. See this:
http://static.springframework.org/spring/docs/2.5.x/reference/web-integration.html
Q. How many separate pieces of software do you need to produce an application using spring?
e.g. A web application would consist of Spring MVC + Spring Backend. A desktop application - Java Swing + Spring backend. In terms of the Spring framework itself (configured with XML) it would involve:
Create your standard JavaBean classes (for services/DTO's/DAO's)
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public ExampleBean(
AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
this.beanOne = anotherBean;
this.beanTwo = yetAnotherBean;
this.i = i;
}
}
Declaring beans within your application context file
Integrate Spring with your web application/application via web.xml etc...
These are taken from the Spring docs btw... see:
http://static.springframework.org/spring/docs/2.0.x/reference/beans.html
Q. How easy is it to produce application using spring framework?
Very easy, but again dependent on what you're building. If you're just using the Spring POJO framework to build a service and integration tier then it's fairly simple. If you have to build a web application layer, then it's a little more complicated (not hugely at all) to understand the internals.
Hope that helps...

Can someone please tell me how they have used spring to produce applications?
Put the Spring JARs in your CLASSPATH, follow the Spring idiom (e.g., interfaces to delineate layers), and use it to glue your code together.
Can someone please point to me some real world applications done in spring ( iread somewhere linkedin.com is done with spring!
Here's one. Running in production now for three years and counting.
Can someone please tell me how this pieces come together ( Strut, javascript, glassfish/jboss, apache, etc and ofcourse spring) to produce an application?
Struts is one choice for web tier; JavaScript is something you can use to make your client dynamic; Glassfish/JBOSS/WebLogic/WebSphere/Tomcat/Spring DM are all app server choices for deploying your Spring app; Apache is an HTTP web server; Spring and your code go on the app server.
How many separate pieces of software do you need to produce an application using spring?
You need an app server and probably a database, a browser, your code and Spring.
How easy is it to produce application using spring framework?
How good a programmer are you? Depends on your knowledge.
Spring certainly made my life easier once I understood it.

Spring is a pretty large framework, it's going to be hard for anyone to summarize everything here. I think the biggest plus for using Spring is its dependency-injection support. It can be used in any type of application, and provides a ton of framework features and utilities. If you are really interested, I'd recommend starting with the docs on springsource.org:
http://www.springsource.org/documentation
There are a lot of tutorials out on the web too. To fully appreciate it, you should try writing a few example apps to get the feel for it, and see what's available.

Just to add another dimension - if you are interested in building java web apps, and have no constraints (like legacy code to integrate with/update), then take a look at grails. its the best-est web framework to write web apps in. Of course, it uses spring internally too, but its fairly abstracted away from you. In fact, i'd liken it to django.

You can start with this http://static.springframework.org/docs/Spring-MVC-step-by-step/. This will provide the basic framework for you and then spring is huge so depending upon what you need you can explore spring and use it.
This has worked for me.

If you're talking about Struts and JavaScript at the same time, it means that you don't know what you're talking about, for Web Development in general.
What is what?
- You have frameworks in Java such as Spring, Struts, etc. which help you build Java based Web applications. Those Java Frameworks integrate generally Web with ORM tools, and other technologies.
- You have Servlet Containers such as Tomcat, Jetty, etc on which you can run classic Java Web Applications. J2EE containers such as JBoss, Glassfish embed a servlet Container such as Tomcat and provide a J2EE container and sometimes other tools, to deploy Java Enterprise applications.
Choosing a Framework
Choosing a Framework depends on how you feel about it and how it fits your needs. For example why are you using Django instead of TurboGears? Some people will tell you that Django sucks while TurboGears rocks, etc.
Whatever you do it's your call, read at the documentation and pick one application server and one or many frameworks to build your application.

Related

Java Spring Boot Hibernate, JPA, MVC, REST confusion

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.

Is it better to use spring mvc with hibernate?

I am going to develop small ERP System. The product has only one major requirement to support multiple databases.
I have planned to use
Front side: JSP+JSTL+JQuery [I have good command on that] + I have create my own custom component for re-useability and full control of my component.
Back-end: Using hibernate[ORM] framework [due to Cross database] and I have also good knowledge of it.
I am happy with above and feeling confident to build product, soon.
BUT
My friend made me unhappy ;)
My friend suggest me that you should use spring MVC with hibernate, because when your product become large,it create a problem in future due to many developers involvement.So, you should use framework , so that every developer follow the pattern and then your product remain stable.
But I am already follow hibernate pattern :)
I have no any knowledge of Spring MVC. When I search about spring MVC , I found it is a different thing than my requirement and also find Cross database context issue, if I use Spring MVC with hibernate.
Either should I used spring MVC or not. If yes, I have to learn then Spring MVC and
I do not want to waste my time, if spring is not suitable for me.
That's why I am consulting that forum , to go for right direction.
Actually, Spring MVC does not interfere with any of the components you have chosen already. It just introduces a way to structure your frontend - backend interface in a standard way (MVC pattern).
I actually run the very same setup and is very happy with it. (Jquery, JSP, Spring MVC, Hibernate).
Other than structure, you will get easy return of invested time in Spring MVC when you want to do Ajax heavy things (as I assume you want in an ERP app). It's trivial to build AJAX/JSON stuff with jQuery and SpringMVC.
Using Spring MVC will make your application structured. This will help in future maintenance.
Your code will look clean and you can separate your code into components and Spring will help you wire them together and you might decide to combine them in different ways, or wish to make it easy to swap out one component for another depending on different settings or environments. This would be great for multiple databases when used with hibernate.
Learning spring will not take much time if you have a good knowledge of Java EE.

Architectural Differences in Java MVC Web Frameworks

I'm trying to choose an AJAX-friendly Java framework for my first web application and am interested in first
understanding the architectural differences between the different flavors that are out there.
I like the concept of MVC frameworks, and so am primarily considering the following:
Any JSF variety (ICEFaces, RichFaces, PrimeFaces, etc.)
Spring Web Flow
ZK
Wicket
I've downloaded each of these projects and tried to follow their samples/tutorials, and there is
so much information to ingest I figured I'd take a breather and come here to cover some preliminaries
first.
I'm interested in how each of these frameworks implements the MVC pattern. Obviously, something rooted
in JSF (like ICEFaces) is going to have a different architecture than Spring. I'm sure that this is a
huge question, so I'm not looking for a full-blown tutorial on each of these frameworks; I'm just
curious as to what sort of artifacts (Java sources, XML config files, etc.) a developer has to write in
order to build a single AJAX-driven page using these. I'm interested in the differences to their approach,
nothing more.
For instance, I would imagine that each framework at some point uses a FrontController (or its likes) to
map HttpRequests to the right Controller implementation. That Controller (bean) would then need to do
some processing, possibly hit the database for some information (using ormapping and forming the Model), and
then construct a View/HttpResponse to send back to the client. This is an oversimplification I'm sure, but
there has to be an easy way to explain the high-level architecture for how each of these frameworks accomplishes
that.
Struts uses the ActionServlet (with Struts2 now its just Action) as the controller and model and jsp is the view.
For Spring MVC is achieved by DispatcherServlet which does the routing and Model is not bound to any framework related object you can use any.
JSF - UI jsp or jsf itself, Model - ManagedBean, Controller - FacesServlet.
I did some similar search for my own project a while ago, have a look at the links below:
Comparison based on multiple parameters : http://static.raibledesigns.com/repository/presentations/ComparingJavaWebFrameworks.pdf
Difference between JSF and Struts
http://struts.apache.org/2.0.14/docs/what-are-the-fundamental-differences-between-struts-and-jsf.html
Somewhat related post
https://stackoverflow.com/questions/7633583/which-mvc-is-better-spring-or-struts
Spring and JSF
http://blog.springsource.org/2007/04/21/what-spring-web-flow-offers-jsf-developers/
Spring MVC : http://static.springsource.org/spring/docs/2.0.x/reference/mvc.html
Best Fit For JSF Component Library: Primefaces based on my own experience
From IBM Clearing the FUD : http://www.ibm.com/developerworks/library/j-jsf1/
Hope this gives you some insight.
Have a look at Matt Raible's talk on Comparing JVM Web Frameworks here. You can also consider looking at Spring MVC and 'Tapestry`.
Also, this link gives you a matrix on capabilities of various java web frameworks.
You should also check out the Play framework. I have used it a little and really like it.
It is very easy to get started with minimal configuration (reminds me of Rails).
http://www.playframework.org/

Considering moving from Java/Spring MVC to Grails

I'm currently using Java & Spring (MVC) to create a webapp, and I'm considering moving to Grails. I'd appreciate feedback/insight on the following:
I have multiple application contexts in the current Java/Spring webapp that I load through the web.xml ContextLoaderListener; is it possible to have multiple application contexts in Grails? If, yes, how?
This webapp extensively uses a CXF restful web service and the current Java/Spring webapp uses the bundled CXF HTTP client. Can I continue to use the (Java) CXF HTTP Client in Grails?
I implemented Spring Security using a custom implementation of UserDetails and UserDetailsService, can I re-use these implementations in Grails "as is" or must I re-implement them?
There is an instance where I've relied on Spring's jdbc template (rather than the available ORM) and an additional data source I defined in app context, can I re-use this in Grails?
I plan on using Maven as the project management tool; are there any issues of using Maven with Grails where there is a combination of groovy and java?
Edit:
I'm considering moving to Grails to make the development of the web component of the webapp "faster," a la Ruby-on-Rails. Also, I'm considering Grails rather than say Ruby-on-Rails, because I want to continue to use the JVM and I've dabbled with Grails in the past and it was fairly easy to pick-up and use.
Probably. Grails uses a sub-class of Spring's ContextLoaderListener class which it configures in the web.xml file. I can answer more precisely if you let me know how you do it with Spring MVC.
Yes. You might even be interested in the CXF plugin, although I can't vouch for it:
http://grails.org/plugin/cxf
You should be able to use them as-is. However, you might want to check whether this is easily done with the Spring Security plugin. I believe it is, but you'll be able to get a definitive answer from Burt Beckwith, the author of the plugin.
Yes. You can also get hold of the Hibernate session factory to do raw Hibernate stuff. GORM can also work with multiple data sources:
http://grails.org/plugin/datasources
Another Burt Beckwith one :)
It depends on what you mean by "a combination of Groovy and Java". You can build Grails projects with Maven, but the integration isn't entirely smooth. If you have Java and Groovy in your Grails project, then that's taken care of automatically.
In response to Bozho, I use standard Grails services + GORM and wouldn't do it any other way. Note that if you use Java for services and the domain model, you won't have automatic reloading of services. You also lose the benefits of expressiveness and conciseness that Groovy bring.
If you want, you can use static types in Grails services to make it easier for your IDE to provide code completion. It can also give you hints on properties and methods it doesn't recognise (which would corresponding to Java compilation errors). That said, even if you use static types, Groovy can't do type checks at compilation time. You'll only find out about them at runtime.
You can do all these things in grails. It supports all existing Java classes and spring configurations (grails is built ontop of spring mvc)
However, I really wouldn't recommend moving the whole application to grails. You can perhaps move only the web layer, if you have web developers that are not java experts.
The service layer, the data access, etc, better remain pure Java. That is, only your web controllers - the components that gather the user input, handle http requests and sessions, should use grails. The rest - the stateless service classes and your domain model would better be Java. That's my opinion, but I have already some experience with grails, and static typing in the service layer will save you much trouble.
2) Yes you can use CXF as is. There is a nice layer on top of CXF called GroovyWS. I have only used it for consuming SOAP services, but maybe it has something for REST as well. It's really easy to use.
For consuming REST services I have used HTTP Builder
4) Yes. You can continue to use e.g. spring config for configuring the datasource, or any other way you do it today. Multiple datasources is no problem.
5) I have recently tried using Grails (1.2.1) with Maven. It works, but there has been some issues with both Maven and Grails trying to do dependency management. The documentation is maybe the worst part. I haven't tried upgrading to 1.3 yet because of some major Maven-related JIRAs, but 1.3.2 is right around the corner, and those issues have now been resolved :) There will also be a 1.3.2 maven archetype. Looking forward to that. "Deployment and resolution of plugins from Maven repositories" is one of the new features of Grails 1.3, so things are probably better. Roadmap for 1.3.2 says release today, but there are 8 issues left at time speaking, so my guess would be tomorrow, the Grails releases are usually on time. If you can wait for that, you will probably save yourself some trouble.
If you are looking for rapid application development but aren't otherwise particularly enthused about groovy, you should look into spring-roo. It offers the same kind of RAD functionality, but builds a completely standard java + ORM + spring-mvc app (which has no actual dependencies (runtime or compile) on roo). It's definitely not as mature as grails, but you may find that it better suits your existing experience with statically typed java code and existing ORM, etc. I've only done a couple of small pet projects in roo, but I've been very impressed so far, particularly with how easy it is to customize the generated code and move back and forth between written and generated code. The initial tutorial is very rapid and quite revealing.

I'm interviewing for a j2EE position using the Spring Framework; help me brush up

I'll be interviewing for a J2EE job using the Spring Framework next week. I've used Spring in my last couple of positions, but I probably want to brush up on it.
What should I keep in mind, and what web sites should look at, to brush up?
I wouldn't ask about the framework in itself, but in which cases would be convenient to apply its features, like when to use LoadTimeWeaving aspects or whether to DI domain objects or not.
I see spring as a tool for solving problems, and I'd like the other person to tell me how would he apply it, when, and most important in which case he wouldn't use it.
I think excellent way to brush up on spring framework skills are to cover concepts given in DZone's RefCards. They are concise PDF format documents.
Spring Configuration RefCardz
Spring Annotations RefCards
Spring and Flex Configuration
Hope this helps!!
Peacefulfire
You might want to look at some of the newer projects and updates that SpringSource is working on.
Spring 3.0 (Key new features are RESTful support on top of MVC, and new expression language)
Spring dm Server (OSGi-based server)
Spring tc Server (preconfigured tomcat)
Spring AMS (centralised management and monitoring of assets)
Spring Security (Now supports expression language which makes for much richer declarative security demarcations)
Spring Integration (Implements a number of the patterns from Enterprise Integration Patterns: gateways, splitters, aggregators, transformers, etc..)
Grails
Just announced at SpringOne:
Roo - a build assistant with command-line options similar to grails for creating domain classes, controllers, and views, but for vanilla java instead of groovy.
SpringSource Tool Suite is now free!

Categories