본문 바로가기

Archived(Programming)/Spring #2(기초)

Layered Architecture & @Component

# 계층화 아키텍처

효율적인 개발과 유지보수를 위해 계층화하여 개발

대부분의 중/대규모 어플리케이션에서 적용

각 레이어는 독립된 R&R을 가짐

3 가지 영역으로 구분(Presentation, Business, Data)

1) Presentation 영역

사용자와 상호작용을 담당

사용자의 요청을 분석/응답

 

2) Business 영역

 기능 수행, 트랜잭션 수행

 

3) Data 영역

데이터의 저장과 조회를 담당

주로 DB와 연동하여 작업

# MVC 패턴

Layered Architecture를 사용한 대표적인 패턴

어플리케이션을 Model, View, Controller로 구분

UI를 가지는 대부분의 어플리케이션은 MVC 기반

(Angular, Android, iOS, SpringMVC 등등)

# 컴포넌트 자동등록

@Annotation을 사용

@Component와 하위 어노테이션을 사용

<context:component-scan base-package="패키지 명" />

패키지 명 이후 하위 패키지를 검색해 @Component 어노테이션을 포함하는 모든 클래스를 빈으로 자동 등록

 

@AutoWired를 통해 의존관계 자동 주입 가능

# 실습 1 수작업- 게시판의 글쓰기 기능 구현(가상 구현)

VO 클래스 작성

package kr.co.acomp.hello.vo;

public class Article {
	private int articleId;
	private String author;
	private String title;
	private String content;

	// constructor
	public Article() {}
	public Article(int articleId, String author, String title, String content) {
		super();
		this.articleId = articleId;
		this.author = author;
		this.title = title;
		this.content = content;
	}
	
	// setter getter
	public int getArticleId() {
		return articleId;
	}
	public void setArticleId(int articleId) {
		this.articleId = articleId;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	
	// to string
	@Override
	public String toString() {
		return "Article [articleId=" + articleId + ", author=" + author + ", title=" + title + ", content=" + content
				+ "]";
	}
	
	
}

DAO 클래스 작성

package kr.co.acomp.hello.dao;

import kr.co.acomp.hello.vo.Article;

public class ArticleDAO {
	
	// 디버깅 용
	public void insertArticle(Article article) {
		System.out.println("insert OK..");
	}
}

Service 클래스 작성

package kr.co.acomp.hello.service;

import kr.co.acomp.hello.dao.ArticleDAO;
import kr.co.acomp.hello.vo.Article;

public class BbsService {
	
	private ArticleDAO articleDAO;
	public void registArticle(Article article) {
		articleDAO.insertArticle(article);
	}
}

spring-context.xml 설정파일에 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="helloService" class="kr.co.acomp.hello.service.HelloService">
		<property name="helloDAO" ref="helloDAO" />
		<property name="anotherDAO" ref="antherDAO" />
	</bean>
	<bean id="helloDAO" class="kr.co.acomp.hello.dao.HelloDAO" /> 
	<bean id="antherDAO" class="kr.co.acomp.hello.dao.AnotherDAO" />
	
	<bean id="bbsService" class="kr.co.acomp.hello.service.BbsService">
		<property name="articleDAO" ref="articleDAO" />
	</bean>
	<bean id="articleDAO" class="kr.co.acomp.hello.dao.ArticleDAO" /> 
</beans>

Dependency Injection  코드 추가

package kr.co.acomp.hello.service;

import kr.co.acomp.hello.dao.ArticleDAO;
import kr.co.acomp.hello.vo.Article;

public class BbsService {
	
	private ArticleDAO articleDAO;
	
	// setter 통한 DI
	public void setArticleDAO(ArticleDAO articleDAO) {
		this.articleDAO = articleDAO;
	}
	
	public void registArticle(Article article) {
		articleDAO.insertArticle(article);
	}
}

Main 코드 변경

package kr.co.acomp;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import kr.co.acomp.hello.service.BbsService;
import kr.co.acomp.hello.vo.Article;

public class HelloMain {

	public static void main(String[] args) {
		AbstractApplicationContext ctx =
				new ClassPathXmlApplicationContext("/spring-context.xml");
		
		BbsService service = ctx.getBean("bbsService", BbsService.class);
		service.registArticle(new Article());
	}

}

# 실습 2- @Annotation 통한 작업

package kr.co.acomp.hello.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import kr.co.acomp.hello.dao.ArticleDAO;
import kr.co.acomp.hello.vo.Article;

@Service //service 어노테이션
public class BbsService {

	@Autowired // 자동으로 DI를 해줌
	private ArticleDAO articleDAO;
	
	public void registArticle(Article article) {
		articleDAO.insertArticle(article);
	}
}
package kr.co.acomp.hello.dao;

import org.springframework.stereotype.Repository;

import kr.co.acomp.hello.vo.Article;

@Repository // Repository Annotation
public class ArticleDAO {
	
	// 디버깅 용
	public void insertArticle(Article article) {
		System.out.println("insert OK..");
	}
}

namespace 체크

context:component-scan 추가!

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	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-3.1.xsd">

	<context:component-scan base-package="kr.co.acomp.hello.hello"></context:component-scan>

	<bean id="helloService" class="kr.co.acomp.hello.service.HelloService">
		<property name="helloDAO" ref="helloDAO" />
		<property name="anotherDAO" ref="antherDAO" />
	</bean>
	<bean id="helloDAO" class="kr.co.acomp.hello.dao.HelloDAO" /> 
	<bean id="antherDAO" class="kr.co.acomp.hello.dao.AnotherDAO" />
	
</beans>

 

'Archived(Programming) > Spring #2(기초)' 카테고리의 다른 글

@Controller  (0) 2020.03.11
Spring MVC  (0) 2020.03.11
DI(Dependency Injection)  (0) 2020.03.10
IoC(Inversion of Control)  (0) 2020.03.10
Spring과 Maven  (0) 2020.03.09