Win a copy of Head First Go this week in the Go forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Spring, XML, beans: Okay, so what's it all mean?  RSS feed

 
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every topic I've grasped in Java, I pretty much had to learn it without actually being told why it matters, meaning your learn what to do, but not when to do it.  

So, learning Spring, I can make a bean, and call it in a class.  So, what? I can't comprehend a real practical example of a problem I have where'd I'd need to solve it with a bean or an xml file.  

What would happen if you didn't use Spring?  How would you use it if you're making an online shopping site, a college course enrollment page, or something similar?  

I just don't feel in going to retain any of this if I don't know why I'm learning it, and for some reason, I can't find a single source on the internet that explains it.
 
Ranch Hand
Posts: 277
2
Fedora Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spring has many different projects that do different things like mvc, jpa, blahblah theres alot.    When just talkin about beans that would be dependency injection.  You dont know when you would use it because you havent yet done dependency injection.

if you want to understand why people use that you need to understand dependency injection.  
Dependency injection makes it easier to change out inplementations of an interface , sharing a class you want only instance of in an app.  Theres probably more but whatever.

The good think about spring is that it handles all of that for you ao you dont have to do it manually.  Makes it so much easier to do di especially if you are making a web app.  


One example could be a service that buys products on a site.  You dont want more than one of those im guessi g.  Or a service that retrieves items from a database.  

Another example is for testing.  If you want to test a classes function but that class uses a service that talks to a db. You can easily switch out the real service with a fake service that doesnt actually talk to the db.

 
Nathan Milota
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so I guess what I don't really understand is that if someone asks me the process of starting to make a web application, I have absolutely no idea what that has to do with Spring.  All they showed us was how to make a bean or what do with an XML file and stuff like that.  I don't know what any of this has to do with anything.
 
Saloon Keeper
Posts: 20514
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to learn about Inversion of Control (IoC).

IoC is a software concept where instead of having your classes having to know all about other classes and where to find them, some sort of control framework takes generic classes and wires them together like TinkerToys™.

Spring Framework is perhaps the most popular. It provides a generic bean factory facility that makes it useful for stand-alone desktop applications, web applications, and other, more exotic systems. It also has a number of extensions that simplify the creation of applications that use database persistence (Spring Data), send/receive email, design web applications using the Model/View/Controller paradigm and much more. And because the components are plug-replaceable, it's easier to unit-test complex applications by replacing the "live" modules with mock modules - an especially good thing if testing an app would otherwise flood the world with test emails.

Spring isn't the only IoC framework by any means. The JavaServer Faces component of the JEE standard is also an IoC system, And, in fact, JSF and Spring can interface very nicely to produce a web application that's extremely powerful with relatively little effort and a lot of ease of maintenance.
 
Nathan Milota
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is this any different than just making an interface, and then a class to use dependency injection of one of the classes that inherits it?
 
Tim Holloway
Saloon Keeper
Posts: 20514
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nathan Milota wrote:How is this any different than just making an interface, and then a class to use dependency injection of one of the classes that inherits it?



A. A class doesn't "inherit" an Interface. It implements it.

B. Spring is the framework that does the dependency injection. And frequently instantiates the beans that are to be injected. And, courtesy of its extension modules, provides an extensive collection of different utility beans to be instantiated and injected.

C. If a class "uses" a dependency injection, there has to be an injector to do the injection. As I mentioned, this can be something like JavaServer Faces, but it can also be Spring.
 
