1 분 소요

생성자 DI

값 형식의 DI설정 말고 생성자 형식의 DI를 설정 할 수 있다

public NewlecExam(int kor, int eng, int math, int com) {
		this.kor = kor;
		this.eng = eng;
		this.math = math;
		this.com = com;
	}


	public NewlecExam() { 
		// TODO Auto-generated constructor stub
	}

기본 생성자를 설정해준다

 

  • Setting.xml
<bean id ="exam" class ="spring.di.entity.NewlecExam">
	
	<constructor-arg value="10"/>
	<constructor-arg value="20"/>
	<constructor-arg value="30"/>
	<constructor-arg value="40"/>
	
	</bean>
	<bean id="console" class="spring.di.ui.GridExamConsole" >
	 <property name="exam" ref="exam" /> 
	</bean>

그런데 무엇이 kor이고 무엇이 eng을 모른다

따라서 지정을 따로 해줘야한다

 

1.인덱스를 지정해주기

<bean id ="exam" class ="spring.di.entity.NewlecExam">
	
	<constructor-arg index="0" value="10"/>
	<constructor-arg index="1"value="20"/>
	<constructor-arg index="2"value="30"/>
	<constructor-arg index="3"value="40"/>
	
	</bean>
	<bean id="console" class="spring.di.ui.GridExamConsole" >
	 <property name="exam" ref="exam" /> 
		</bean>

값을 출력 시키기위해 Newlecture에 tostring을 넣어준다

	@Override
	public String toString() {
		return "NewlecExam [kor=" + kor + ", eng=" + eng + ", math=" + math + ", com=" + com + "]";
	}

  • program

ApplicationContext context = new ClassPathXmlApplicationContext("spring/di/setting.xml");

		Exam exam = context.getBean(Exam.class);
		//Exam에 데이터가 있으면 반환해 주세요 
		//이것을 출력해주세요
		System.out.println(exam.toString());

		ExamConsole console = (ExamConsole) context.getBean("console");
		// ExamConsole console = context.getBean(ExamConsole.class);
		console.print();

 

2.이름으로 지정하기

    <constructor-arg name ="kor" value="10"/>
	<constructor-arg name ="eng" value="20"/>
	<constructor-arg name ="math" value="30"/>
	<constructor-arg name ="com" value="40"/>

 

3.생성자 호출의 모호한 매개변수를 호출 할때 - 타입을 지정해 줄 수있다

<constructor-arg name ="kor" type="float"  value="10"/>
	<constructor-arg name ="eng" type="float" value="20"/>
	<constructor-arg name ="math" type="float"  value="30"/>
	<constructor-arg name ="com" type="float"  value="40"/>

 

만약 태그가 번잡하다면?

처리기를 사용해서 사용한다

namespace를 사용한다


	xmlns:p="http://www.springframework.org/schema/p"

    <홍길동:bean> </홍길동:bean> 
   
    <김길동:bean> </김길동:bean>


네임스페이스의 태그의 부분은 특정한 처리기에 의해 실행될수있도록 그것을 측정한다 태그의 이름을 식별하기 위해서 사용한다

만약 홍길동 김길동이 있으면 성으로 구분하고 홍길동이 2명이 있다면 확장된 것을 통해 구분을 한다 그것을 namespace라고 한다

 

  • p태그 이용하기 ```xml

``` 국어 영어만 10씩 값이 들어간다

카테고리:

업데이트:

댓글남기기