Java Bean

DTO(Data Transfer Object)

DTO๋Š” ๊ณ„์ธต ๊ฐ„ ๋ฐ์ดํ„ฐ ๊ตํ™˜์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด๋‹ค. ์—ฌ๊ธฐ์„œ ๊ณ„์ธต์€ View-Controller-Service-DAO์™€ ๊ฐ™์€ ๊ณ„์ธต์„ ์˜๋ฏธ

๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์„ private ์†์„ฑ๊ณผ ๊ทธ ์†์„ฑ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” Getter, Setter ๋ฉ”์†Œ๋“œ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ๋‹ค.

VO(Value Object)์™€ ํ˜ผ์šฉ๋˜์–ด ์“ฐ์ด๋‚˜, VO๋Š” ๋‚ด๋ถ€ ์†์„ฑ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋Š”(imuutable) Read-Only์˜ ์˜๋ฏธ์  ํŠน์„ฑ์„ ๊ฐ€์ง„ ๊ฐ์ฒด์ด๋‹ค. ์ฆ‰, ๋ณ€๊ฒฝ์—†์ด ๊ฐ’์œผ๋กœ ์ทจ๊ธ‰ํ•  ๊ฐ์ฒด๋ฅผ ๋งํ•œ๋‹ค.

JavaBean(=Bean) ์ด๋ž€?

๋ฐ˜๋ณต์ ์ธ ์ž‘์—…์„ ํšจ์œจ์ ์œผ๋กœ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.

Bean ์ด๋ž€ JAVA ์–ธ์–ด์˜ ๋ฐ์ดํ„ฐ(์†์„ฑ)๊ณผ ๊ธฐ๋Šฅ(๋ฉ”์†Œ๋“œ)๋กœ ์ด๋ฃจ์–ด์ง„ ํด๋ž˜์Šค์ด๋‹ค.

  • Default ์ƒ์„ฑ์ž : ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—†๋Š” Default ์ƒ์„ฑ์ž๋ฅผ ๊ฐ–๊ณ  ์žˆ์–ด์•ผํ•œ๋‹ค.

  • Property : ์ž๋ฐ”๋นˆ์ด ๋…ธ์ถœํ•˜๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง„ ์†์„ฑ์„ Property๋ผ๊ณ  ํ•˜๋ฉฐ, Property๋Š” set์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ˆ˜์ •์ž ๋ฉ”์†Œ๋“œ(setter)์™€ get์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ ‘๊ทผ์ž ๋ฉ”์†Œ๋“œ(getter)๋ฅผ ์ด์šฉํ•ด ์ˆ˜์ • ๋˜๋Š” ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

DTO์™€ Java Beans์˜ ๊ด€๊ณ„์— ๋Œ€ํ•ด์„  DTO์˜ ํ˜•์‹์œผ๋กœ Java Beans๋ฅผ ๋”ฐ๋ฅด๊ณ  ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

Spring์—์„œ ์ง€์นญํ•˜๋Š” Bean์ด๋ž€, Spring์˜ IoC Container(=DI Container)๋ฅผ ํ†ตํ•ด ๊ด€๋ฆฌ(์ƒ์„ฑ, ์ œ์–ด)๋˜๋Š” ๊ฐ์ฒด๋ฅผ ๋งํ•˜๋ฉฐ, ์ด๋Š” Spring IoC Container์—์„œ ์ž์„ธํžˆ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

Bean ๋งŒ๋“ค๊ธฐ

๋นˆ์„ ๋งŒ๋“ ๋‹ค๋Š” ๊ฒƒ์€ ๋ฐ์ดํ„ฐ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค. ( getter, setter )

package example;

public class Student {

    private String name;
    private int age;
    private int grade;


    public Student() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

}

Bean ์‚ฌ์šฉํ•˜๊ธฐ

๊ด€๋ จ ์•ก์…˜ ํƒœ๊ทธ(useBean, getProperty, setProperty)๋กœ ์ฃผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ณ , ์–ป์–ด์˜ค๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

useBean

ํŠน์ • Bean์„ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๋ช…์‹œํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

<jsp:useBean id = "๋นˆ์ด๋ฆ„" class="ํŒจํ‚ค์ง€๋ช…์„ ํฌํ•จํ•œ ํด๋ž˜์Šค๋ช…" scope="์Šค์ฝ”ํ”„ ๋ฒ”์œ„"/>
<jsp:useBean id = "student" class="example.Student" scope="page"/>
  • Scope ๋ฒ”์œ„

scope

์„ค๋ช…

page

์ƒ์„ฑ๋œ ํŽ˜์ด์ง€ ๋‚ด์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

request

์š”์ฒญ๋œ ํŽ˜์ด์ง€ ๋‚ด์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

session

์›น ๋ธŒ๋ผ์šฐ์ € ์ƒ๋ช…์ฃผ๊ธฐ์™€ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

application

์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ƒ๋ช…์ฃผ๊ธฐ์™€ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉ๊ฐ€๋Šฅ

setProperty

๋ฐ์ดํ„ฐ ๊ฐ’์„ ์„ค์ •ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.(setter)

<jsp:setProperty name="๋นˆ ์ด๋ฆ„" property="์†์„ฑ ์ด๋ฆ„" value ="์†์„ฑ ๊ฐ’" />
<jsp:setProperty name="student" property="name" value ="ํ™๊ธธ๋™" />

getProperty

๋ฐ์ดํ„ฐ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.(getter)

<jsp:getProperty name="๋นˆ ์ด๋ฆ„" property="์†์„ฑ ์ด๋ฆ„" />
<jsp:getProperty name="student" property="name" />

์˜ˆ์ œ

  • Java Class

package example;

public class Student {

    private String name;
    private int age;
    private int grade;


    public Student() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

}
  • jsp๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<jsp:useBean id="student" class="example.Student" scope="page" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

    <jsp:setProperty name="student" property="name" value="๋ฐ•์„œ"/>
    <jsp:setProperty name="student" property="age" value="13"/>
    <jsp:setProperty name="student" property="grade" value="6"/>

    ์ด๋ฆ„  : <jsp:getProperty name="student" property="name" /><br />
    ๋‚˜์ด  : <jsp:getProperty name="student" property="age" /><br />
    ํ•™๋…„  : <jsp:getProperty name="student" property="grade" /><br />

</body>
</html>

Last updated