본문 바로가기
수업 내용/Spring

(54일차) 1월 7일

by 효자로 캉테 2022. 1. 7.

[Spring]

 

설치: Spring Tool Suite 3.9.14 (full distribution on Eclipse 4.15) 

 

스프링 설정

 1. General => Workspace 탭 => Text file encoding 항목에 Other: UTF-8로 설정

 2. Web => 각 CSS, HTML, JSP Files 탭 => Encoding: ISO(UTF-8)로 설정

 3. 서버창 아래 기존 스프링 서버 삭제 => Tomcat 9.0 server 설정  

 

 


 

 

* 스프링의 핵심 :  빈(인스턴스)등록과 의존 설정

 

[Bean 등록과 의존관계 설정법] (bean태그 이용법, 어노테이션 이용법)

 1. GenericXmlApplicationContext 클래스

  - 스프링 설정 xml 파일에서 정의한 <bean> 설정 정보를 읽어와 내부적으로 객체를 생성하고 초기화한다.

// 컨테이너 가동 (삼성Tv,LgTv 인스턴스 생성)

GenericXmlApplicationContext container = new GenericXmlApplicationContext("aaa.xml");

TV tv = (TV) container.getBean("tv"); // Dependancy Lookup: 아이디 "tv"을 로드
		
tv.powerOn();
tv.volumeUp();
tv.volumeDown();
tv.powerOff();

 

 

 1.1 Bean 태그 이용

 

  1) <bean> (bean 등록: 인스턴스 생성)

   - 스프링(Spring)이 IoC(Inversion of Control) 방식으로 관리하는 오브젝트

   - 스프링(Spring)이 직접 그 생성과 제어를 담당하는 오브젝트

 

  * Bean의 주요 속성

   - class : 정규화된 자바 클래스의 이름

   - id : 빈(Bean)의 고유 식별자

   - scope : 객체의 범위(singleton과 prototype)

   - property : 생성시 bean setter에 전달할 인수 -> setter가 없으면 에러 발생  

<!-- xml 예제 -->
<bean class="polymorphism.SamsungTv">  <!-- 삼성tv 인스턴스 생성 -->

 

 

  2) <property> (DI : 의존관계 설정)

   - setter 메소드를 통해 의존관계가 있는 bean에 주입. 즉, bean 객체 간의 의존 관계를 설정 (name="speaker" => setSpeaker())

   - ref : 주입할 bean의 이름

<!-- bean 태그 : 인스턴스 생성 -->
<bean id="tv" class="polymorphism.SamsungTv">  <!-- 삼성tv 인스턴스 생성 -->
<property name="speaker" ref="sonySpeaker"></property>  <!-- DI: 부품 조립 -->
</bean>													                       
	
<bean id="sonySpeaker" class="polymorphism.SonySpeaker"></bean>  <!-- 소니 스피커 인스턴스 생성 -->
<bean id="appleSpeaker" class="polymorphism.AppleSpeaker"></bean>  <!-- 애플 스피커 인스턴스 생성 -->

 

 

 

 1.2 어노테이션 이용법

 

  1) <context:component-scan> (빈 등록: 인스턴스 생성)

   - 스프링이 자동으로 해당 어노테이션이 붙은 클래스들을 스캔하여 bean으로 등록해주는 설정

<!--  어노테이션 설정법 -->
<context:component-scan base-package="패키지명"></context:component-scan>

 

 

  2) 어노테이션 종류

   @Component :  해당 Class를 Bean으로 등록할 때 사용

 

   @Controller해당 Class가 Controller의 역할을 한다고 명시할 때 사용

 

   @Service :  해당 Class가 비즈니스 로직을 수행한다고 명시할 때 사용

 

   @Repository :  DB에 접근하는 메소드를 가진 Class라고 명시할 때 사용

 

   @Autowired (DI : 의존관계 설정) :  의존관계가 있는 bean을 찾아 주입 (field, setter method, 생성자에서 사용)

 

 


 

 

* 프로젝트 생성 : Spring Legacy Project => 프로젝트명 SpringEx (Spring MVC Project 선택) => 패키지 설정 (com.ja.springex)

 

* .xml 파일 생성 : scr/main/resources 폴더 선택 => New => Spring Bean Configuration File => 생성

 

 

프로젝트 SpringEx => 패키지 polymorphism => 클래스 TVUser, SamsungTv, LgTv, TvContainer (src/main/java 폴더)

                                                                            인터페이스 TV (src/main/java 폴더)

                                                                            빈 aaa.xml (src/main/resources 폴더) 

  

                                                                            인터페이스 Speaker (src/main/java 폴더)

                                                                            클래스 SonySpeaker, AppleSpeaker (src/main/java 폴더)

                                                                            빈 bbb.xml (src/main/resources 폴더)

 

'수업 내용 > Spring' 카테고리의 다른 글

(59일차) 1월 14일  (0) 2022.01.14
(58일차) 1월 13일  (0) 2022.01.13
(57일차) 1월 12일  (0) 2022.01.12
(56일차) 1월 11일  (0) 2022.01.11
(55일차) 1월 10일  (0) 2022.01.10