Bartender
Posts: 1008
18
IntelliJ IDE Java Linux Mac OS X Oracle Redhat Spring Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add; (IMHO) from a high level the overall objective of Spring is to improve system design for better quality and maintainability.  
The aim is to allow as much as possible, a flexible, customisable framework in which developers can focus on the business domain logic (in plain old java classes [pojo]) rather than having to deal with all the software infrastructure complexities.  The framework uses DI (or IoC) to reduce coupling (reduce the fixed ties between parts of the system), and also increase cohesion (classes / methods do one thing).  DI has been discussed above.  


 
Tim Holloway
Saloon Keeper
Posts: 20514
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Peter Rooke wrote:Just to add; (IMHO) from a high level the overall objective of Spring is to improve system design for better quality and maintainability.  
The aim is to allow as much as possible, a flexible, customisable framework in which developers can focus on the business domain logic (in plain old java classes [pojo]) rather than having to deal with all the software infrastructure complexities.  The framework uses DI (or IoC) to reduce coupling (reduce the fixed ties between parts of the system), and also increase cohesion (classes / methods do one thing).  DI has been discussed above.  



Also, core Spring is fairly compact and general-purpose. JavaServer Faces is powerful, and it's DI-based, but it's exclusively devoted to GUI applications using the Model/View/Controller paradigm. Notice I didn't say "webapps" or "html". That's because JSF was actually intended to be extensible to other MVC systems, although such has never been done on a large scale.

Spring, on the other hand, supports MVC as only one of many component options. A Spring app doesn't have to be a GUI app, a webapp, or indeed any specific kind of application. And it can be used, as I mentioned earlier, alongside other DI frameworks. JSF manages only the app's GUI. It has no persistence capabilities. But a JSF webapp can employ Spring to provide persistence via Spring Data while including absolutely no code from Spring Web.
 
Nathan Milota
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just yet to see a clear example of what something would look like without a bean compared to what it looks like with a bean.  The problem is, many developers are simply horrible teachers when it comes to explaining it to a beginner.
 
Nathan Milota
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, I wasn't calling anyone here a horrible teacher, just stating a generalization I've found among tutorials I've gone through.

So, I found a website here that explains it a bit.

https://www.baeldung.com/spring-bean

If I'm correct, what I see them doing is simply cutting out one step of setting the company address to the object of address.  Is that right?  

It doesn't seem to me that this is saving any time, as they have to declare the object company as well, and instead of just putting address in the constructor, they are using an ApplicationContext and then injecting it into each new company that is declared, which doesn't seem to be saving any time at all, but taking more time.  Is this supposed to be easier?
 
Tim Holloway
Saloon Keeper
Posts: 20514
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaBeans are a fundamental concept in Java and really should have been covered in any basic book on Java programming. They extend past and pre-date the IoC and DI paradigms in Java, in fact and are not limited to IoC/DI.

The idea was that a JavaBean would be a standardized form of OOP object for Java that generic frameworks could manipulate. Some of the early shaping factors were done with sidelong glances at what Microsoft was trying to do towards making objects that would be equally applicable for either desktop or web use, although that never really panned out.

A JavaBean is a Java class formed according to certain hard and soft standards. Among the hard standards are that the class name Must start with a capital letter and that properties and methods Must start with a lower-case letter (failure to adhere may cause some bean-manipulating tools to malfunction). Ideally, properties should be private and only accessed via get/set member methods. Also ideally, a bean should contain data (properties) or logic (methods), but not both, but this ideal is rarely realized. Especially in JSF, where for convenience, the MVC Model and the action methods for the Model are often co-resident in the same backing bean.

Aside from IoC, JavaBeans are also part of the MBean concept where a JavaBean is shadowed by another JavaBean (the MBean) that allows monitoring and control of the original bean. The Tomcat web application server is primarily comprised of JavaBeans, and, in fact, can be embedded in other applications via a master Tomcat javabean. Tomcat can be heavily customized because much of its architecture is defined by a template file - its server.xml file - which is read by the Apache Digester library to construct a Directed Acyclic Graph of configured Tomcat components. Early versions of JBoss were extensively managed by MBeans. Virtually every significant product written in Java has JavaBeans in it.

Then there's the beanutils library that is employed by such subsystems as the JEE Expression Language (EL) processors to provide intelligent features to JSPs and JSF Views, among other things.

