property를 이용한 값 주입(하위태그,속성,p:namespace이용)-spring_di_02[실습]
프로그래밍/Spring Framework 2012. 6. 20. 14:31
package di.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import di.movie.dto.DirectorDTO;
import di.movie.dto.MovieDTO;
public class TestMovie {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("di/movie/config/movie.xml");
System.out.println("------director--------");
DirectorDTO dto = (DirectorDTO)ctx.getBean("director1");
System.out.println(dto);
dto = (DirectorDTO)ctx.getBean("director2");
System.out.println(dto);
dto = (DirectorDTO)ctx.getBean("director3");
System.out.println(dto);
System.out.println("------movie--------");
MovieDTO mto = (MovieDTO)ctx.getBean("movie1");
System.out.println(mto);
mto = (MovieDTO)ctx.getBean("movie2");
System.out.println(mto);
mto = (MovieDTO)ctx.getBean("movie3");
System.out.println(mto);
}
}
<?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-3.0.xsd">
<!-- property 이용 -->
<!-- Director 객체 생성 후 값 주입 -->
<!-- property 태그 - 하위태그 이용해 값 설정 -->
<bean id="director1" class="di.movie.dto.DirectorDTO">
<property name="directorId">
<value>d-0001</value>
</property>
<property name="name">
<value>임권택</value>
</property>
<property name="age">
<value>28</value>
</property>
</bean>
<!-- property 태그 - 속성을 이용해 값 설정 -->
<bean id="director2" class="di.movie.dto.DirectorDTO">
<property name="directorId" value="d-0001"/>
<property name="name" value="임권택"/>
<property name="age" value="28"/>
</bean>
<!-- property 태그 - p : namespace 이용해 값 설정 -->
<bean id="director3"
class="di.movie.dto.DirectorDTO"
p:directorId="d-0001"
p:name="임권택"
p:age="28"/>
<!--MovieDTO 객체 생성 후 값 주입 -->
<!-- property 태그 - 하위태그 이용해 값 설정 -->
<bean id="movie1" class="di.movie.dto.MovieDTO">
<property name="movieId">
<value>0001</value>
</property>
<property name="title">
<value>간기남</value>
</property>
<property name="runningTime">
<value>60</value>
</property>
<property name="genre">
<value>멜로</value>
</property>
<property name="director" >
<ref bean="director1"/>
</property>
</bean>
<!-- property 태그 - 속성을 이용해 값 설정 -->
<bean id="movie2" class="di.movie.dto.MovieDTO">
<property name="movieId" value="0001"/>
<property name="title" value="간기남"/>
<property name="runningTime" value="60"/>
<property name="genre" value="멜로"/>
<property name="director" ref="director1"/>
</bean>
<!-- property 태그 - p : namespace 이용해 값 설정 -->
<bean id="movie3"
class="di.movie.dto.MovieDTO"
p:movieId="0001"
p:title="간기남"
p:runningTime="60"
p:genre="멜로"
p:director-ref="director1"/>
</beans>
package di.movie.dto;
public class DirectorDTO {
private String directorId;
private String name;
private int age;
public DirectorDTO() {
super();
}
public DirectorDTO(String directorId, String name, int age) {
super();
this.directorId = directorId;
this.name = name;
this.age = age;
}
public String getDirectorId() {
return directorId;
}
public void setDirectorId(String directorId) {
this.directorId = directorId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "DirectorDTO [directorId=" + directorId + ", name=" + name
+ ", age=" + age + "]";
}
}
package di.movie.dto;
public class MovieDTO {
private String movieId;
private String title;
private double runningTime;
private String genre;
private DirectorDTO director;
public MovieDTO() {
super();
}
public MovieDTO(String movieId, String title) {
super();
this.movieId = movieId;
this.title = title;
}
public MovieDTO(String movieId, String title, double runningTime) {
super();
this.movieId = movieId;
this.title = title;
this.runningTime = runningTime;
}
public MovieDTO(String movieId, String title, double runningTime,
String genre, DirectorDTO director) {
super();
this.movieId = movieId;
this.title = title;
this.runningTime = runningTime;
this.genre = genre;
this.director = director;
}
public String getMovieId() {
return movieId;
}
public void setMovieId(String movieId) {
this.movieId = movieId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getRunningTime() {
return runningTime;
}
public void setRunningTime(double runningTime) {
this.runningTime = runningTime;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public DirectorDTO getDirector() {
return director;
}
public void setDirector(DirectorDTO director) {
this.director = director;
}
@Override
public String toString() {
return "MovieDTO [movieId=" + movieId + ", title=" + title
+ ", runningTime=" + runningTime + ", genre=" + genre
+ ", director=" + director + "]";
}
}
* 결과
'프로그래밍 > Spring Framework' 카테고리의 다른 글
Collection 객체 주입하기 - spring_di_03 (0) | 2012.06.20 |
---|---|
Collection 객체 주입하기 - Spring (0) | 2012.06.20 |
property를 이용한 객체 주입-spring_di_02[실습] (0) | 2012.06.20 |
설정을 통한 객체 주입 - Property를 이용 (0) | 2012.06.20 |
설정을 통한 객체 주입 - Constructor 이용 - movie.xml[실습] (0) | 2012.06.20 |