bean,properties설정이란?


Bean의존관계 설정방법

Property값 설정방법

Property파일을 이용한 설정


Bean의존관계 설정방법


Setter Injection

- Setter 메서드를 통해 의존관계가 있는 Bean을 주입하려면 <property>태그사용한다.

- ref는 객체 Bean이름을 통해 주입할 Bean을찾는다.

- value는 단순값이다.


ex)

class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
private String name;
 
private Printer printer;
 
 
 
public void setName(String name) {
 
    this.name = name;
 
}
 
 
 
public void setPrinter(Printer printer) {
 
    this.printer = printer;
 
}
 
 
 
public String sayHello() {
 
    return "Hello" + name;
 
}
 
 
 
public void print(){
 
    this.printer.print(sayHello());
 
}

bean.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<bean id="hello" class="myspirng.di.xml.Hello">
 
    <!-- setName(name) -->
 
    <property name="name" value="Spring"></property>
 
    <!-- 객체레퍼런스타입
 
    setPrinter(Printer) -->
 
    <property name="printer" ref="printer"></property>
 
</bean>
 
 
 
<bean id="printer" class="myspirng.di.xml.StringPrinter" />


Constructor Injection

- Constructor를 통해 의존관계가 있는 Bean을 주입하려면<constructor-arg>태그를사용한다.

- Constructor 주입방식은 생성자의 파라미터를 이용하기 때문에 한번에 여러개의 객체를 주입할 수 있다.<------------------Setter와차이점


ex)

class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private String name;
 
private Printer printer;
 
 
 
public Hello(){}
 
 
 
public Hello(String name, Printer printer) {
 
    this.name = name;
 
    this.printer = printer;
 
}


bean.xml

index경우

1
2
3
4
5
6
7
8
9
10
11
12
13
<bean id="hello" class="myspirng.di.xml.Hello">
 
    <!-- setName(name) -->
 
    <constructor-arg index="0" value="Spring"/>
 
    <!-- 객체레퍼런스타입
 
    setPrinter(Printer) -->
 
    <constructor-arg index="1" ref="printer"/>
 
</bean>

name경우

1
2
3
4
5
6
7
8
9
10
11
12
13
<bean id="hello" class="myspirng.di.xml.Hello">
 
    <!-- setName(name) -->
 
    <constructor-arg name="name" value="Spring"/>
 
    <!-- 객체레퍼런스타입
 
    setPrinter(Printer) -->
 
    <constructor-arg name="printer" ref="printer"/>
 
</bean>



Property설정방법

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!-- constructor등록 setter방식 -->
 
<bean id="hello2" class="myspirng.di.xml.Hello">
 
    <constructor-arg index="0" value="Spring"></constructor-arg>
 
    <constructor-arg index="1" ref="printer"></constructor-arg>
 
    <property name="names">
 
        <list>
 
            <value>AOP</value>
 
            <value>Spring</value>
 
            <value>DI</value>
 
        </list>
 
    </property>
 
</bean>




프로퍼티 파일을 이용할 설정방법

- 자주변경되는정보를 사용할때 사용한다.(Ex:DB정보)

- properties파일로 분리하는 깔끔하다.

- key=value로 구성된다.



프로퍼티파일생성

1
2
3
4
5
6
7
8
9
10
11
value.properties
 
myname=Spring
 
myprinter=printer
 
value1=AOP
 
value2=Spring
 
value3=DI


bean.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<context:property-placeholder location="classpath:config/value.properties"/>
 
<!-- constructor등록 setter방식 -->
 
<bean id="hello2" class="myspirng.di.xml.Hello">
 
    <constructor-arg index="0" value="${myname}"></constructor-arg>
 
    <constructor-arg index="1" ref="${myprinter}"></constructor-arg>
 
    <property name="names">
 
        <list>
 
            <value>${value1}</value>
 
            <value>${value2}</value>
 
            <value>${value3}</value>
 
        </list>
 
    </property>
 
</bean>




+ Recent posts