실습 2일차_exam
Spring exam 1_Constructor
MainClass.java
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.Cat;
import com.dto.Person;
public class MainClass {
public static void main(String[] args) {
// IoC Container
ApplicationContext ctx =
new GenericXmlApplicationContext("classpath:com/config/test.xml");
Person p1 = ctx.getBean("p1", Person.class);
Cat c = p1.getCat();
System.out.printf("이름:%s 나이:%s 주소:%s", p1.getUsername(), p1.getUserage(), p1.getAddress()); // printformat
System.out.printf("고양이 이름:%s 나이:%s 성별:%s", c.getName(), c.getAge(), c.getSex());
}
}
Cat.java
package com.dto;
public class Cat {
String name;
int age;
String sex;
// 기본 생성자 안 만들고 인자있는 생성자 만듬
public Cat(String name, int age, String sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Cat [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}
Person.java
package com.dto;
public class Person {
String username;
int userage;
String address;
Cat cat;
// 기본 생성자 안 만들고 인자있는 생성자 만듬
public Person(String username, int userage, String address, Cat cat) {
super();
this.username = username;
this.userage = userage;
this.address = address;
this.cat = cat;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getUserage() {
return userage;
}
public void setUserage(int userage) {
this.userage = userage;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
@Override
public String toString() {
return "Person [username=" + username + ", userage=" + userage + ", address=" + address + ", cat=" + cat + "]";
}
}
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="cat" class="com.dto.Cat"> <!-- 생성자로 주입 -->
<constructor-arg name="name" value="나비" />
<constructor-arg name="age" value="3" />
<constructor-arg name="sex" value="암컷" />
</bean>
<bean id="p1" class="com.dto.Person">
<constructor-arg name="username" value="고길동" />
<constructor-arg name="userage" value="30" />
<constructor-arg name="address" value="서울" />
<constructor-arg name="cat" ref="cat" />
</bean>
</beans>
* 출력 화면
Spring exam 2_setter
MainClass.java
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.Cat;
import com.dto.Person;
public class MainClass {
public static void main(String[] args) {
// IoC Container
ApplicationContext ctx =
new GenericXmlApplicationContext("classpath:com/config/test.xml");
Person p1 = ctx.getBean("p1", Person.class);
Cat c = p1.getCat();
System.out.printf("이름:%s 나이:%s 주소:%s", p1.getUsername(), p1.getUserage(), p1.getAddress()); // printformat
System.out.printf("고양이 이름:%s 나이:%s 성별:%s", c.getName(), c.getAge(), c.getSex());
}
}
Cat.java
package com.dto;
public class Cat {
String name;
int age;
String sex;
/*
* <bean id="p1" class="com.dto.Person">
<constructor-arg name="username" value="홍길동" />
<constructor-arg name="userage" value="30" />
<constructor-arg name="address" value="서울" />
<constructor-arg name="cat" ref="cat" />
</bean>
* constructor가 아닌 property는 기본 생성자가 있어야함
*/
public Cat() {
}
public Cat(String name, int age, String sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Cat [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}
Person.java
package com.dto;
public class Person {
String username;
int userage;
String address;
Cat cat;
/*
* <bean id="p1" class="com.dto.Person">
<constructor-arg name="username" value="홍길동" />
<constructor-arg name="userage" value="30" />
<constructor-arg name="address" value="서울" />
<constructor-arg name="cat" ref="cat" />
</bean>
* constructor가 아닌 property는 기본 생성자가 있어야함
*/
public Person() {
// TODO Auto-generated constructor stub
}
public Person(String username, int userage, String address, Cat cat) {
super();
this.username = username;
this.userage = userage;
this.address = address;
this.cat = cat;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getUserage() {
return userage;
}
public void setUserage(int userage) {
this.userage = userage;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
@Override
public String toString() {
return "Person [username=" + username + ", userage=" + userage + ", address=" + address + ", cat=" + cat + "]";
}
}
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="cat" class="com.dto.Cat"> <!-- 생성자로 주입 -->
<property name="name" value="나비" />
<property name="age" value="3" />
<property name="sex" value="암컷" />
</bean>
<bean id="p1" class="com.dto.Person">
<property name="username" value="홍길동" />
<property name="userage" value="30" />
<property name="address" value="서울" />
<property name="cat" ref="cat" />
</bean>
</beans>
* 출력 화면
출처
+ 강의 교재
'AI Bootcamp > Spring' 카테고리의 다른 글
[Spring] 실습 2일차_Injection_shortcut_c (0) | 2022.03.11 |
---|---|
[Spring] 실습 2일차_Injection_shortcut_p (0) | 2022.03.11 |
[Spring] 실습 2일차_Setter Injection (0) | 2022.03.10 |
[Spring] 실습 1일차_Injection (Constructor) (0) | 2022.03.10 |
[Spring] 실습 1일차_Bean Create (0) | 2022.03.10 |