This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Zero to AI - A non-technical, hype-free guide to prospering in the AI era and have Nicolò Valigi and Gianluca Mauro on-line!
See this thread for details.
Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning 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
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Spring IoC

 
Greenhorn
Posts: 17
Netbeans IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am taking my first steps into Spring with the intent of rewriting an existing application I have written and adding a REST API to it.

The REST API using Spring Web seems easy enough to figure out, but I am having a hard time figuring inversion of control.

To create objects, I made heavy use of the Builder pattern. From what I can see, object creation is done by Spring: is it as simple as replacing every instance of the  'new' keyword by Springs ApplicationContext to create my objects, which will be anmotated by metadata to help Spring figure out what to create?

One thing that is stumping me is List<> of objects. My app depends on them, just wondered how Spring IoC deals with this?

Any help appreciated!
 
Saloon Keeper
Posts: 22634
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like to refer to IoC as "good things come to those who wait",

Most of us are conditioned to "go out and do things. But that's not how IoC works. IoC works with a framework and IoC objects simply tell the framework (e. g., via an Annotation) that the framework should construct (vis the Spring BeanFactory) and wire together sets of objects. This is very good, because it reduces chunks of the application to plug-in components which can be switch out for similar components. As, for example, if you have an app that blasts out emails, you chould create a standard emailer interface and implement 2 classes: a dummy emailer for testing and a live emailer for production. Using Spring, you wire in the dummy one while you're debugging or maintaining and avoid spamming the world with a lot of test emails. Then have Spring wire in the production module when you're ready to go live.

In Spring, the BeanFactory is where you start, since hopefully you're not using static variables. You can ask for a bean by name and Spring will locate it in its inventory (or construct it if it doesn't exist) and automatically create and connect any auto-wired beans that that bean has referenced, so you get a whole lot of functionality with relatively little effort.

Spring is not the only IoC service out there. In fact, there are a number of extinct IoC services I used to work with (Apache Avalon for instance). Another one is the JEE standard JavaServer Faces framwork, whose own IoC services can be integrated with Spring via a simple configuration option to make the two factories work seamlessly in concert. The relatively recent Java standard injection facility also supports IoC, although I find Spring to be more civilized.
 
Shaun Connelly-Flymn
Greenhorn
Posts: 17
Netbeans IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply: I think I understand the benefit if you have a series of objects that do work.

Eg I have a class which reads a word document, parses it and returns objects for use in my program.

Each step in the class is represented by an interface so I change implementations easily if I need to. The object is created by a builder.

However I create temporary objects for intermediate work during the parsing process: would the output of the parsing process be considered a bean in Spring talk? It is an object with properties set by constructor accessible by getters only.

These objects will be persisted (I use Serialization for now: will use BaseX or other XML DB).

Maybe I am getting confused between objects which perform a function (parsing to objects) and items being retrieved from a database.

I do not doubt IoC is useful...it feels like I am trying to shoehorn it  into an app and am confused how to do it.
 
Tim Holloway
Saloon Keeper
Posts: 22634
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that your parser is a good fir for Spring, no. A parser would typically construct a directed acyclic graph (tree) and do so by sequencing itself through the source data. The DAG, then would vary in structure and content based on the source data.

Spring can construct a DAG, but the DAG would be a fixed geometry and content, based on the meta-data that describes the beans that would be instantiated and assembled. So the parser would probably either be an Operator new/newInstance function or at best you'd have a factory that produced DAG nodes. Which could be a Spring BeanFactory called on-demand, but wouldn't have to be. Incidentally, Spring most often works with Singleton beans, so a factory for multiple bean nodes would have to have them defined to Spring as non-sigleton beans.
 
Shaun Connelly-Flymn
Greenhorn
Posts: 17
Netbeans IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you- This answers the question about how Spring fits (or rather doesn't!) fit with what I was planning to do, and why I am struggling to understand how it works!

So... I would have to rewrite my program for the Spring Framework?

 
Shaun Connelly-Flymn
Greenhorn
Posts: 17
Netbeans IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So... maybe I am barking up the wrong tree.

What I want to do is place a REST API on the backend and have the browser as the front end (eg Angular or Vue)

Is there a different way to achieve this?
 
Tim Holloway
Saloon Keeper
Posts: 22634
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure which way you mean that. If you're looking to have a GUI front-end webapp that invokes web services via a separate ReST back-end webapp, then Spring can definitely help gluing the back-end components together. It's just that if the backend is going to get a data stream and parse it, Spring can't help much on that part, whereas it can be very useful indeed for a ReST backend that wants to work with persistent data (Spring Data) or other backend services.

Whether Spring could help much on a mostly JavaScript front-end app depends on the overall architecture of the front-end, although I wouldn't be optimistic. Spring doesn't work directly with JavaScript.
 
Shaun Connelly-Flymn
Greenhorn
Posts: 17
Netbeans IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly... thank you for your patience with me I do appreciate it.

So the parsing bit I could hive off as a separate project: the end result is objects for use in the program which can be serialized as JSON and stored in a database.

I want a backend which serves JSON following operations on the objects, and a front end which works in the browser which uses GET, POST, PUT... etc to work on the objects.
 
Tim Holloway
Saloon Keeper
Posts: 22634
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep. It would depend on your design, but Spring might get involved with the Web Services API and with the JSON and other persistent data, but the parser would likely at best use Spring to manufacture elements for the constructed parse tree, if at all.
 
Shaun Connelly-Flymn
Greenhorn
Posts: 17
Netbeans IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Tim : I think that I tried to embrace all of Spring following some bad advice from other forums.

There was a REST API tutorial where objects were returned by @ RestControllers that were automatically serialized by Jackon... I think that might need to be my starting point
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic