From 668a0ed7de3360b4a3c3c5a8dbc873b5cb1e5846 Mon Sep 17 00:00:00 2001 From: taesan Date: Mon, 30 Dec 2019 15:45:46 +0900 Subject: [PATCH] =?UTF-8?q?XML=EB=B0=A9=EC=8B=9D=EC=9D=84=20Java=20Configu?= =?UTF-8?q?ration=EB=B0=A9=EC=8B=9D=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= =?UTF-8?q?=ED=95=B4=EB=B3=B4=EB=8A=94=20=EC=8B=A4=EC=8A=B5,=20Annotation?= =?UTF-8?q?=EC=9D=84=20=ED=99=9C=EC=9A=A9=ED=95=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring/di/ExamAppConfig.java | 20 ++++++++++++ spring/di/Program.java | 26 +++------------- spring/di/constSetting.xml | 47 +++++++++++++++++++++++++++++ spring/di/entity/NewlecExam.java | 19 ++++++++++-- spring/di/entity/TaesanExam.java | 43 ++++++++++++++++++++++++++ spring/di/setting.xml | 13 +++----- spring/di/ui/InlineExamConsole.java | 19 ++++++++++-- 7 files changed, 152 insertions(+), 35 deletions(-) create mode 100644 spring/di/ExamAppConfig.java create mode 100644 spring/di/constSetting.xml create mode 100644 spring/di/entity/TaesanExam.java diff --git a/spring/di/ExamAppConfig.java b/spring/di/ExamAppConfig.java new file mode 100644 index 0000000..6a49ed2 --- /dev/null +++ b/spring/di/ExamAppConfig.java @@ -0,0 +1,20 @@ +package spring.di; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +import spring.di.entity.Exam; +import spring.di.entity.NewlecExam; + +@ComponentScan( { "spring.di.ui", "spring.di.entity" }) +@Configuration +public class ExamAppConfig { + + // 메소드 명이 bean id와같이 식별자 역할을 해준다. + @Bean + public Exam exam() { + return new NewlecExam(); + } + +} diff --git a/spring/di/Program.java b/spring/di/Program.java index 7c2f0a9..1b26924 100644 --- a/spring/di/Program.java +++ b/spring/di/Program.java @@ -1,36 +1,20 @@ package spring.di; import org.springframework.context.ApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import spring.di.entity.Exam; -import spring.di.entity.NewlecExam; import spring.di.ui.ExamConsole; public class Program { public static void main(String[] args) { - //ExamConsole console = new InlineExamConsole(exam); // DI - //ExamConsole console = new GridExamConsole(exam); - - /* - new InlineExamConsole(exam) - new FridExamConsole(exam) - - 같은 메서드를 호출하게되는 위 코드의 수정을 대신해줄 수 있도록 하는 기능이 Spring의 DI다. - - */ - - // 해당 클래스 사용하기위해서 - // Maven 프로젝트로 바꿔서 빌드툴을 사용하도록 하였고 - // Maven Repository에가서 Spring context를 dependency해주었음. ApplicationContext context = - new ClassPathXmlApplicationContext("spring/di/setting.xml"); - + //new ClassPathXmlApplicationContext("spring/di/setting.xml"); + new AnnotationConfigApplicationContext(ExamAppConfig.class); - //ExamConsole console = (ExamConsole)context.getBean("console"); - ExamConsole console = context.getBean(ExamConsole.class); + //ExamConsole console = (ExamConsole)context.getBean("ExamConsole.class"); + ExamConsole console = (ExamConsole)context.getBean("console"); console.print(); diff --git a/spring/di/constSetting.xml b/spring/di/constSetting.xml new file mode 100644 index 0000000..1f64f39 --- /dev/null +++ b/spring/di/constSetting.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring/di/entity/NewlecExam.java b/spring/di/entity/NewlecExam.java index a1a6c70..fa5783a 100644 --- a/spring/di/entity/NewlecExam.java +++ b/spring/di/entity/NewlecExam.java @@ -7,16 +7,31 @@ public class NewlecExam implements Exam { private int math; private int com; + public NewlecExam() { + + } + + public NewlecExam(int kor, int eng, int math, int com) { + this.kor=kor; + this.eng=eng; + this.math=math; + this.com=com; + } + @Override public int total() { - // TODO Auto-generated method stub return kor+eng+math+com; } @Override public float avg() { - // TODO Auto-generated method stub return total() / 4.0f; } + @Override + public String toString() { + return "NewlecExam [kor=" + kor + ", eng=" + eng + ", math=" + math + ", com=" + com + "]"; + } + + } diff --git a/spring/di/entity/TaesanExam.java b/spring/di/entity/TaesanExam.java new file mode 100644 index 0000000..eb3c0ad --- /dev/null +++ b/spring/di/entity/TaesanExam.java @@ -0,0 +1,43 @@ +package spring.di.entity; + +import org.springframework.beans.factory.annotation.Value; + +public class TaesanExam implements Exam { + + @Value("10") + private int kor; + @Value("20") + private int eng; + @Value("30") + private int math; + @Value("40") + private int com; + + public TaesanExam() { + + } + + public TaesanExam(int kor, int eng, int math, int com) { + this.kor=kor; + this.eng=eng; + this.math=math; + this.com=com; + } + + @Override + public int total() { + return kor+eng+math+com; + } + + @Override + public float avg() { + return total() / 4.0f; + } + + @Override + public String toString() { + return "NewlecExam [kor=" + kor + ", eng=" + eng + ", math=" + math + ", com=" + com + "]"; + } + + +} diff --git a/spring/di/setting.xml b/spring/di/setting.xml index bd8bf57..1ccfaed 100644 --- a/spring/di/setting.xml +++ b/spring/di/setting.xml @@ -1,15 +1,10 @@ + xmlns:context="http://www.springframework.org/schema/context" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> - - - - - - - - + diff --git a/spring/di/ui/InlineExamConsole.java b/spring/di/ui/InlineExamConsole.java index 70ad151..84e5c4a 100644 --- a/spring/di/ui/InlineExamConsole.java +++ b/spring/di/ui/InlineExamConsole.java @@ -1,21 +1,34 @@ package spring.di.ui; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Component; + import spring.di.entity.Exam; +@Component("console") public class InlineExamConsole implements ExamConsole { + //@Qualifier("exam1") + @Autowired(required=false) private Exam exam; - + public InlineExamConsole() { + System.out.println("기본생성자"); } - + public InlineExamConsole(Exam exam) { this.exam = exam; } @Override public void print() { - System.out.printf("total is %d, avg is %f\n", exam.total(), exam.avg()); + if(exam != null ) { + System.out.println(" 현재 생성된 Exam 의 클래스는 ?? " + exam.getClass()); + System.out.printf("total is %d, avg is %f\n", exam.total(), exam.avg()); + } + else + System.out.println(" null 임다 "); } @Override