Skip to content

Commit fe2d590

Browse files
learn/spring: autowire in spring using xml
1 parent 035e11c commit fe2d590

6 files changed

Lines changed: 43 additions & 28 deletions

File tree

telusko/Spring/third-spring-frmaework/src/main/java/org/akash/learn/spring/App.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public static void main(String[] args) {
1212
System.out.println("In main method");
1313
System.out.println(Arrays.toString(names));
1414

15-
Dev obj = (Dev) context.getBean("dev");
16-
System.out.println(obj.getAge());
15+
Dev obj = context.getBean(Dev.class);
16+
// System.out.println(obj.getAge());
1717
obj.learn();
1818
}
1919
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.akash.learn.spring;
2+
3+
public interface Computer {
4+
void compile();
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.akash.learn.spring;
2+
3+
public class Desktop implements Computer {
4+
@Override
5+
public void compile(){
6+
System.out.println("Compiling in Desktop");
7+
}
8+
}
Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,38 @@
11
package org.akash.learn.spring;
22

3-
import org.springframework.stereotype.Component;
4-
53
public class Dev
64
{
7-
private Laptop laptop;
8-
private int age;
5+
private Computer computer;
6+
// private int age;
97

108
// public Dev(int age) {
119
// this.age = age;
1210
// }
1311

1412

15-
public Dev(int age, Laptop laptop) {
16-
this.age = age;
17-
this.laptop = laptop;
18-
}
19-
20-
public Laptop getLaptop() {
21-
return laptop;
22-
}
13+
// public Dev(int age, Laptop laptop) {
14+
// this.age = age;
15+
// this.laptop = laptop;
16+
// }
2317

24-
public void setLaptop(Laptop laptop) {
25-
this.laptop = laptop;
18+
public Computer getComputer() {
19+
return computer;
2620
}
2721

28-
public int getAge() {
29-
return age;
22+
public void setComputer(Computer computer) {
23+
this.computer = computer;
3024
}
3125

32-
public void setAge(int age) {
33-
this.age = age;
34-
}
26+
// public int getAge() {
27+
// return age;
28+
// }
29+
//
30+
// public void setAge(int age) {
31+
// this.age = age;
32+
// }
3533

3634
public void learn(){
3735
System.out.println("Learning Spring Boot");
38-
laptop.compile();
36+
computer.compile();
3937
}
4038
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package org.akash.learn.spring;
22

3-
public class Laptop {
3+
public class Laptop implements Computer {
4+
@Override
45
public void compile(){
5-
System.out.println("Compiling.........");
6+
System.out.println("Compiling in Laptop");
67
}
78
}

telusko/Spring/third-spring-frmaework/src/main/resources/spring.xml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="
55
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
6-
<bean id="dev" class="org.akash.learn.spring.Dev">
6+
<bean id="dev" class="org.akash.learn.spring.Dev" autowire="byType" >
77
<!-- setter injection-->
88
<!-- <property name="age" value="12"/>-->
99
<!-- constructor injection-->
1010
<!-- <constructor-arg value="14"/>-->
1111
<!-- <property name="laptop" ref="laptop"/>-->
12-
<constructor-arg index="0" value="12"/>
13-
<constructor-arg index="1" ref="laptop"/>
12+
<!-- <constructor-arg index="0" value="12"/>-->
13+
<!-- <constructor-arg index="1" ref="laptop"/>-->
14+
<!-- <property name="computer" ref="desktop" />-->
1415
</bean>
15-
<bean id="laptop" class="org.akash.learn.spring.Laptop">
16+
<bean id="laptop" class="org.akash.learn.spring.Laptop" primary="true">
17+
</bean>
18+
<bean id="desktop" class="org.akash.learn.spring.Desktop">
1619
</bean>
1720
</beans>

0 commit comments

Comments
 (0)