Spring DI 지시서 작성 - Spring Bean Configuraion

    스프링 라이브러리 설정하기

    maven pom.xml 만들기

    스프링 라이브러리 포함시키기

    maven index 설치하기 (시간이 오래 걸림)

    설치하면 Dependencies에서 add 선택 후 검색해서 maven 라이브러리 추가가 가능하다.

    mvnrepository.com/search?q=Springframework

     

    Maven Repository: Springframework

    Spring Web MVC Last Release on Feb 16, 2021

    mvnrepository.com

    pom.xml


    injection  setter방법과 생성자 방법에서 Spring DI를 이용한 객체 결합하기

    xml 방식

     

    Application Context 생성하기

    Program.java

    package spring.di;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import spring.di.entity.Exam;
    import spring.di.entity.NewIecExam;
    import spring.di.ui.ExamConsole;
    import spring.di.ui.GridExamConsole;
    import spring.di.ui.InlineExamConsole;
    
    public class Program {
    
    	public static void main(String[] args) {
    		
    		/* 스프링에게 지시하는 방법으로 코드를 변경
    		 * Exam exam = new NewIecExam(); 
    		 * ExamConsole console = new GridExamConsole();
    		 * 
    		 * console.setExam(exam);
    		 */
    		
    		//지시서 읽어들이기
    		ApplicationContext context = new ClassPathXmlApplicationContext("spring/di/setting.xml");
    		
    		//ExamConsole console = (ExamConsole) context.getBean("console"); //id로 꺼내기
    		ExamConsole console = context.getBean(ExamConsole.class); //(가르키는 인터페이스 형식에 찹조하는 객체를 찾아준다.) 클래스명으로 가져오기
    		console.print();
    	}
    
    }

    ref 형식 DI

    setting.xml

    <?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">
    	<!-- Exam exam = new NewIecExam(); -->
    	<bean id="exam" class="spring.di.entity.NewIecExam" />
    	<!-- ExamConsole console = new GridExamConsole(); -->
    	<bean id="console" class="spring.di.ui.GridExamConsole" >
    		<!-- console.setExam(exam); -->
    		<property name="exam" ref="exam" />
    	</bean>	
    </beans>

    값 형식 DI

    getter, setter 해준다.

    NewIecExam.java

    package spring.di.entity;
    
    public class NewIecExam implements Exam {
    	
    	private int kor;
    	private int eng;
    	private int math;
    	private int com;
    	
    	public int getKor() {
    		return kor;
    	}
    
    	public void setKor(int kor) {
    		this.kor = kor;
    	}
    
    	public int getEng() {
    		return eng;
    	}
    
    	public void setEng(int eng) {
    		this.eng = eng;
    	}
    
    	public int getMath() {
    		return math;
    	}
    
    	public void setMath(int math) {
    		this.math = math;
    	}
    
    	public int getCom() {
    		return com;
    	}
    
    	public void setCom(int com) {
    		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;
    	}
    
    }

    pom.xml

    <?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">
    	<!-- Exam exam = new NewIecExam(); -->
    	<bean id="exam" class="spring.di.entity.NewIecExam" >
    		<property name="kor" >
    			<value>10</value>
    		</property>
    		<property name="eng" value="10" />
    		<property name="math" value="10" />
    		<property name="com" value="10" />
    	</bean>
    	<!-- ExamConsole console = new GridExamConsole(); -->
    	<bean id="console" class="spring.di.ui.GridExamConsole" >
    		<!-- console.setExam(exam); -->
    		<property name="exam" ref="exam" />
    	</bean>	
    </beans>


    생성자 DI

    생성자를 통해서 값을 설정하는 방법

     

    Program.java

    package spring.di;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import spring.di.entity.Exam;
    import spring.di.entity.NewIecExam;
    import spring.di.ui.ExamConsole;
    import spring.di.ui.GridExamConsole;
    import spring.di.ui.InlineExamConsole;
    
    public class Program {
    
    	public static void main(String[] args) {
    		
    		/* 스프링에게 지시하는 방법으로 코드를 변경
    		 * Exam exam = new NewIecExam();
    		 * Exam exam = new NewIecExam(10,10,10,10); 
    		 * ExamConsole console = new GridExamConsole();
    		 * 
    		 * console.setExam(exam);
    		 */
    		
    		//지시서 읽어들이기
    		ApplicationContext context = new ClassPathXmlApplicationContext("spring/di/setting.xml");
    		
    		Exam exam = context.getBean(Exam.class);
    		System.out.println(exam.toString());
    		//ExamConsole console = (ExamConsole) context.getBean("console"); //id로 꺼내기
    		ExamConsole console = context.getBean(ExamConsole.class); //(가르키는 인터페이스 형식에 찹조하는 객체를 찾아준다.) 클래스명으로 가져오기
    		console.print();
    	}
    
    }

    NewIecExam.java

    package spring.di.entity;
    
    public class NewIecExam implements Exam {
    	
    	private int kor;
    	private int eng;
    	private int math;
    	private int com;
    	
    	//생성자
    	public NewIecExam() {
    		// TODO Auto-generated constructor stub
    	}
    	
    	//생성자 오버로드
    	public NewIecExam(int kor, int eng, int math, int com) {
    		super();
    		this.kor = kor;
    		this.eng = eng;
    		this.math = math;
    		this.com = com;
    	}
    
    	public int getKor() {
    		return kor;
    	}
    
    	public void setKor(int kor) {
    		this.kor = kor;
    	}
    
    	public int getEng() {
    		return eng;
    	}
    
    	public void setEng(int eng) {
    		this.eng = eng;
    	}
    
    	public int getMath() {
    		return math;
    	}
    
    	public void setMath(int math) {
    		this.math = math;
    	}
    
    	public int getCom() {
    		return com;
    	}
    
    	public void setCom(int com) {
    		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 "NewIecExam [kor=" + kor + ", eng=" + eng + ", math=" + math + ", com=" + com + "]";
    	}
    	
    
    }

     

    pom.xml

    <?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">
    	<!-- Exam exam = new NewIecExam(); -->
    	<bean id="exam" class="spring.di.entity.NewIecExam" >
    		<constructor-arg name="kor" value="10" />
    		<constructor-arg name="math" value="10" />
    		<constructor-arg name="com" value="10" />
    		<constructor-arg name="eng" value="10" />
    	</bean>
    	<!-- ExamConsole console = new GridExamConsole(); -->
    	<bean id="console" class="spring.di.ui.GridExamConsole" >
    		<!-- console.setExam(exam); -->
    		<property name="exam" ref="exam" />
    	</bean>	
    </beans>

    name이나 index 값이 없을 경우 순서대로 적용됨


    설정 파일 처리기 - Namespaces 사용하기

    추가적인 처리기의 도움을 받아서 사용함 - 처리기 불러오기

    Namespaces p 체크

    이름을 식별하기 위해서 확장된 이름을 사용한다.

    pom.xml

    <?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"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    	<!-- Exam exam = new NewIecExam(); -->
    	<bean id="exam" class="spring.di.entity.NewIecExam" p:kor="10"  p:eng="10" />
    	<!-- 	
    	<bean id="exam" class="spring.di.entity.NewIecExam" >
    		<constructor-arg name="kor" value="10" />
    		<constructor-arg name="math" value="10" />
    		<constructor-arg name="com" value="10" />
    		<constructor-arg name="eng" value="10" />
    	</bean> 
    	-->
    	<!-- ExamConsole console = new GridExamConsole(); -->
    	<bean id="console" class="spring.di.ui.GridExamConsole" >
    		<!-- console.setExam(exam); -->
    		<property name="exam" ref="exam" />
    	</bean>	
    </beans>

    • 네이버 블러그 공유하기
    • 네이버 밴드에 공유하기
    • 페이스북 공유하기
    • 카카오스토리 공유하기
    loading