티스토리 뷰

Spring DI 에 인자를 전달하는 것과 같이 어떤 객체를 주입할 수도 있다.

객체 주입에는 크게 2가지 방법이 있다.


1. 생성자 방식(constructor-arg 이용)

 

◎JdbcDao

package test.exam03;

public class JdbcDao {

	public JdbcDao() {
			
	}
	
	public int insert() {
		System.out.println("insert 작업");
		return 0;
	}
	
	public void delete() {
		System.out.println("delete 작업");
	}

}

 

◎JdbcService

package test.exam03;

public class JdbcService {

	private JdbcDao jdbcDao;
	
	public JdbcService() {
		
	}
	
	public JdbcService(JdbcDao jdbcDao) {
		this.jdbcDao = jdbcDao;
	}
	
	public void serviceTest() {
		jdbcDao.insert();
		jdbcDao.delete();
	}

}

 

◎exam03.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">
	
	<bean id = "service" class = "test.exam03.JdbcService">
          <constructor-arg>
            <ref bean = "jdbcDao"></ref>
          </constructor-arg>
	</bean>
	
	
</beans>

 

◎ExamMain

package test.exam03;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class ExamMain {

	public static void main(String[] args) {
		ApplicationContext context = new FileSystemXmlApplicationContext("/src/test/exam03/exam03.xml");
		JdbcService service = (JdbcService)context.getBean("service");
		
		service.serviceTest();
	
	}

}

2. 프로퍼티 방식(Property 이용)

 

◎IbatisDao

package test.exam04;

public class IbatisDao {

	public IbatisDao() {
		
	}

	public void insertDB() {
		System.out.println("insert");
	}
	
	public void updateDB() {
		System.out.println("update");
	}
	
}

 

◎IbatisService

package test.exam04;

public class IbatisService {

	private IbatisDao ibatisDao;
	
	public IbatisService() {
		// TODO Auto-generated constructor stub
	}

	public void setIbatisDao(IbatisDao ibatisDao) {
		this.ibatisDao = ibatisDao;
	}
	
	public void daoTest() {
		System.out.println("Start");
		ibatisDao.insertDB();
		ibatisDao.updateDB();
		System.out.println("End");
	}


}

 

◎exam04.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">
	
	<bean id = "ibatis" class = "test.exam04.IbatisDao"></bean>
	
	<bean id = "service" class = "test.exam04.IbatisService">
		<property name="ibatisDao" ref = "ibatis"></property>
	</bean>
	
</beans>

이때, 주입하는 객체는 bean으로 이미 등록되어 있어야 property 사용이 가능하며, property name 은 해당 객체의 맴버 변수의 이름과 동일하다.

 

◎main

package test.exam04;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;


public class ExamMain {
	public static void main(String[] args) {
		ApplicationContext context = new FileSystemXmlApplicationContext("/src/test/exam04/exam04.xml");
		IbatisService service = (IbatisService)context.getBean("service");
		
		service.daoTest();
	}


}

3. xml name space를 활용하는 방법

xmlns를 사용하면 <property> 태그를 사용하지 않고도 객체를 주입할 수 있다.

 

2번과 동일한 예제 코드를 활용하며, xml 파일만 수정하면 된다.

 

◎exam04.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">
	
	<bean id = "ibatis" class = "test.exam04.IbatisDao"></bean>
	
	<bean id = "service" class = "test.exam04.IbatisService"
		p:ibatisDao-ref="ibatis">
	</bean>
	
</beans>
반응형
Comments