injecting beans
This commit is contained in:
32
injecting-beans/pom.xml
Normal file
32
injecting-beans/pom.xml
Normal file
@@ -0,0 +1,32 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.common</groupId>
|
||||
<artifactId>SpringExample</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>SpringExample</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring framework -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>4.1.4.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>4.1.4.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
16
injecting-beans/src/main/java/com/baeldung/App.java
Normal file
16
injecting-beans/src/main/java/com/baeldung/App.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* Application presenting various possibilities of bean injections in Spring.
|
||||
*/
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
|
||||
|
||||
Computer obj = (Computer) context.getBean("computer");
|
||||
obj.print();
|
||||
}
|
||||
}
|
||||
55
injecting-beans/src/main/java/com/baeldung/Computer.java
Normal file
55
injecting-beans/src/main/java/com/baeldung/Computer.java
Normal file
@@ -0,0 +1,55 @@
|
||||
package com.baeldung;
|
||||
|
||||
import com.baeldung.model.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
/**
|
||||
* Model for Computer.
|
||||
*/
|
||||
public class Computer {
|
||||
|
||||
@Autowired
|
||||
private GraphicsCard graphicsCard;
|
||||
private Processor processor;
|
||||
private HardDisk hardDisk;
|
||||
private Screen screen;
|
||||
private OperatingSystem operatingSystem;
|
||||
|
||||
public Computer(Processor processor) {
|
||||
this.processor = processor;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public Computer(OperatingSystem operatingSystem, Processor processor) {
|
||||
this.operatingSystem = operatingSystem;
|
||||
this.processor = processor;
|
||||
}
|
||||
|
||||
public void setHardDisk(HardDisk hardDisk) {
|
||||
this.hardDisk = hardDisk;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setScreen(Screen screen) {
|
||||
this.screen = screen;
|
||||
}
|
||||
|
||||
public void print() {
|
||||
System.out.println("Injected processor by constructor injection: " + processor.getName());
|
||||
System.out.println("Injected hard disk by setter injection: " + hardDisk.getName());
|
||||
System.out.println("Injected graphics card with field annotation: " + graphicsCard.getName());
|
||||
System.out.println("Injected screen with setter annotation: " + screen.getSize());
|
||||
System.out.println("Injected operating system name with constructor annotation: " + operatingSystem.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Computer{" +
|
||||
"graphicsCard=" + graphicsCard +
|
||||
", processor=" + processor +
|
||||
", hardDisk=" + hardDisk +
|
||||
", screen=" + screen +
|
||||
", operatingSystem=" + operatingSystem +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
/**
|
||||
* Model for graphics card.
|
||||
*/
|
||||
public class GraphicsCard {
|
||||
String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "GraphicsCard{" +
|
||||
"name='" + name + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
/**
|
||||
* Model for hard disk drive.
|
||||
*/
|
||||
public class HardDisk {
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "HardDisk{" +
|
||||
"name='" + name + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
/**
|
||||
* Model for operating system.
|
||||
*/
|
||||
public class OperatingSystem {
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "OperatingSystem{" +
|
||||
"name='" + name + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
/**
|
||||
* Model for Processor.
|
||||
*/
|
||||
public class Processor {
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Processor{" +
|
||||
"name='" + name + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
24
injecting-beans/src/main/java/com/baeldung/model/Screen.java
Normal file
24
injecting-beans/src/main/java/com/baeldung/model/Screen.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
/**
|
||||
* Model for Screen.
|
||||
*/
|
||||
public class Screen {
|
||||
|
||||
private String size;
|
||||
|
||||
public String getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public void setSize(String size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Screen{" +
|
||||
"size='" + size + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
37
injecting-beans/src/main/resources/Spring-Module.xml
Normal file
37
injecting-beans/src/main/resources/Spring-Module.xml
Normal file
@@ -0,0 +1,37 @@
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
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">
|
||||
|
||||
<bean id="computer" class="com.baeldung.Computer">
|
||||
<constructor-arg>
|
||||
<ref bean="processor"/>
|
||||
</constructor-arg>
|
||||
<property name="hardDisk">
|
||||
<ref bean="hardDisk"/>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="processor" class="com.baeldung.model.Processor">
|
||||
<property name="name" value="Intel Core i7-6650U"/>
|
||||
</bean>
|
||||
|
||||
<bean id="hardDisk" class="com.baeldung.model.HardDisk">
|
||||
<property name="name" value="SSD TRANSCEND MTS400"/>
|
||||
</bean>
|
||||
|
||||
<bean id="graphicsCard" class="com.baeldung.model.GraphicsCard">
|
||||
<property name="name" value="NVIDIA GeForce GTX980M"/>
|
||||
</bean>
|
||||
|
||||
<bean id="screen" class="com.baeldung.model.Screen">
|
||||
<property name="size" value="17 inches"/>
|
||||
</bean>
|
||||
|
||||
<bean id="operatingSystem" class="com.baeldung.model.OperatingSystem">
|
||||
<property name="name" value="Windows 10 Pro"/>
|
||||
</bean>
|
||||
|
||||
<context:annotation-config/>
|
||||
</beans>
|
||||
Reference in New Issue
Block a user