• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Dependency injection

 
Saloon Keeper
Posts: 24496
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because IoC is outside in and constructors, Service Locators are inside out.

The reason why IoC took over so much of the Java world is that if you have to invoke "new" you are hard-coding a class type in a class that might want to be able to work with different types of classes that share a common interface. Service Locators are even worse, since you have to know the location and the protocol of the Service Locator. Which can be an especial pain when doing Unit Tests where you'd prefer to be able to use a mock service.

As I said, IoC allows generic components (class instances) to be wired together by the framework. The framework is declarative, and declarative logic tends to be simpler and harder to screw up than hard-wired logic. And more flexible, because the actual classes to be used are defined as part of the declaration rather than as part of the Java code.

In practical usage, we do "hard code connections" using annotations, but those connections can be overridden without changing source code using framework declarations.

Edit: I like Paul's analogy. Do you want to look up sinks in a catalog, go out to a hardware store, buy a sink, take it home, install it yourself, including attaching it to the pipes, or do you want a plumber to walk in with the sink and put it in for you?

To stretch the analogy, suppose you'd designed a custom vanity and you could only install one kind of sink in it because the vanity had a certain shape? Now think of a generic vanity where you can pick what kind of sink would be installed?
 
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Background: I wrote web applications for years before you could use annotations to configure them. I'm envious of the people who don't have to maintain miles of boilerplate XML files to describe their web apps any more. And generally when I use code written by other people (which I did a lot back in those days and still do) I don't spend a lot of time inquiring about how that code works. Basically none, in fact, unless it doesn't work the way I was led to expect by the code's documentation.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I usually like doing my own code.  Of course if using Tomee I use @Inject.

But just two things to add.  A stackexchange reply said the following would work:




And two, I changed mine a little to:



To make sure class being injected is good I made:


Remember this is just for Tomcat.  And as far as factories, interfaces, etc, remember at runtime a CPU doesn't care where code comes from
a class is a class, I can't see these patterns in just a small project, Fedex, UPS, yes.  Though any app should work
with just MVC with no special "pattern" classes.  It's hard to believe some of the patterns modern developers have
come up with.  At run time it all runs as procedural code anyway.  No different than some of the old business Basic
programming apps.

In Fact if done correctly you could write a large app with just JSP.  I wouldn't but you could.  

Honest, no arguments, just I hate all those modern patterns that isn't really necessary in programming.
I just like the classes and servlets and the JSP for presentation.

I once used a CAD program (Fastcad) written in assembly language.

But I ran app, paginated at same time in Chrome, FF, and Edge
 
Paul Clapham
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, like I said I had to write web apps where I had to do all of that stuff (but not using reflection to load classes because like you said way earlier, they were already in the classpath).

Can I post this unsolicited code which simplifies one of those classes? (Requires Java 9, but even if you're still on Java 8 then a Set would still be preferable to a Map.)

 
Jim Whitaker
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the code.  And like I said I know I can use weld, Guice, or just Tomee then use @Inject.  But my whole point behind this is to do a simple DI myself.  If for no other reason, just for fun.

I did Java in the early 2000's at a trucking company, but nothing large scale, it was just an intranet.  Been away a while, saw the new Jakarta ee 9 and was interested to see how it differs.  I found it similar, except I had never used maven in the past.  I do appreciate this post and the replies.
 
Tim Holloway
Saloon Keeper
Posts: 24496
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On a slight tangent here, I want to gripe out IoC systems that won't Play Well Together.

CDI has been out about a decade. And for nearly that long, I've been struggling to get it integrated into my own apps.

The problem is, I use Spring for all sorts of backend services such as JPA, task scheduling, mail services, etc., etc., etc. and I have been accustomed to getting Spring beans injected into my JEE JSF backing beans.

Injection in JSF is primarily done via expressions written Unified Expression Language (UEL, or EL, for short), so it was possible to provide a bridge module that the UEL symbol resolved could employ to reference both JSF-managed and Spring-managed beans transparently.

CDI broke that. I've spent the last day trying to get an equivalent system running. I failed. JSF 2.3 is inherently hostile to Spring. It was double-instantiating beans, once in Spring and once in CDI, and the CDI version of the bean didn't have the upstream injections that Spring provides.

The ultimate solution was to fall back to JSF 2.2 and to revert to the pre-CDI semantics, but I don't like being behind the curve. Hopefully now that Jakarta owns JEE, they and the Spring folks can get their act together and make things happy again.
 
Jim Whitaker
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Tim Holloway I am guessing you have viewed articles by baeldung such as:

https://www.baeldung.com/java-ee-cdi

I tried JSF once, I stuck with JSP.  It for me did not play well
when using jquery, whereas JSP does.
 
Tim Holloway
Saloon Keeper
Posts: 24496
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I look everywhere.

JSF and jQuery do play nice, but it's important to note that JSF2 uses jQuery for its AJAX functionality internally and it's often a very old version of jQuery. So to obtain joy, you have to persuade JSF to pull a more up-to-date jQuery, which isn't that hard.

Then again, when using an advanced extension tagset like RichFaces, I haven't coded much client-side Java as it could quickly get very messy for data functions. So data was something I left for the server side and the UI functions were already being done via the extended tags.
 
Jim Whitaker
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you used fetch js? It's pretty good.
 
Tim Holloway
Saloon Keeper
Posts: 24496
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Whitaker wrote:Have you used fetch js? It's pretty good.



Nope. In JSF you don't code AJAX, you mark form controls with AJAX attributes and the under-cover jQuery handles it automatically. When I'm working in NodeJS, I'm at the mercy of those infernal callback stacks. So there's probably nowhere I'd be able to use it at the moment.
 
Jim Whitaker
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See https://gist.github.com/jimgwhit/208ae9427b2623e5366bd4ed3277d1e0

A fetch example, not in jsp, but laravel (php).  But in jsp it would be similar.

Hard to believe jsf doesn't give you more control. I have also done server fetched partials in jsp.  Like a lookup table.

When I said I tried JSF in past, that's why I decided against, I like control of what I am doing.  I have no problems with using jstl in jsp, in fact I like it.  

I admit I enjoy back-and-forth conversation with other programmers.
 
Tim Holloway
Saloon Keeper
Posts: 24496
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSF does give partial page control. I can tell it exactly what parts of a form to submit for an AJAX request and what parts of a View to update as a result. I just don't have to write code to make it do so.

JSPs are things that these days I only use for non-form purposes. Like emitting raw text, XML, JSON or YAML. JSF is what I use for forms, since it has automatic validation, retry, error reporting and model/view updating. I suppose I could do JavaScript on my JSPs, but the more common use case for me there is to output a fixed page in response to a request.
 
Jim Whitaker
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I might look over JSF again, if I do I will post a question in new topic.
 
Tim Holloway
Saloon Keeper
Posts: 24496
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Whitaker wrote:I might look over JSF again, if I do I will post a question in new topic.

We have a whole forum for it!

I hear the moderator's a really nice guy.
 
reply
    Bookmark Topic Watch Topic
  • New Topic