note

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

JSP/Spring

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

투한 2012. 2. 29. 09:44








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="writeArticleService" class="madvirus.spring.chap02.WriteArticleServiceImpl" autowire="byName" />
	
	<bean name="articleDao" class="madvirus.spring.chap02.MySQLArticleDao" />
</beans>



Main05
package madvirus.spring.chap02;

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

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

		ApplicationContext context = new ClassPathXmlApplicationContext(
				configLocations);

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



MySQLArticleDao
package madvirus.spring.chap02;

public class MySQLArticleDao implements ArticleDao{

	@Override
	public void insert(Article article) {
		System.out.println("MySQLArticleDao.insert()실행");
	}
}


WriteArticleService
package madvirus.spring.chap02;

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



Article
package madvirus.spring.chap02;

public class Article {

}


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

Spring 국제화  (0) 2012.02.29
Spring 프로퍼티 타입을 이용한 의존 관계 자동 설정  (0) 2012.02.29
Spring Di 심화 학습  (0) 2012.02.29
Spring Di(Dipendency Injection)  (0) 2012.02.28
Spring 설치 & 셋팅(응용 프로그램)  (0) 2012.02.28