uses of spring framework in java - java

I have read that the main uses of spring is DI and AOP.
As far as i understand spring in 3 weeks , i feel that AOP can be done in aspectj
and DI is technique not specific to spring with API's.
so is i am missing anything. spring only creates beans.
is that it

I have read that the main uses of
spring is DI and AOP.
And the modules and libraries that come with both. Spring is a three-legged stool, in my opinion.
As far as i understand spring in 3
weeks , i feel that AOP can be done in
aspectj
Of course AOP can be done with AspectJ - as long as you don't mind byte code alterations. You can also use Spring's original AOP, which uses proxies and is less invasive.
and DI is technique not
specific to spring with API's.
I'm not sure I understand your point here. The wording isn't very clear. If your point is that DI can be done without Spring, then I'd say you are correct.
so is i am missing anything. spring
only creates beans.
I'd say you're missing everything.
is that it
What else do you want it to do?
What alternative do you prefer?
UPDATE:
I don't know about PHP, but there's a version of Spring for Python: Spring.py. This suggests to me that you can certainly do both DI and AOP in Python. I would say that they're possible in any language that's truly object-oriented. The ideas of DI and AOP are like any other OO pattern: language agnostic.

It is a container and MVC web application framework as well. There is also OSGI support. Spring consists of many parts/modules, which can be integrated with other frameworks such as Hibernate.
http://www.springsource.org/about

Spring is in my opinion a defacto standard when it comes to developing web applications/services, or even general application development.
It provides dependency injection as one of its strongpoints, to offer the conveniance of having all necessary tools in one package.
AoP on the other hand nicely integrates with Spring, but is in no way a fundamental reason to use spring. In fact, I dare to claim that alot of people probably don't explicitly (the underlying framework might still do it for you though). You can probably use spring a lifetime without having to explicitly use AoP.

Related

Spring project without xml

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 :)

Spring - what is it and why would I want to use it? [duplicate]

This question already has answers here:
What are Dependency Injection & Spring Framework about? [duplicate]
(10 answers)
Closed 9 years ago.
I keep seeing the phrase Dependency Injection whenever I search for Spring Framework in Google.
In plane English , can someone please explain what exactly is Spring , and why would I want
to use it ?
For example , a few days ago I've learned about Hibernate : it converts JAVA objects into records
in a DB table ... it's great , reduces the amount of code and a bunch of SQL statements .
So, how exactly does Spring helps me?
Spring is a framework which supports (among other things) dependency injection. Using Spring also makes it very easy to create web applications. Spring favors convention over configuration and was originally created as an alternative to Java EE (which was overly complex, verbose, and burdensome). But Spring is more than just dependency injection. It has core support for dependency injection, transaction management, web applications, data access, messaging, aspect-oriented programming, testing, and more.
Spring helps you by making it easy to inject dependencies, which reduces tight coupling between a class and specific implementations. For web applications, Spring follows the MVC (Model View Controller) pattern, and has specific annotations (and XML-based configuration) to help you write code that follows this pattern.
I suggest reading more about dependency injection, MVC, and the Spring framework in general to know more; there's far too much information to cover in just a single answer.
It creates loose coupling between elements due to DI. Best explanation as always by Martin Fowler
Recommended reading:
Spring in action
Official documentation
Plainer than "dependency injection"?
Let's ask what it is and why you'd use it. This is still the best explanation I know of.
Spring is more than dependency injection. It's a three legged stool:
dependency injection
aspect-oriented programming
libraries for web MVC, database, messaging, remoting, and more
There are other DI engines out there, like Guice.
If you have to ask, I'd suggest that you dig into it more before deciding.

why do we always put Hibernate, Spring and Strut in one app?

