0 ratings0% found this document useful (0 votes) 61 views5 pagesJava Dependency Injection
about advanced java for btech cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
What is Dependency Injection:
Dependency Injection is the main functionality provided ;
by Spring |OC(Inversion of Control), The Spring-Core module is responsible
for injecting dependencies through either Constructor or Setter methods. The
design principle of Inversion of Control emphasizes keeping the Java classes
independent of each other and the container frees them from object creation
and maintenance. These classes, managed by Spring, must adhere to the
standard definition of Java-Bean. Dependency Injection in Spring also
ensures loose-coupling between the classes.
Need for Dependency Injection:
Suppose class One needs the object of class Two to instantiate or operate a
method, then class One is said to be dependent on class Two. Now though
it might appear okay to depend a module on the other but, in the real world,
this could lead to a lot of problems, including system failure. Hence such
dependencies need to be avoided
Spring IOC resolves such dependencies with Dependency Injection, which
makes the code easier to test and reuse. Loose coupling between classes
can be possible by defining interfaces for common functionality and the
injector will instantiate the objects of required implementation. The task of
instantiating objects is done by the container according to the configurations
specified by the developer.
The Dependency Injection is a design pattern that removes the dependency of the
programs. In such case we provide the information from the external source such as
XML file, It makes our code loosely coupled and easier for testing. In such case we
write the code as:
1. class Employee
2, Address address;
B)
4, Employee(Address address){
5. [Link]=address;
6}
7. public void setAddress(Address address){
8. [Link]=address;
}
EBSInjection by Constructor
dency by constructor. The subelement
used for constructor injection. Here we are going to inject
1. primitive and String-based values
2. Dependent object (contained object)
3, Collection values etc.
Injecting primitive and string-based values
Let's see the simple example to inject primitive and string-based values. We have
created three files here:
‘0 Employeejava
‘© applicationContextxm!
co Testjava
Itisa simple class containing two fields id and name, There are four constructors and
‘one method in this class.
1. package comjavatpoint;
25
3. public class Employee (
4, private int id;
5. private String name;
6[Link]
We are providing the information into the bean by this file. The constructor-arg
element invokes the constructor. In such case, parameterized constructor of int type
will be invoked. The value attribute of constructor-arg element will assign the
specified value. The type attribute specifies that int parameter constructor will be
invoked.
1.
2.
|.
10.
11
12.
13.
[Link]This class gets the bean from the applicationContextxmi file and calls the show
method.
1. package comjavatpoint;
2
3. import org springframework beans. [Link]; E
4 import ‘rg [Link];
s import [Link].";
6
7. public class Test (
8. public static void main(String{] args) {
a
10. Resource r=new ClassPathResource("[Link]");
11. BeanFactory factory=new XmiBeanFactory();
12,
13, Employee s=(Employee}factory getBean(e");
14. sshowd;
15.
16. }
7.)
Output:10 null
Injecting string-based values
4f you don't specify the type attribute in the Constructor-arg element, by default
string type constructor will be invoked.
*
I.
Sewn,
"you change the bean element as Given above, string parameter Constructor will be
Invoked and the output will be 0 10,
Output:0 10
You may aso pass the string literal as following: