|
| 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 | +} |
0 commit comments