XML-based configuration replaced with Java Config.
This commit is contained in:
2
injecting-beans/README.txt
Normal file
2
injecting-beans/README.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
If you are interested in XML-based configuration, you can see all the injections done with this approach by
|
||||
just using Spring-Module.xml in App.java (It's commented by default).
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.baeldung;
|
||||
|
||||
import com.baeldung.config.AppConfig;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
@@ -8,8 +10,9 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
*/
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
|
||||
// ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
|
||||
|
||||
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
|
||||
Computer obj = (Computer) context.getBean("computer");
|
||||
obj.print();
|
||||
}
|
||||
|
||||
@@ -15,14 +15,11 @@ public class Computer {
|
||||
private Screen screen;
|
||||
private OperatingSystem operatingSystem;
|
||||
|
||||
public Computer(Processor processor) {
|
||||
this.processor = processor;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public Computer(OperatingSystem operatingSystem, Processor processor) {
|
||||
public Computer(Processor processor, OperatingSystem operatingSystem, HardDisk hardDisk) {
|
||||
this.operatingSystem = operatingSystem;
|
||||
this.processor = processor;
|
||||
this.hardDisk = hardDisk;
|
||||
}
|
||||
|
||||
public void setHardDisk(HardDisk hardDisk) {
|
||||
@@ -35,11 +32,11 @@ public class Computer {
|
||||
}
|
||||
|
||||
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());
|
||||
System.out.println("Injected processor: " + processor.getName());
|
||||
System.out.println("Injected hard disk: " + hardDisk.getName());
|
||||
System.out.println("Injected graphics card: " + graphicsCard.getName());
|
||||
System.out.println("Injected screen size: " + screen.getSize());
|
||||
System.out.println("Injected operating system name: " + operatingSystem.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.config;
|
||||
|
||||
import com.baeldung.Computer;
|
||||
import com.baeldung.model.*;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Beans configuration.
|
||||
*/
|
||||
@Configuration
|
||||
public class AppConfig {
|
||||
|
||||
@Bean(name = "computer")
|
||||
public Computer computer() {
|
||||
return new Computer(processor(), operatingSystem(), hardDisk());
|
||||
}
|
||||
|
||||
@Bean(name = "processor")
|
||||
public Processor processor() {
|
||||
Processor processor = new Processor();
|
||||
processor.setName("Intel Core i7-6650U");
|
||||
return processor;
|
||||
}
|
||||
|
||||
@Bean(name = "hardDisk")
|
||||
public HardDisk hardDisk() {
|
||||
HardDisk hardDisk = new HardDisk();
|
||||
hardDisk.setName("SSD TRANSCEND MTS400");
|
||||
return hardDisk;
|
||||
}
|
||||
|
||||
@Bean(name = "graphicsCard")
|
||||
public GraphicsCard graphicsCard() {
|
||||
GraphicsCard graphicsCard = new GraphicsCard();
|
||||
graphicsCard.setName("NVIDIA GeForce GTX980M");
|
||||
return graphicsCard;
|
||||
}
|
||||
|
||||
@Bean(name = "operatingSystem")
|
||||
public OperatingSystem operatingSystem() {
|
||||
OperatingSystem operatingSystem = new OperatingSystem();
|
||||
operatingSystem.setName("Windows 10 Pro");
|
||||
return operatingSystem;
|
||||
}
|
||||
|
||||
@Bean(name = "screen")
|
||||
public Screen screen() {
|
||||
Screen screen = new Screen();
|
||||
screen.setSize("17 inches");
|
||||
return screen;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,7 +4,10 @@ package com.baeldung.model;
|
||||
* Model for graphics card.
|
||||
*/
|
||||
public class GraphicsCard {
|
||||
String name;
|
||||
private String name;
|
||||
|
||||
public GraphicsCard() {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
|
||||
@@ -6,6 +6,9 @@ package com.baeldung.model;
|
||||
public class OperatingSystem {
|
||||
private String name;
|
||||
|
||||
public OperatingSystem(){
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,9 @@ package com.baeldung.model;
|
||||
public class Processor {
|
||||
private String name;
|
||||
|
||||
public Processor() {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@@ -4,9 +4,11 @@ package com.baeldung.model;
|
||||
* Model for Screen.
|
||||
*/
|
||||
public class Screen {
|
||||
|
||||
private String size;
|
||||
|
||||
public Screen() {
|
||||
}
|
||||
|
||||
public String getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user