실습 2일차_Setter Injection

의존성 주입의 방법으로는 크게 3가지가 있다.

  • 생성자 주입(Constructor Injection)
  • 필드 주입(Field Injection)
  • 세터 주입(Setter Injection)

 

 Spring Setter Injection 1

MainClass.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;

public class MainClass {
	public static void main(String[] args) {
		
		// IoC Container
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/user.xml");

		UserService service =
				ctx.getBean("service", UserService.class);
		System.out.println(service.getMesg());
	}
}

UserService.java

package com.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// POJO Class
public class UserService {

	String mesg; // null ==> 외부에서 문자열을 주입
	
	// setter 메서드 주입
	public void setMesg(String mesg) {
		System.out.println("setMesg 메서드");
		this.mesg = mesg;
	}
	
	public String getMesg() {
		return mesg;
	}
}

user.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="com.service.UserService"> <!-- id는 식별 가능한 값, class는 클래스 입력 -->
		<property name="mesg" value="HelloWorld" />     <!-- System.out.println("setMesg 메서드"); property에 의해 출력 -->
	</bean>
	<!-- 다음 코드와 동일
		UserService service = new UserService();
		service.setMesg("HelloWorld");
	 -->
</beans>

* 출력 화면 

 

Spring Setter Injection 2

MainClass.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;

public class MainClass {

	public static void main(String[] args) {
		
		// IoC Container
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/user.xml");

		UserService service =
				ctx.getBean("service", UserService.class);
		System.out.println(service.getMesg());
		System.out.println(service);
		
		UserService service2 =
				ctx.getBean("service2", UserService.class);
		System.out.println(service2.getMesg());
		System.out.println(service2);
		
		UserService service3 =
				ctx.getBean("service3", UserService.class);
		System.out.println(service3.getMesg());
		System.out.println(service3);
	}
}

UserService.java

package com.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// POJO Class
public class UserService {
	// property (인스턴스 변수)
	String mesg; // null ==> 외부에서 문자열을 주입
	int num; 	 // 0 ==> 외부에서 값을 주입
	
	// setter 메서드 주입
	public void setMesg(String mesg) {
		this.mesg = mesg;
	}
	
	public void setNum(int num) {
		this.num = num;
	}
	
	public void setMesgNum(String mesg, int num) {
		this.mesg = mesg;
		this.num = num;
	}
	
	// getter
	public String getMesg() {
		return mesg;
	}
	
	public int getNum() {
		return num;
	}

	// toString
	@Override
	public String toString() {
		return "UserService [mesg=" + mesg + ", num=" + num + "]";
	}
}

user.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="com.service.UserService"> <!-- id는 식별 가능한 값, class는 클래스 입력 -->
		<property name="mesg" value="HelloWorld" />     <!-- System.out.println("setMesg 메서드"); property에 의해 출력 -->
	</bean>
	<!-- 다음 코드와 동일
		UserService service = new UserService();
		service.setMesg("HelloWorld");
	 -->
	
	<bean id="service2" class="com.service.UserService">
		<property name="num" value="20" />
	</bean>

	<bean id="service3" class="com.service.UserService">
		<property name="mesg" value="Happy" />
		<property name="num" value="35" />
	</bean>
	<!-- setter based injection -->
</beans>

* 출력 화면

 

Spring Setter Injection 3_Service_dao 추가

MainClass.java

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;

public class MainClass {
	public static void main(String[] args) {
		
		// IoC Container
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/user.xml");

		UserService service = ctx.getBean("service", UserService.class);
		List<String> list = service.list();
		System.out.println(list);
	}
}

UserService.java

package com.service;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.dao.UserDAO;

// POJO Class
public class UserService {
	// property (인스턴스 변수)
	UserDAO dao;
	
	// setter 메서드 주입
	public void setDao(UserDAO dao) {
		this.dao = dao;
	}
	
	public List<String> list(){
		return dao.list();
	}
}

UserDAO.java

package com.dao;
import java.util.Arrays;
import java.util.List;

public class UserDAO {

	// DB 연동 가정
	public List<String> list(){
		List<String> list = Arrays.asList("고길동", "홍길동", "신길동");
		return list;
	}
}

user.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="userDAO" class="com.dao.UserDAO" /> 		<!-- bean 태그로 등록하면 기본 생성자 호출 가능 -->
	<!--  
		UserDAO userDAO = new UserDAO();
	 --> 
	<bean id="service" class="com.service.UserService"> <!-- id는 식별 가능한 값, class는 클래스 입력 -->
		<property name="dao" ref="userDAO"></property>
	</bean>
	<!-- 다음 코드와 동일
		UserService service = new UserService();
		service.setDAO(UserDAO);
	 -->
</beans>

* 출력 화면 :

 

 출처

 

+ 강의 교재

+ Recent posts