Definitely, I'm talking about working with MVC pattern.
Definitely, Hibernate make our life easier with Model layer.
But, Spring and Strut both work with Controller and View.
So, my question is: "I cant understand why other guy always put both Spring & Strut in one application while we need just one of them (Strut or Spring). If anyone understand that, please tell me, thanks!"
I'm just a kid in Java world, so any comments are appreciated.
But, Spring and Strut both work with Controller and View.
Spring is many things, as you can see in this diagram:
One of these things is the Spring MVC framework. I agree, it does not (usually) make sense to use Spring MVC together with Struts (although in large sites, different departments may have different requirements).
However, Spring is also many other things, most of all an IOC framework, and as such it makes perfect sense to integrate different model and view technologies.
Spring is both Spring - the IOC container and Spring MVC - the web action framework. Struts is only a web action framework. So if you prefer Struts over Spring MVC, but also want an IOC container, you will use Struts with Spring.
Additionally, Spring also provides declarative transaction management, a security framework, a set of JDBC helper classes, etc., that you might want to use in a Struts/Hibernate application.
I wouldn't say always. Personally, I have never put Spring and Struts together in the same application, and I am willing to bet that most Spring/Hibernate projects also do not also use Struts.
Spring isn't just MVC. It has much more integrations, such as database, security, DI etc. Usually you want to use one of that features if you use Spring (which doesn't also mean, that you have to use Spring MVC).
Lets say that Spring and Struts are both frameworks that do overlap in some aspects. Even if I think that, if you are utilizing spring to its full extent, there should be no need for struts at all. But people tend to stick with the stuff they are used to. As Struts has been around for quite some time there are a lot of applications based on this and a lot of people that have made a profession out of this and would never commit throwing that away. That's why I have seen quite a lot of these hybrid application around.
I think you misunderstand MVC Pattern in first place. Model is not about persistence, but about the business logic in first place. It usually involves some persistences and service classes. For this purpose, many people choose Hibernate for persistence and Spring IoC for dependency injection purpose.
For the View and Controller part of web application, a well known web mvc framework is Struts and Spring MVC. Spring itself is consists of many components, Spring IoC and Spring MVC is two of them. Spring MVC is an equivalent with Struts so you don't use them together. But it is ok to combine Struts and Spring IoC.
Struts - usually provides MVC framework (most of Production support & maintenance applications are already integrated with it).
Spring - to inject/ add new componenets without disturbing the existing java classes/ code.
IT mostly depends on your project requirement, in our project we have used JQuery there are lots of Struts tags are used at the UI Layer and that is the main reason we are using struts2 because struts2 is having very good integration with JQuery
Struts2-JQuery Tag Library is very useful hence we are using Struts2
+
Spring framework provides an easy way to manage the dependency. (because of its DI and IoC)
It can be easily integrated with struts 2 framework.
The ContextLoaderListener class is used to communicate spring application with struts 2.

What are the benefits of Spring Framework in Terms of Testing Application?

We have heard a lot about Benefits of Spring like it offers loose coupling, dependency injection, inversion of control etc but from testing point of view, I have couple of question.
What are the advantages of Spring Framework in terms of testing an application?
Why Application developed using Spring as considered my testable as opposed to other Web Application?
Please provide some useful examples as then it would be much more easier to understand explanation. I am new to Spring as such and want to understand precise benefits offered by Spring Framework from Application Developer point of view ?
An application geared towards dependency injection is typically more testable because resources are easily replaced with test-oriented resources. Consider a service object that uses a data access object; in production, you'd inject a DAO that talked to a backend data store. But for each test you could create a DAO that returned specific, known data; this allows you to focus on the object being tested.
Spring is one of many dependency injection frameworks for Java, albeit the most popular; any DI framework is going to give you this kind of benefit.

What java validation library should I use?

I'm kinda stuck on this decision. My project already uses Spring and Spring Blazeds integration but I don't think there will be any real web interface (HTML). I have limited experience with Spring MVC and the Spring validators which are OK, but I've read comments from people suggesting not to use Spring validators outside Spring MVC.
What validation framework would you recommend based on personal experience?
"..., but I've read comments from people suggesting not to use Spring validators outside Spring MVC..."
I'd like to see those citations. I've used the Spring DataBinding API outside Spring MVC, and it's terrific. I would say that criticism is unfounded.
Apache Commons Validator is an alternative, but I'd still recommend Spring's DataBinder first. Especially if you're already using Spring. The benefit of switching to anything else is more than offset by the cost of yet another dependency.
You can try Hibernate Validator which is in fact the reference implementation for JSR 303: Bean Validation. BTW, it is still in beta.

Categories