Ioc Container: Dependency Injection, Dependency Lookup? - spring

I am trying to understand Spring Framework more what I know currently, and I am referring to "Pro Spring 3" book.
I came across the following section in the book as below:
It says that in general IoC can be decomposed into two components viz:
Dependency Injection and Dependency Lookup.
With respect to this, I have following questions:
1) Do Spring provide both Dependency Injection ,Dependency Lookup ?
2) Do all Ioc container have both these systems viz: Dependency Injection ,Dependency Lookup?
3) If Spring provides both Dependency Injection ,Dependency Lookup, then isn't it wrong to say that Spring is DI framework, when it has both these capabilities?

1: Yes, Spring provides both dependency injection and dependency lookup. You can let Spring inject dependencies using for example the #Autowired annotation, and you can also manually lookup components from Spring's ApplicationContext by calling one of the getBean methods.
The main thing to understand about the concept "inversion of control" (IoC) is that Spring does the work for you, instead of the other way around: you let Spring create instances of your components, and you let Spring inject the dependencies, instead of the other way around, where you write code yourself to create instances and lookup dependencies.
2: No, not necessarily.
3: Spring can do dependency injection (DI), so it is a DI framework. Just because it also does other things (such allow you to lookup components explicitly) doesn't suddenly not make it a DI framework anymore.

Related

Spring Boot Dependency only for Dependency Injection Features?

I just started a Compose for Desktop project with Kotlin. So there is no web stuff I am used to. This is why I did not use Spring stuff from the beginning.
However after a while I kind of miss dependency injection ;)
I tried to look for a spring boot / spring framework dependency that only has functionality like #Service, #Component, #Autowired etc. I was not able to find anything. This always was somewhat included into other contexts like Spring-Boot-Web etc.
So my question: Is there a Dependency Injection Only library with spring?

What is autowiring and AOP in Spring Framework?

I want to understand what is the exact meaning of term "autowiring" and AOP in Spring Framework.
From the official Spring Framework Reference:
The Spring container can autowire relationships between collaborating
beans. You can allow Spring to resolve collaborators (other beans)
automatically for your bean by inspecting the contents of the
ApplicationContext.
Which basically means you can leave it up to the Spring Framework to initialize your beans and ensure they're there when you need them. For example, lets say I have a number of Things in my app. Now, if I want to get all the Things and have them placed in a List<Thing>, in Spring (assuming I've configured it properly), I can simply use this code:
#Autowired
private List<Thing> things;
With no additional effort on my part I have the list I needed.
The question concerning Aspect Oriented Programming (AOP) as it relates to Spring would be quite a bit to put in a post. The simple explanation can be seen in the official Spring Framework Reference:
Aspect-Oriented Programming (AOP) complements Object-Oriented
Programming (OOP) by providing another way of thinking about program
structure. The key unit of modularity in OOP is the class, whereas in
AOP the unit of modularity is the aspect. Aspects enable the
modularization of concerns such as transaction management that cut
across multiple types and objects. (Such concerns are often termed
crosscutting concerns in AOP literature.)
One of the key components of Spring is the AOP framework. While the
Spring IoC container does not depend on AOP, meaning you do not need
to use AOP if you don’t want to, AOP complements Spring IoC to provide
a very capable middleware solution.
That section is very detailed and worth a read if you're interested in AOP.
Autowiring is a mechanism of resolving dependencies in Spring IoC container, as per Spring Reference. So instead of directly specifying the dependency (in XML or in Java configuration), you can depend on container itself to provide you with candidate(s). Spring itself should abort if you find more than one matching dependency (unless you are looking for a collection of beans).

Integrate Spring 4 with EJB in legacy system

TL;DR: How do I autowire an EJB into a Spring #Controller??
I'm trying to add a new Spring 4 webapp into a legacy system that currently uses EJBs, and runs in Resin 4. All EJBs are Stateless, and Local, and all contain:
#Resource
SessionContext ejbContext
EJBs are looked up via a singleton that contains the line:
return new InitialContext().lookup("production/entapp/default/myapplication/" + className + "/local");
I've got no experience of using EJBs other than via this method, so I'm not too familiar with how the lookup is performed, only that all the EJBs are found in this manner.
My question:
How do I autowire an EJB into a Spring #Controller??
I suspect I need to create an Interceptor of some kind, that will perform this lookup when it finds the need to autowire an EJB, but having looked through pages and pages of Google and SO, I'm none the wiser.
It's also worth mentioning that I'm trying to do all of this without any XML, using a Java #Configuration class.

Spring can be used in Seam?

I understand that Spring has really nice features, such as dependency injection. I am new to Spring. I have understood that I can use Spring alongside with struts and other frameworks too, in order to use its capabilities.
In my project I am going to use Seam 2.0, I am using JNDI to lookup for the EJBs. I am wondering if I can integrate Spring with Seam and use its ApplicationContext in order to get beans from that directly and not use JNDI lookup anymore?
There is a whole chapter in the Seam reference dedicated to this:
27. Spring Framework integration

Does Grails use Spring Dependency Injection by default

An additional question: What is service layer in Grails app?
Thanks
Yes, Grails uses Spring Dependency Injection by default. Grails is in fact Spring at the core of it all.
Your second question, "what is a service layer" is best described as singleton instances of classes that serve as a layer of abstraction and encapsulation of business logic.

Resources