Skip to content

Commit d2ac201

Browse files
committed
injecting beans
1 parent 29882a1 commit d2ac201

9 files changed

Lines changed: 256 additions & 0 deletions

File tree

‎injecting-beans/pom.xml‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung.common</groupId>
5+
<artifactId>SpringExample</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>SpringExample</name>
9+
<url>http://maven.apache.org</url>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>junit</groupId>
14+
<artifactId>junit</artifactId>
15+
<version>4.12</version>
16+
<scope>test</scope>
17+
</dependency>
18+
19+
<!-- Spring framework -->
20+
<dependency>
21+
<groupId>org.springframework</groupId>
22+
<artifactId>spring-core</artifactId>
23+
<version>4.1.4.RELEASE</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.springframework</groupId>
27+
<artifactId>spring-context</artifactId>
28+
<version>4.1.4.RELEASE</version>
29+
</dependency>
30+
31+
</dependencies>
32+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.support.ClassPathXmlApplicationContext;
5+
6+
/**
7+
* Application presenting various possibilities of bean injections in Spring.
8+
*/
9+
public class App {
10+
public static void main(String[] args) {
11+
ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
12+
13+
Computer obj = (Computer) context.getBean("computer");
14+
obj.print();
15+
}
16+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung;
2+
3+
import com.baeldung.model.*;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
6+
/**
7+
* Model for Computer.
8+
*/
9+
public class Computer {
10+
11+
@Autowired
12+
private GraphicsCard graphicsCard;
13+
private Processor processor;
14+
private HardDisk hardDisk;
15+
private Screen screen;
16+
private OperatingSystem operatingSystem;
17+
18+
public Computer(Processor processor) {
19+
this.processor = processor;
20+
}
21+
22+
@Autowired
23+
public Computer(OperatingSystem operatingSystem, Processor processor) {
24+
this.operatingSystem = operatingSystem;
25+
this.processor = processor;
26+
}
27+
28+
public void setHardDisk(HardDisk hardDisk) {
29+
this.hardDisk = hardDisk;
30+
}
31+
32+
@Autowired
33+
public void setScreen(Screen screen) {
34+
this.screen = screen;
35+
}
36+
37+
public void print() {
38+
System.out.println("Injected processor by constructor injection: " + processor.getName());
39+
System.out.println("Injected hard disk by setter injection: " + hardDisk.getName());
40+
System.out.println("Injected graphics card with field annotation: " + graphicsCard.getName());
41+
System.out.println("Injected screen with setter annotation: " + screen.getSize());
42+
System.out.println("Injected operating system name with constructor annotation: " + operatingSystem.getName());
43+
}
44+
45+
@Override
46+
public String toString() {
47+
return "Computer{" +
48+
"graphicsCard=" + graphicsCard +
49+
", processor=" + processor +
50+
", hardDisk=" + hardDisk +
51+
", screen=" + screen +
52+
", operatingSystem=" + operatingSystem +
53+
'}';
54+
}
55+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.model;
2+
3+
/**
4+
* Model for graphics card.
5+
*/
6+
public class GraphicsCard {
7+
String name;
8+
9+
public String getName() {
10+
return name;
11+
}
12+
13+
public void setName(String name) {
14+
this.name = name;
15+
}
16+
17+
@Override
18+
public String toString() {
19+
return "GraphicsCard{" +
20+
"name='" + name + '\'' +
21+
'}';
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.model;
2+
3+
/**
4+
* Model for hard disk drive.
5+
*/
6+
public class HardDisk {
7+
private String name;
8+
9+
public String getName() {
10+
return name;
11+
}
12+
13+
public void setName(String name) {
14+
this.name = name;
15+
}
16+
17+
@Override
18+
public String toString() {
19+
return "HardDisk{" +
20+
"name='" + name + '\'' +
21+
'}';
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.model;
2+
3+
/**
4+
* Model for operating system.
5+
*/
6+
public class OperatingSystem {
7+
private String name;
8+
9+
public String getName() {
10+
return name;
11+
}
12+
13+
public void setName(String name) {
14+
this.name = name;
15+
}
16+
17+
@Override
18+
public String toString() {
19+
return "OperatingSystem{" +
20+
"name='" + name + '\'' +
21+
'}';
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.model;
2+
3+
/**
4+
* Model for Processor.
5+
*/
6+
public class Processor {
7+
private String name;
8+
9+
public String getName() {
10+
return name;
11+
}
12+
13+
public void setName(String name) {
14+
this.name = name;
15+
}
16+
17+
@Override
18+
public String toString() {
19+
return "Processor{" +
20+
"name='" + name + '\'' +
21+
'}';
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.model;
2+
3+
/**
4+
* Model for Screen.
5+
*/
6+
public class Screen {
7+
8+
private String size;
9+
10+
public String getSize() {
11+
return size;
12+
}
13+
14+
public void setSize(String size) {
15+
this.size = size;
16+
}
17+
18+
@Override
19+
public String toString() {
20+
return "Screen{" +
21+
"size='" + size + '\'' +
22+
'}';
23+
}
24+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<beans xmlns="http://www.springframework.org/schema/beans"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns:context="http://www.springframework.org/schema/context"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans
5+
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
6+
7+
<bean id="computer" class="com.baeldung.Computer">
8+
<constructor-arg>
9+
<ref bean="processor"/>
10+
</constructor-arg>
11+
<property name="hardDisk">
12+
<ref bean="hardDisk"/>
13+
</property>
14+
</bean>
15+
16+
<bean id="processor" class="com.baeldung.model.Processor">
17+
<property name="name" value="Intel Core i7-6650U"/>
18+
</bean>
19+
20+
<bean id="hardDisk" class="com.baeldung.model.HardDisk">
21+
<property name="name" value="SSD TRANSCEND MTS400"/>
22+
</bean>
23+
24+
<bean id="graphicsCard" class="com.baeldung.model.GraphicsCard">
25+
<property name="name" value="NVIDIA GeForce GTX980M"/>
26+
</bean>
27+
28+
<bean id="screen" class="com.baeldung.model.Screen">
29+
<property name="size" value="17 inches"/>
30+
</bean>
31+
32+
<bean id="operatingSystem" class="com.baeldung.model.OperatingSystem">
33+
<property name="name" value="Windows 10 Pro"/>
34+
</bean>
35+
36+
<context:annotation-config/>
37+
</beans>

0 commit comments

Comments
 (0)