Skip to content

Instantly share code, notes, and snippets.

@yoonseohyun0206
Created May 31, 2025 14:52
Show Gist options
  • Save yoonseohyun0206/95f50c009fe388df843f3f462308a15a to your computer and use it in GitHub Desktop.
Save yoonseohyun0206/95f50c009fe388df843f3f462308a15a to your computer and use it in GitHub Desktop.
package org.example;
import org.springframework.stereotype.Component;
@Component
public class ConsolePrinter implements Printer {
@Override
public void printMessage(String message) {
System.out.println("Console : " + message);
}
}
package org.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class FilePrinter {
private final Printer printer;
@Autowired
public FilePrinter(Printer printer) {
this.printer = printer;
}
public void play() {
printer.printMessage("Hello World");
System.out.println("파일이 출력됩니다.");
}
}
package org.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
// IoC 컨테이너 초기화
ApplicationContext context = new AnnotationConfigApplicationContext("org.example");
// Spring container가 관리하는 객체 가져오기
FilePrinter fileprinter = context.getBean(FilePrinter.class);
fileprinter.play();
}
}
package org.example;
public interface Printer {
void printMessage(String message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment