일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Java#JAVA#매개변수
- 국비지원JAVA#국비지원자바#프로그랭#JSP#국비지원JSP#국비교육JSP#웹개발자#코딩
- 국비지원#국비교육
- 항해99 #항해플러스 #주니어개발자 #주니어개발자역량강화 #주니어개발자멘토링 #개발자사이드프로젝트 #코딩부트캠프 #코딩부트캠프후기
- tibero#티베로#이중화#failvover
- 프로그래밍
- 개발자#it도서#도메인#DDD#ddd
- java
- Spring#spring#스프링#스프링프레임워크#스프링자동주입#스프링생성자#스프링기본#국비지원#국비교육#국비지원스프링
- 국비지원JAVA#국비교육JAVA#국비지원자바#국비교육자바#JSP#SERVLET#서블릿#
- #
- 국비지원#국비교육#국비지원자바#국비교육자바#css#HTML#JAVA
- html#HTML#프론트엔드#개발자#코딩#국비지원#국비교육#국비지원프론트엔드#국비지원HTML#국비지원html#국비프론트엔드
- 항해플러스#항해#항해플러스3기#회고
- 국비지원JAVA
- IntelliJ#인텔리제이#인텔리#단축키
- spring#Spring#RequestBody#ResponseBody
- Java#java#자바#다오#디티오#브이오#dao#dto#vo
- 국비지원자바#국비교육자바#국비지원java#국비교육java#자바스크립트#프론트엔드
- Java#java#메모리영역#클래스로더#가비지컬렉터
- Java#컴파일러#자바컴파일러#
- Spring#spring#스프링#스프링프레임워크#스프링의존성주입#스프링생성자#스프링기본#국비지원#국비교육#국비지원스프링
- 개발자도서#개발자책#도메인#DDD#도메인주도개발시작하기#개발스터디#
- Java#정처기#비트연산자#정보처리기사
- #java#JAVA#프로그래밍#웹개발자
- 자바#Java#배열예시#연습#기초다지기
- Resource #
- 쿼리스트링#쿼리문자열#바인딩
- 자바
- db#데이터베이스#오라클#sql
Archives
- Today
- Total
개린이 개발노트
[Spring] 스프링 자동주입 본문
728x90
Bean을 정의할 때 주입할 객체는 생성자를 통한 주입이나 setter를 통한 주입을 사용했다.
Spring에서는 객체를 주입할 때 자동으로 주입될 수 있도록 설정이 가능하다.
자동 주입은 이름, 타입, 생성자를 통할 수 있으며 auto wire라는 용어로 부른다.
이름을통한주입
byName: 빈 객체의 property 이름과 정의된 Bean의 이름이 같은 것을 찾아 자동으로 주입함.
<?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='obj1' class='sanghoon.beans.TestBean1'>
<property name="data1" ref='data_bean1'/>
<property name="data2" ref='data_bean1'/>
</bean>
<bean id='data_bean1' class='sanghoon.beans.DataBean1' scope='prototype'/>
<bean id='obj2' class='sanghoon.beans.TestBean1' autowire="byName"/>
<bean id='data1' class='sanghoon.beans.DataBean1'/>
<bean id='data2' class='sanghoon.beans.DataBean1'/>
</beans>
package sanghoon.main;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import sanghoon.beans.TestBean1;
public class MainClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("sanghoon/config/beans.xml");
TestBean1 obj1 =ctx.getBean("obj1",TestBean1.class);
System.out.printf("obj1.data1:%s\n" , obj1.getData1());
System.out.printf("obj1.data2:%s\n" , obj1.getData2());
System.out.println("-----------------------");
TestBean1 obj2 = ctx.getBean("obj2",TestBean1.class);
System.out.printf("obj2.data1:%s\n" , obj1.getData1());
System.out.printf("obj2.data2:%s\n" , obj1.getData2());
ctx.close();
}
}
autowire에 bytype 이면 class타입으로 주입, constructor면 생성자로 주입
728x90
'스프링Spring' 카테고리의 다른 글
[Spring] Pojo란? (0) | 2023.03.05 |
---|---|
[Spring Framework] Application Context (0) | 2023.01.17 |
[Spring] 스프링 Spring Framework 컬렉션 주입 (0) | 2023.01.15 |
[Spring] 스프링 Setter 메서드를 통한 주입 (0) | 2023.01.15 |
[Spring] 스프링 생성자를 통한 주입 의존성 주입(Dependency Injection) (1) | 2023.01.15 |