hyeonga_code
Spring_의존성 주입_p, c 네임스페이스 사용하기, 룩업 메소드 인젝션 방식 본문
- 네임스페이스 사용하기
- applicationContext.xml
- Namespaces > c, p 체크합니다.
- <property>태그를 사용하지 않고 간단하게 사용할 수 있습니다.
'applicationContext.xml' 파일의 beans에 추가됩니다.
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
- p 네임스페이스
p:'변수이름'-ref = '참조하는 객체의 아이디'
p:'변수 이름' = '설정값'
<bean id="tv" class="basic.spring.polymorphism.SamsungTV7" p:speaker-ref="son" p:price="1500000">
- c 네임스페이스
c:'변수 이름'-ref = '참조하는 객체의 아이디'
c:'변수 이름' = '설정값'
<bean id="tv" class="basic.spring.polymorphism.SamsungTV7" c:speaker-ref="apple" c:price="4500000">
- src/main/resources > applicationContext.xml 파일 수정하기
=====
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- p, c 네임 스페이스 사용
p/c : 변수 이름-ref = '참조할 객체 아이디';
p/c : 변수 이름 = '설정할 값'; -->
<bean id="lgTV2" class="sts.spring.ioc3.LgTV2" p:speaker-ref="sony" p:price="1300" />
<bean id="sony" class="sts.spring.ioc3.SonySpeaker" />
</beans>
|
- sts.spring.ioc3 > LgTV2.java 클래스 생성
=====
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
package sts.spring.ioc3;
public class LgTV2 implements TV{
private Speaker sp;
// 가격 변수를 선언합니다.
private int price;
public LgTV2() {
System.out.println("[ Create LgTV2 ]");
}
public LgTV2(Speaker sp) {
System.out.println("Create LgTV2 : Injection");
this.sp = sp;
}
// 생성자 추가
public LgTV2(Speaker sp, int price) {
System.out.println("Create LgTV2 : MultiMapping Injection");
this.sp = sp;
this.price = price;
}
// getter/setter 메소드 추가
public Speaker getSpeaker() {
return sp;
}
public void setSpeaker(Speaker sp) {
System.out.println("LgTV2 setSpeaker()");
this.sp = sp;
}
public void setPrice(int price) {
System.out.println("LgTV2 setPrice()");
this.price = price;
}
@Override
public void powerOn() {
System.out.println("LgTV2 powerOn\n - price : " + price + "\n");
}
@Override
public void powerOff() {
System.out.println("LgTV2 powerOff");
}
@Override
public void volumeUp() {
sp.volumeUp();
}
@Override
public void volumeDown() {
sp.volumeDown();
}
}
|
- sts.spring.ioc3 > TVUser.java 클래스 생성
=====
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
36
37
|
package sts.spring.ioc3;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class TVUser3 {
public static void main(String[] args) {
// Spring 컨테이너를 구동합니다.
AbstractApplicationContext fac = new GenericXmlApplicationContext("applicationContext.xml");
// Spring 컨테이너에 등록한 객체를 요청합니다.
TV lgTV2 = (TV) fac.getBean("lgTV2");
lgTV2.powerOn();
lgTV2.volumeUp();
lgTV2.volumeDown();
lgTV2.powerOff();
System.out.println();
// Spring 컨테이너를 종료합니다.
fac.close();
/*
[ Create LgTV2 ]
+++ Create SonySpeaker +++
LgTV2 setPrice()
LgTV2 setSpeaker()
LgTV2 powerOn
- price : 1300
+ SonySpeaker : volumeUp +
+ SonySpeaker : volumeDown +
LgTV2 powerOff
*/
}
}
|
- 룩업 메소드 인젝션
- lookup-method
- 규칙
- 접근 수식어가 public, protected
- 리턴 타입이 void이면 안된다
- 인자를 갖지 않는다
- 추상 메소드도 가능하다
- 추상 메소드가 아닐 경우 코드를 임의로 구현
- final이 아니다.
- 임의의 빈 객체를 전달
- 식별값을 갖지 않으므로 임의 빈 객체는 재사용할 수 없다.
'Spring' 카테고리의 다른 글
Spring_의존성 주입_어노테이션 기반 설정 (0) | 2023.12.17 |
---|---|
Spring_Spring DI_컬렉션 타입_ 컬렉션 객체 설정 (1) | 2023.12.17 |
Spring_의존성 주입_Setter 인젝션 사용하기 (0) | 2023.12.16 |
Spring_의존성 주입_의존 관계의 변경 (0) | 2023.12.16 |
Spring_스프링 의존성 주입_생서자 인젝션 이용하기, 다중 변수 매핑 (0) | 2023.12.16 |