[Spring] xml 파일 작성부터 콘솔출력까지

·2023년 1월 25일
0

Spring

목록 보기
1/4

🌿개발을 보다 쉽고 편리하게 할 수 있도록 지원해주는 오픈소스 프레임워크

🌱특징

🌼빈(bean) 태그로 객체를 생성 및 관리

🌼가벼워서 경량 컨테이너라고도 부름

🌼loC(Inversion of Control): 역제어 컨테이너

🌼스프링 컨테이너: Spring에서 객체들을 관리하는 공간

---> =싱글톤 컨테이너
---> 최상위 컨테이너 : BeanFactory

🌱객체 생성하기(<bean> 태그)

1️⃣ Bean 객체 활용을 위한 java클래스파일 생성(Team.java/Player.java)

---> 생성자, 기본생성자, getter/setter메소드 포함

public class Team {
	private String tname;
	private String loc;
    .
    .
}
public class Player {
	private String name;
	private int age;
	private Team team; //Team 유형
    .
    .    
}

2️⃣ Bean 객체 활용을 위한 xml파일 생성 & 작성(nol.xml)

---> Spring Bean Configuration File로 생성한 후 작성

<bean id="team" class="model.domain.Team">
	<constructor-arg name="tname" value="흥국생명"/>
	<constructor-arg name="loc" value="수원"/>
</bean>
<bean id="player" class="model.domain.Player">
	<property name="name" value="김연경"/>
	<property name="age" value="34"/>
	<!-- team 참조 -->
	<property name="team">
		<ref bean="team"/>
	</property>
</bean>

🌼constructor-arg : 매개변수가 존재하는 생성자 호출해서 데이터

---> name 속성값을 써주지 않으면 생성자 순서대로 value값이 들어감

🌼property : 기본 생성자 호출 후 setXxx()호출하여 초기화

🌼ref(reference) : 참조. 다른 클래스를 참조하여 객체호출

🌱Scope: 빈이 존재할 수 있는 범위

🌼SingleTone(기본으로 제공하는 스코프)

---> 가장 넓은 범위
---> 빈 객체를 오직 하나만 생성해서 공유함

🌼ProtoType

---> 생성/관계까지만 관여하는 스코프
---> 요청될때마다 빈 객체 생성

🌱출력 테스트(RunningTest.java/main메소드 사용)

ApplicationContext context = new ClassPathXmlApplicationContext("nol.xml");
Team team = context.getBean("team", Team.class);

🌼ApplicationContext : BeanFactory를 구현한 인터페이스

---> 빈 객체를 호출함.

🌼context.getBean("빈 변수", 생성할 객체클래스)

profile
웹개발입문자

0개의 댓글