본문 바로가기
Spring

[Spring] 03. Spring 컨테이너의 활용

by ssunooo 2024. 10. 1.

 

 

Spring 프레임워크 : "IoC와 AOP를 지원하는 경량의 프레임워크"

 

<IoC, 스프링 컨테이너 생성 / 컨테이너를 구동시키는 코드>

AbstractApplicationContext factory = new GenericXmlApplicationContext("applicationContext.xml");

applicationContext.xml 생성

 


결합도를 낮추기 위한 방법

 

개발 패턴 활용하기

  • 팩토리 패턴 - 객체 요청 시 해당 객체를 주는 패턴, ex) HandlerMapper

 

 

컨테이너 설정

 

  • 컨테이너 - 개발자 대신 객체 관리
  • .xml에서 스프링 컨테이너 설정

 

 

설정 요소

 

beans - 루트(최상위) 엘리먼트 (요소, 태그)

 

< applicationContext.xml >

<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 class="test.IPhone" id="apple"></bean>
   <bean class="test.GalaxyPhone" id="samsung"></bean> -- 빈 등록 == useBean과 닮았다

   
</beans>

 

 

<bean을 불러오는 코드 - UseBean과 유사>

Phone phone = (Phone)factory.getBean(bean 이름);
Phone phone = (Phone)factory.getBean(args[0]);


Bean - 자바객체 : POJO
getBean - 객체를 요청 : look up

 

args[] - Program arguments로 지정 가능

Run Configurations - Program arguments에 지정후 Run

 

 

<bean 설정 코드>

<bean class="test.GalaxyPhone" id="samsung" lazy-init="true" init-method="initMethod" scope="singleton"></bean>

 

  • lazy-init="true" - 지연로딩으로 설정 변경 (default = pre-loading - Run과 동시에 생성자 생성)
  • init-method="initMethod" - init() 함수에서 초기화
  • scope="singleton" / scope="prototype" - prototype으로 설정 시 생성자 중복 생성 가능



POJO (Plain Old Java Object) 란?


오래된 방식의 간단한 자바 오브젝트로

Java EE 등의 중량 프레임워크들을 사용 시

해당 프레임워크에 종속된 "무거운" 객체를 만들게 된 것에 반발해서 사용
 
이후에 특정 자바 모델이나 기능, 프레임워크 등을 따르지 않은 자바 오브젝트를 지칭
 
POJO가 다른 분야에서 사용되는 언어

  • POPO : Plain Old PHP Object, PHP 언어에서 사용
  • POCO : Plain Old CLR Object, 닷넷 프레임워크에서 사용
  • PODS : Plain Old Data Structures, C++ 언어에서 오직 C 언어의 특징만 사용하는 경우
  • POD : Plain Old Documentation, 펄(Perl) 언어에서 사용
  • POTS : Plain Old Telephone Service

 

 


2024.10.01