
DI / IoC를 위해 xml 파일을 만들때 Spring Bean Configuration File로 만들었었습니다!


파일 이름을 작성하고 Finish가 아니라 Next를 누르게 되면

뭔가가 많이 나와있죠?
다양한 속성을 사용할수 있도록 도와주는 역할을 합니다.
저희는 이번에 c: 속성과 p:속성을 사용하겠습니다!

p도 사용할 예정이지만 일단 c만 만들어서 Finish를 눌러볼까요??

xml 파일이 만들어질때 뭔가가 한줄이 더 작성되어서 만들어졌습니다!
근데 아까 p속성도 사용한다고 했는데 왜 안했냐구요??

왼쪽 하단에 Namespaces가 있습니다!
클릭해주시고 혹시 이 창이 없으신 분들이라면 metadata를 삭제하고 다시 만들어보시고 그래도 안된다면 이클립스 버전을 업데이트 해보시기바랍니다~!

Namespaces에 들어오면 기존에 사용하던 beans에 체크가 되어있고 파일을 만들때 체크했던 c에 체크가 되어있습니다.

이제 p에 체크를 하고 다시 Source로 넘어가 볼까요??

만들때 없었던 내용이 또 생겼습니다!
그러면 체크한 것들과 코드에 생긴 url을 어떻게 사용하는지 예제를 볼까요??
Emp클래스와 이를 상속받는 Developer클래스와 Engineer클래스를 만들었습니다.
패키지 구조

Emp.java
package com.test05;
public class Emp {
private String name;
private int salary;
public Emp() {
}
public Emp(String name, int salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return "이름 : " + name + " \t 월급 : " + salary;
}
}
Developer.java
package com.test05;
public class Developer {
private Emp emp;
private String dept;
public Developer() {
}
public Developer(Emp emp, String dept) {
this.emp = emp;
this.dept = dept;
}
public Emp getEmp() {
return emp;
}
public void setEmp(Emp emp) {
this.emp = emp;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
@Override
public String toString() {
return emp + " \t 부서 : " + dept;
}
}
Engineer.java
package com.test05;
public class Engineer {
private Emp emp;
private String dept;
public Engineer() {
}
public Engineer(Emp emp, String dept) {
this.emp = emp;
this.dept = dept;
}
public Emp getEmp() {
return emp;
}
public void setEmp(Emp emp) {
this.emp = emp;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
@Override
public String toString() {
return emp + " \t 부서 : " + dept;
}
}
applicationContext.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"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- c: constructor -->
<bean id="lee" class="com.test05.Emp" c:name="이순신" c:salary="3000000" />
<bean id="lee-ss" class="com.test05.Developer" c:emp-ref="lee" c:dept="개발팀" />
<!-- p: property -->
<bean id="hong" class="com.test05.Emp" p:name="홍길동" p:salary="2500000" />
<bean id="hong-gd" class="com.test05.Engineer" p:emp-ref="hong" p:dept="기술팀" />
</beans>
MTest.java
package com.test05;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MTest {
public static void main(String[] args) {
ApplicationContext factory = new ClassPathXmlApplicationContext("com/test05/applicationContext.xml");
Developer lee = (Developer) factory.getBean("lee-ss");
Engineer hong = factory.getBean("hong-gd", Engineer.class);
System.out.println(lee);
System.out.println(hong);
}
}
실행 결과

applicationContext.xml파일에서 보면 <bean>태그의 속성으로 c:name, c:salary / p:name, p:salary를 사용하였습니다.
Emp, Developer, Engineer클래스 모두 기본생성자, 필드에 대한 파라미터가 있는 생성자, 게터&세터가 있습니다.
<!-- c: constructor -->
<bean id="lee" class="com.test05.Emp" c:name="이순신" c:salary="3000000" />
<bean id="lee-ss" class="com.test05.Developer" c:emp-ref="lee" c:dept="개발팀" />
이 코드는
<bean id="lee" class="com.test05.Emp">
<constructor-arg name="name" value="이순신" />
<constructor-arg name="salary" value="3000000" />
</bean>
<bean id="lee-ss" class="com.test05.Developer">
<constructor-arg name="emp" ref="lee" />
<constructor-arg name="dept" value="개발팀" />
</bean>
와 동일한 코드입니다. 그치만 c:속성을 사용해서 코드를 조금 간결하게 만들어줍니다!
c:은 constructor-arg와 동일한 의미라고 볼수 있습니다!
<!-- p: property -->
<bean id="hong" class="com.test05.Emp" p:name="홍길동" p:salary="2500000" />
<bean id="hong-gd" class="com.test05.Engineer" p:emp-ref="hong" p:dept="기술팀" />
p:은 마찬가지로 property와 동일합니다
<bean id="honf" class="com.test05.Emp">
<property name="name" value="홍길동" />
<property name="salary" value="2300000" />
</bean>
<bean id="hong-gd" class="com.test05.Engineer">
<property name="emp" ref="hong" />
<property name="dept" value="기술팀" />
</bean>
이것과 같은 코드라고 볼수있습니다! 어렵지 않죠?
'Java 관련 > Spring Legecy' 카테고리의 다른 글
[Spring] DI / IoC (Bean 객체_09) - 호출 (0) | 2022.03.18 |
---|---|
[Spring] DI / IoC (Bean 객체 생성_08) - <bean>태그 속성 (0) | 2022.03.17 |
[Spring] DI / IoC (Bean 객체 생성_06) - 객체 참조(ref) (0) | 2022.03.15 |
[Spring] DI / IoC (Bean 객체 생성_05) - construct-arg와 property 동시 사용도 될까? (0) | 2022.03.14 |
[Spring] DI / IoC (Bean 객체 생성_04) - Singleton (0) | 2022.03.13 |