note

Spring 프로퍼티 타입을 이용한 의존 관계 자동 설정 본문

JSP/Spring

Spring 프로퍼티 타입을 이용한 의존 관계 자동 설정

투한 2012. 2. 29. 09:52








applicationContext02.xml
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	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-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!-- 프로퍼티 타입을 이용한 의존 관계 자동 설정 -->
	<bean name="writeArticleService2" class="madvirus.spring.chap02.WriteArticleServiceImpl" autowire="byType" />
	
	<bean name="mysqlArticleDao" class="madvirus.spring.chap02.MySQLArticleDao" />
</beans>



Main06
package madvirus.spring.chap02;

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

public class Main06 {
	public static void main(String[] args){
		String[] configLocations = new String[] { "applicationContext02.xml" };
		ApplicationContext context = new ClassPathXmlApplicationContext(
				configLocations);

		// 프로퍼티 이름을 이용한 의존 관계 자동 설정
		WriteArticleService articleService = (WriteArticleService) context
				.getBean("writeArticleService2");

		articleService.write(new Article());
	}
}



WriteArticleService
package madvirus.spring.chap02;

public interface WriteArticleService {
	void write(Article article);
}



Article
package madvirus.spring.chap02;

public class Article {

}



'JSP > Spring' 카테고리의 다른 글

Spring @Required 어노테이션(Annotation)  (0) 2012.02.29
Spring 국제화  (0) 2012.02.29
Spring 프로퍼티 이름을 이용한 의존 관계 자동 설정  (0) 2012.02.29
Spring Di 심화 학습  (0) 2012.02.29
Spring Di(Dipendency Injection)  (0) 2012.02.28