In short, you can get by without knowing Spring or even DI/IoC, if poorly. But if you don't understand what JavaBeans are all about, you don't know beans about Java.
 
Nathan Milota
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took Introduction to Programming and Advanced Software Development, and neither course ever mentioned as much as the word beans.
 
Tim Holloway
Saloon Keeper
Posts: 20514
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, speaking of JavaBeans, there's POJOs - Plain Old Java Objects.

Many of the early Java frameworks such as Apache Avalon (one of the first IoC frameworks), Enterprise Java Beans Versions 1 and 2, Apache Struts, and many more worked with things that were JavaBeans in general form, but had restrictions against general use. For example, Struts user-designed components were required to implement certain Interfaces and/or subclass certain base classes, as were EJBs. This led to all sorts of awkwardness because the beans could not be used outside their native environment for re-usable components, unit testing, and other cases where it was preferable to deal with a bean in isolation.

Having learned their lesson, later framework designers have tried to make their systems POJO-based. JSF and Spring, for example.
 
Nathan Milota
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I get it.  I'm an idiot.  I don't know why I can't wrap my head around this.  
 
Tim Holloway
Saloon Keeper
Posts: 20514
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, to be fair, most of the popular languages these days are code-and-go "git 'er dun!" with relatively little formal structuring, so you're not likely to come across the bean concept in courses on general programming and design. Beans make more sense when coding speed is less important than the ability to re-use components and to interface with complex systems.

To really ken a language these days, there are 3 things that must be mastered:

1. The language syntax and grammar
2. The support library
3. The language philosophy

While support libraries for modern languages are often massive, the philosophy is the hardest, because it's fuzzy, subject to evolution, and yet it shapes how the language and its environment will be formed and evolve. The JavaBean concept comes from philosophy.

And don't feel bad. I've been working with Python for years now and still haven't managed to make full sense of its packaging structure. Python's packaging architecture is more fuzzy and more dynamic than Java's is.
 
Nathan Milota
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to just start trying to learn SpringBoot or SpringMVC before learning Spring at all?  Maybe it would be easier to see what these are actually used for instead of just going through a bunch of tutorials that seem meaningless at the time.
 
Tim Holloway
Saloon Keeper
Posts: 20514
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't. Spring Boot, Spring MVC, Spring Data, Spring whatever are all module extensions to basic Spring. So you need to understand Spring itself to understand what Spring will be doing with them.

Others may have their own suggestions, but feel like the Manning Publications "Spring in Action" book provides a pretty clear introduction.
 
Nathan Milota
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I'm just going through my normal panic cycle where something doesn't make sense to me, so I freak out and start to hate myself and think I'm stupid, and do a bunch of practice exercise and learn the fundamentals, but still have no idea what the meaning of learning all that was to begin with, and cannot explain how or why I'd ever use it, and then a long, long way from then, I begin to get it after several weeks of aggravation, loading up on liquor, and punching holes in my walls and fixing them with drywall and spackle.
 
Tim Holloway
Saloon Keeper
Posts: 20514
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes. the usual.

But my TinkerToy™ analogy is actually pretty accurate, I think. When you learn to think about what beans do and how wiring them together creates something greater than their components, it all seems rather brilliant, I think!
 
Peter Rooke
Bartender
Posts: 1008
18
IntelliJ IDE Java Linux Mac OS X Oracle Redhat Spring Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't worry - the thing is we are all constantly learning, that seems to be the deal as technologies are always evolving.

As for Spring - don't worry about XML configuration as that way of configuration is now old and the preferred method of configuration is either Java Configuration or / and component scanning.  

For learning I'll start with the core spring documentation, which was always quite good Spring Core

I do think that using spring adds some extra complexity to any project, but the real win is in later stages when code is more maintainable.  Some of us remember before spring and when we had J2EE enterprise java beans [2] which were just horrific ;-)  
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!