XML방식을 Java Configuration방식으로 변경해보는 실습, Annotation을 활용함
This commit is contained in:
20
spring/di/ExamAppConfig.java
Normal file
20
spring/di/ExamAppConfig.java
Normal file
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
|
||||
47
spring/di/constSetting.xml
Normal file
47
spring/di/constSetting.xml
Normal file
@@ -0,0 +1,47 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
|
||||
|
||||
<!-- Exam exam = new NewlecExam(); -->
|
||||
<bean id="exam" class="spring.di.entity.NewlecExam">
|
||||
<constructor-arg type="int" name="kor" value="10" />
|
||||
<constructor-arg type="int" name="eng" value="10" />
|
||||
<constructor-arg type="int" name="math" value="10" />
|
||||
<constructor-arg type="int" name="com" value="10" />
|
||||
</bean>
|
||||
|
||||
<bean id="exam2" class="spring.di.entity.NewlecExam">
|
||||
<constructor-arg type="int" name="kor" value="20" />
|
||||
<constructor-arg type="int" name="eng" value="20" />
|
||||
<constructor-arg type="int" name="math" value="20" />
|
||||
<constructor-arg type="int" name="com" value="20" />
|
||||
</bean>
|
||||
|
||||
<!-- ExamConsole console = new GridExamConsole(); -->
|
||||
<bean id="console" class="spring.di.ui.InlineExamConsole">
|
||||
<!-- console.setExam(exam); -->
|
||||
<property name="exam" ref="exam" />
|
||||
</bean>
|
||||
|
||||
<!-- 첫번째 방법, constructor-arg태그에 list태그 사용 -->
|
||||
<bean id="exams" class="java.util.ArrayList">
|
||||
<constructor-arg>
|
||||
<list>
|
||||
<ref bean="exam" />
|
||||
<ref bean="exam2" />
|
||||
</list>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
|
||||
<!-- 두번째 방법, util 네임스페이스를 사용해서 표현 -->
|
||||
<util:list id="exams2" list-class="java.util.ArrayList">
|
||||
<ref bean="exam" />
|
||||
<ref bean="exam2" />
|
||||
</util:list>
|
||||
|
||||
|
||||
</beans>
|
||||
@@ -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 + "]";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
43
spring/di/entity/TaesanExam.java
Normal file
43
spring/di/entity/TaesanExam.java
Normal file
@@ -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 + "]";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,15 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
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">
|
||||
|
||||
<!-- Exam exam = new NewlecExam(); -->
|
||||
<bean id="exam" class="spring.di.entity.NewlecExam" />
|
||||
|
||||
<!-- ExamConsole console = new GridExamConsole(); -->
|
||||
<bean id="console" class="spring.di.ui.InlineExamConsole">
|
||||
<!-- console.setExam(exam); -->
|
||||
<property name="exam" ref="exam" />
|
||||
</bean>
|
||||
<context:component-scan base-package="spring.di.ui, spring.di.entity" />
|
||||
|
||||
</beans>
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user