반응형
250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 자바스크립트recude
- 인프런자바스크립트
- 이벤트리스너
- c#
- 자바스크립트틱택토
- 객체의비교
- 인프런
- 제로초
- 자바스크립트함수
- .NET
- sort
- HTTP
- 콜백함수
- EntityFramework
- 객체리터럴
- 인프런인강
- 자바스크립트객체리터럴
- 자바스크립트파라미터
- Blazor
- 인터넷프로토콜
- 자바스크립트
- 고차함수
- 인프런강의
- 비주얼스튜디오
- 코딩
- 인프런무료강좌
- 틱택토구현
- 인프런강좌
- slice
- NPM
Archives
- Today
- Total
샐님은 개발중
06. 의존 객체 자동 주입 본문
728x90
반응형
의존 객체 자동 주입이란?
-스프링 설정 파일에서 의존 객체를 주입할 때 또는 태그로 의존 대상 객체를 명시하지 않아도 스프링 컨테이너 가 자동으로 필요한 의존 대상 객체를 찾아서 의존 대상 객체가 필요한 객체에 주입해 주는 기능이다. 구현 방법은
@Autowired와 @Resource 어노테이션을 이용해서 쉽게 구현할 수 있다.
@Autowired
- 주입하려고 하는 객체의 타입이 일치하는 객체를 스프링 컨테이너 내에세 찾아 자동으로 주입한다.
기존 appicationContext.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="cwalk" class="testprj.TranspotationWalk" /> -->
<bean id="studentDao" class="testprj.dao.StudentDao" ></bean>
<bean id="cregisterService" class="testprj.service.StudentRegiterService">
<!--기존 파일에서 constructor-arg 를 사용하지 않고 생성자에 studentDao를 주입시킨다.-->
<constructor-arg ref="studentDao" ></constructor-arg>
</bean>
<bean id="cSelectAllService" class="testprj.service.StudentSelectAllService">
<constructor-arg ref="studentDao" ></constructor-arg>
</bean>
<bean id="cSelectService" class="testprj.service.StudentSelectService">
<constructor-arg ref="studentDao" ></constructor-arg>
</bean>
</beans>
변경후
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<bean id="studentDao" class="testprj.dao.StudentDao" ></bean>
<bean id="cregisterService" class="testprj.service.StudentRegiterService">
</bean>
<bean id="cSelectAllService" class="testprj.service.StudentSelectAllService">
</bean>
<bean id="cSelectService" class="testprj.service.StudentSelectService">
</bean>
</beans>
Main.java 에서 바뀐 xml 을 사용한다.
@Resource
@Resource란?
- 객체의 이름과 같은 bean id 를 찾아 생성해준다.
- @Autowired 와 다르게 생성자에서는 못쓰고 프로퍼티 또는 메소드에서만 가능
#springframework #스프링프레임워크 #어노테이션 #자바 #개발자공부 #코딩 #프로그래밍 #인프런강의 #스프링강의
728x90
반응형
'Spring Framework > 인프런강의-자바 스프링 프레임워크' 카테고리의 다른 글
08. 생명주기 (0) | 2023.05.21 |
---|---|
07. 의존객체 선택 (0) | 2023.05.21 |
05. 스프링 설정 파일 (0) | 2023.05.21 |
04. DI 의존성 객체 주입 (0) | 2023.05.21 |
03.스프링 프로젝트 생성 (2) | 2023.05.21 |