[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item03 ์™„๋ฒฝ๊ณต๋žต. ์ง๋ ฌํ™” Serializable

2023. 1. 11. 11:12ใ†JAVA/Effective JAVA

728x90

 

item03. private ์ƒ์„ฑ์ž๋‚˜ ์—ด๊ฑฐ ํƒ€์ž…์œผ๋กœ ์‹ฑ๊ธ€ํ†ค์ž„์„ ๋ณด์ฆํ•˜๋ผ. 

" p24. ์ง๋ ฌํ™”, ์—ญ์ง๋ ฌํ™”, Serializable, transient"

 

 

 

์ง๋ ฌํ™” Serializable ๋ž€? 

: ๊ฐ์ฒด๋ฅผ ๋ฐ”์ดํŠธ์ŠคํŠธ๋ฆผ์œผ๋กœ ์ƒํ˜ธ ๋ณ€ํ™˜ํ•˜๋Š” ๊ธฐ์ˆ  

์ž๋ฐ” ์‹œ์Šคํ…œ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ์ฒด ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์™ธ๋ถ€์˜ ์ž๋ฐ” ์‹œ์Šคํ…œ์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก byte ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜ํ•˜๋Š” ๊ธฐ์ˆ ์ด๊ณ , 

์—ญ์ง๋ ฌํ™”๋Š” ์ด๋ ‡๊ฒŒ byte ๋กœ ๋ณ€ํ™˜๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์‹œ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ธฐ์ˆ ์„ ๋งํ•œ๋‹ค. 

 

JVM์˜ ๋ฉ”๋ชจ๋ฆฌ์— ์ƒ์ฃผ(ํž™ ๋˜๋Š” ์Šคํƒ) ๋˜์–ด ์žˆ๋Š” ๊ฐ์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”์ดํŠธ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ธฐ์ˆ (์ง๋ ฌํ™”) ๊ณผ ๋ฐ”์ดํŠธ ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•ด์„œ JVM์œผ๋กœ ์ƒ์ฃผ์‹œํ‚ค๋Š” ํ˜•ํƒœ(์—ญ์ง๋ ฌํ™”) 

 

๋ฐ”์ดํŠธ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋ณ€ํ™”๋‚œ ๊ฐ์ฒด๋ฅผ ํŒŒ์ผ๋กœ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด ๋‹ค๋ฅธ ์‹œ์Šคํ…œ์œผ๋กœ ์ „์†กํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

์ง๋ ฌํ™” Serializable ์‚ฌ์šฉ ๋ฐฉ๋ฒ• 

: ์ง๋ ฌํ™”๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”  java.io.Serializable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์† ๋ฐ›์•„์•ผ ์ง๋ ฌํ™”๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค. 

(์ธํ„ฐํŽ˜์ด์Šค ์ƒ์† ์•ˆ๋ฐ›์œผ๋ฉด ์—๋Ÿฌ๋‚จ!) 

public class Book implements Serializable {

    private static final long serialVersionUID = 1L;

    public static String name;
    private String isbn;
    private String title;
    private String author;
    private LocalDate published;
    private  transient int numberOfSold; // transient ๋ฅผ ๋ถ™์ด๋ฉด ์ง๋ ฌํ™” X

    public Book(String isbn, String title, String author, LocalDate published) {
        this.isbn = isbn;
        this.title = title;
        this.author = author;
        this.published = published;
    }

    @Override
    public String toString() {
        return "Book{" +
                "isbn='" + isbn + '\'' +
                ", title='" + title + '\'' +
                ", author='" + author + '\'' +
                ", published=" + published +
                ", numberOfSold=" + numberOfSold +
                '}';
    }

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public LocalDate getPublished() {
        return published;
    }

    public void setPublished(LocalDate published) {
        this.published = published;
    }

    public int getNumberOfSold() {
        return numberOfSold;
    }

    public void setNumberOfSold(int numberOfSold) {
        this.numberOfSold = numberOfSold;
    }
}

๊ทธ๋ฆฌ๊ณ  ํ˜น์‹œ ์ง๋ ฌํ™”๋ฅผ ์›ํ•˜์ง€ ์•Š๋Š” ํ•„๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด transient๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ง๋ ฌํ™” ํ•˜์ง€ ์•Š์„ ํ•„๋“œ๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

์ž๋ฐ” ์ง๋ ฌํ™”๋Š” java.io.ObjectOutputStream ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•œ๋‹ค. 

private void serialize(Book book) {
    try (ObjectOutput out = new ObjectOutputStream(new FileOutputStream("book.obj"))) {
        out.writeObject(book);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

 

์—ญ์ง๋ ฌํ™”๋ฅผ ํ•  ๋•Œ์—๋Š” ์ง๋ ฌํ™”๋œ ๊ฐ์ฒด์˜ ํด๋ž˜์Šค๊ฐ€ classpath์— ์กด์žฌํ•ด์•ผ ํ•˜๊ณ , import ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. 

์ž๋ฐ” ์—ญ์ง๋ ฌํ™”๋Š” java.io.ObjectInputStream ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•œ๋‹ค. 

private Book deserialize() {
    try (ObjectInput in = new ObjectInputStream(new FileInputStream("book.obj"))) {
        return (Book) in.readObject();
    } catch (IOException | ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}

 

 

public static void main(String[] args) {
    Book book = new Book("12345", "์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ” ์™„๋ฒฝ ๊ณต๋žต", "๋ฐฑ๊ธฐ์„ ", LocalDate.of(2023, 1, 11));
    book.setNumberOfSold(200);
    Book.name = "hyejin";

    SerializationExample example = new SerializationExample();
    example.serialize(book);
    Book.name = "whiteship";
    Book deserialize = example.deserialize();

    System.out.println("book = " + book);
    System.out.println("deserialize = " + deserialize);

}

์ด๋ ‡๊ฒŒ ์ง๋ ฌํ™”, ์—ญ์ง๋ ฌํ™”๋ฅผ ํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๋„ ์žˆ๋‹ค. 

 

์—ฌ๊ธฐ์„œ ์ •์  ๋ณ€์ˆ˜ static variable ๊ฐ’์ธ name์€ ์ง๋ ฌํ™”๋˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

์™œ๋ƒํ•˜๋ฉด ์ •์  ๋ณ€์ˆ˜๋Š” ์ €์žฅ๋˜์ง€ ์•Š๊ณ  ๊ฐ์ฒด๋ฅผ ์ง๋ ฌํ™” ํ•  ๋•Œ ํ˜„์žฌ ์ •์  ๋ณ€์ˆ˜์— ๋“ค์–ด์žˆ๋Š” ๊ฐ’์„ ๋ฐ›๊ฒŒ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ํ˜„์žฌ ์ •์  ๋ณ€์ˆ˜์— ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ๊ฐ’์„ ๋ฐ›๊ฒŒ ๋˜๋Š”๋ฐ ์œ„์— Book.name = "hyejin"์ด ์•„๋‹Œ ๋งˆ์ง€๋ง‰์— ์„ ์–ธํ•œ "whiteship"์„ ๋ฐ›๊ฒŒ ๋œ๋‹ค.  

๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ง๋ ฌํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋•Œ๋Š” ๋™์ ์œผ๋กœ ๋ฐ”๋€” ์ˆ˜ ์žˆ๋Š” ์ •์  ๋ณ€์ˆ˜์— ์˜์กดํ•˜์ง€ ์•Š๊ฒŒ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. 

 

 

 

SerialVersionUID๋ฅผ  ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

์œ„์— Book์—์„œ๋„ ๋ณด๋ฉด SerialZable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›๊ณ , serialVersionUID๋ฅผ ์„ ์–ธํ•ด์ค€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

์ด๋ฅผ ์„ ์–ธํ•ด์ฃผ์ง€ ์•Š์•„๋„ ์ž๋™์œผ๋กœ ์ž„์˜์˜ serialVersionUID๋ฅผ ์„ ์–ธํ•ด์ฃผ๋Š”๋ฐ, ์ด๋Š” ์‹คํ–‰ํ•  ๋•Œ ๋งˆ๋‹ค ๋žœ๋ค๊ฐ’์„ ํ• ๋‹นํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์—, 

๋งŒ์•ฝ ์ง๋ ฌํ™”๋ฅผ ํ•˜๊ณ , ์—ญ์ง๋ ฌํ™”๋ฅผ ํ•˜๋ ค๋Š”๋ฐ ๋งŒ์•ฝ ํด๋ž˜์Šค๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ๋‹ค๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. 

 

๊ทผ๋ฐ SerialVersionUID๋ฅผ ์„ ์–ธํ•ด์ฃผ๊ณ , ์ง์ ‘ ๊ด€๋ฆฌํ•ด์ฃผ๊ฒŒ ๋˜๋ฉด ํด๋ž˜์Šค์˜ ๋ณ€๊ฒฝ์ด ์žˆ๋”๋ผ๋„ ์—ญ์ง๋ ฌํ™”๋ฅผ ํ•  ๋•Œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค. 

ํด๋ž˜์Šค์˜ ๋ณ€๊ฒฝ์— ๋ฏผ๊ฐํ•˜์ง€ ์•Š๋‹ค๋ฉด ์ข€ ๋” ์œ ์—ฐํ•˜๊ฒŒ ๋Œ€์‘ํ•  ์ˆ˜ ์žˆ๋‹ค.  

 

 

 

๋‹ค์–‘ํ•œ ์ง๋ ฌํ™” ๋ฐฉ๋ฒ• 

: ์ง๋ ฌํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” csv, json, ์ง๋ ฌํ™” ๋“ฑ์˜ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. 

์ด ์ค‘์—์„œ๋Š” json์„ ์š”์ฆ˜์€ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค. 

์ง๋ ฌํ™” ๋ฐฉ๋ฒ•์€ ๋ชฉ์ ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๋Š”๋ฐ ์ž๋ฐ” ์ง๋ ฌํ™”๋Š” ์‚ฌ์‹ค ์ž๋ฐ” ์‹œ์Šคํ…œ์—์„œ ์ž๋ฐ” ์‹œ์Šคํ…œ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

728x90