티스토리 뷰
반응형
Autowire 사용과 Resource의 사용
<?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: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-3.1.xsd">
<context:annotation-config />
<bean id="tire" class="expert004.KoreaTire"></bean>
<bean id="wheel" class="expert004.AmericaTire"></bean>
<bean id="car" class="expert004.Car"></bean>
</beans>
public class Car {
// TODO: 속성 자동 주입
@Autowired
Tire tire;
public String getTireBrand() {
return "장착된 타이어: " + tire.getBrand();
}
}
public class Car {
// TODO: 속성 자동 주입
@Resource
Tire tire;
public String getTireBrand() {
return "장착된 타이어: " + tire.getBrand();
}
}
위 코드를 본다면 차이가 보이지 않는다. 단, 어노테이션만 다를 뿐이다.
| @Autowire | @Resource | |
|---|---|---|
| 출처 | 스프링 프레임워크 | 표준 자바 |
| 소속 패키지 | org.springframework.beans.factory.annotation.Autowired | import javax.annotation.Resource |
| 빈 검색 | byType 먼저, 못찾으면 byName | byName 먼저, 못찾으면 byType |
| 특이사항 | @Qualifire("") | name 어트리뷰트 |
| byName 강제하기 | @Autowire @Qualifire("tire1") |
@Resource(name = "tire1") |
Reference
반응형
'SPRING 🍃 > Basic' 카테고리의 다른 글
| @Configuration의 어노테이션은 SingleTon 이다. (0) | 2023.04.01 |
|---|---|
| Spring DI - 여러가지 방법 편 (0) | 2022.04.17 |
| Spring AOP의 용어 (0) | 2022.04.16 |
| Spring AOP 관점지향 프로그래밍 (0) | 2022.04.15 |
| Spring ICO / DI - 의존성 주입 편 (0) | 2022.04.14 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Spring
- OOP
- 알고리즘
- 프로그래머스
- 백준
- C언어
- docker
- CS
- security
- 수학
- 그래프
- 스프링부트
- springboot
- 자바
- kakao
- java
- 릿코드
- 스프링
- 디자인패턴
- 자격증
- nginx
- 매트랩
- Solid
- interview
- Matlab
- Algorithm
- ajax
- JPA
- spring-cloud
- 면접
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
글 보관함