POJO JAVA / IoC / DI


POJO JAVA


  • Plain Old Java Object

  • POJO์˜ ํŠน์ง•

    • ํŠน์ • ๊ทœ์•ฝ, ํŠน์ • ํ™˜๊ฒฝ์— ์ข…์†๋˜์ง€ ์•Š์Œ

    • ํŠน์ • ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‚˜ ๋ชจ๋“ˆ์—์„œ ์ •์˜๋œ ํด๋ž˜์Šค๋ฅผ ์ƒ์† ๋ฐ›์•„์„œ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์Œ

    • POJO๊ฐ€ ๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์™ธ๋ถ€์˜ ์˜์กด์„ฑ์„ ๋‘์ง€ ์•Š๊ณ  ์ˆœ์ˆ˜ํ•œ ์ž๋ฐ”๋กœ ๊ตฌ์„ฑ์ด ๊ฐ€๋Šฅํ•ด์•ผ ํ•จ

    • ํŠน์ • ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„์— ์™ธ๋ถ€ ์ข…์†์ ์ธ http request, session ๋“ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด pojo๋ฅผ ์œ„๋ฐฐํ•˜๋Š”๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผ

    • @Annotation ๊ธฐ๋ฐ˜ ์„ค์ •๋„ ๋งŽ์ด ์‚ฌ์šฉ๋˜๊ณ ๋Š” ์žˆ์ง€๋งŒ ์—„์—ฐํžˆ ๋”ฐ์ง€๋ฉด POJO๋ฅผ ์œ„๋ฐ˜ํ–ˆ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Œ

  • Spring, Hibernate ๋‘ ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ๊ฐ์ฒด์ง€ํ–ฅ์ ์ธ ์„ค๊ณ„๋ฅผ ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, POJO๋ฅผ ์ง€ํ–ฅํ•จ



IoC


  • Inversion of Control (์ œ์–ด์˜ ์—ญ์ „)

  • ์Šคํ”„๋ง์—์„œ๋Š” ๊ฐ์ฒด๋ฅผ new๋กœ ์ƒ์„ฑํ•˜์—ฌ ๊ฐœ๋ฐœ์ž๊ฐ€ ๊ด€๋ฆฌ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ Spring Container์— ๋ชจ๋‘ ๋งก๊ธฐ๋Š”๋ฐ, ๊ฐœ๋ฐœ์ž์—์„œ ํ”„๋ ˆ์ž„์›Œํฌ๋กœ ์ œ์–ด์˜ ๊ฐ์ฒด ๊ด€๋ฆฌ ๊ถŒํ•œ์ด ๋„˜์–ด๊ฐ”์œผ๋ฏ€๋กœ ์ œ์–ด์˜ ์—ญ์ „(=์ œ์–ด์˜ ํ๋ฆ„์„ ๋ฐ”๊พผ๋‹ค)์ด๋ผ๊ณ  ํ•œ๋‹ค.



DI


  • Dependency Injection, (์˜์กด ๊ด€๊ณ„ ์ฃผ์ž…)

  • Field Injection / Setter Injection / Constructor Injection

  • ๊ฐ์ฒด(Bean)๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ์™ธ๋ถ€์—์„œ ์ƒ์„ฑํ•œ ํ›„ ์ฃผ์ž…์‹œํ‚ค๋Š”(setter๋‚˜ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด) ๋ฐฉ์‹

  • DI์˜ ์žฅ์ 

    • ๋ชจ๋“ˆ๊ฐ„์˜ ๊ฒฐํ•ฉ๋„๊ฐ€ ๋‚ฎ์•„์ง€๊ณ  ์œ ์—ฐ์„ฑ์ด ๋†’์•„์ง

    • ์˜์กด์„ฑ(ํ•œ ๊ฐ์ฒด๊ฐ€ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ)์ด ์ค„์–ด๋“ฆ : ์ฃผ์ž… ๋ฐ›๋Š” ๋Œ€์ƒ์ด ๋ณ€ํ•˜๋”๋ผ๋„ ๊ตฌํ˜„ ์ž์ฒด๋ฅผ ์ˆ˜์ •ํ•  ์ผ์ด ์—†๊ฑฐ๋‚˜ ์ค„์–ด๋“ฆ

    • ๊ฐ€๋…์„ฑ์ด ๋†’์•„์ง€๊ณ , ์žฌ์‚ฌ์šฉ์„ฑ์ด ๋†’์•„์ง€๋ฉฐ, ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์šฉ์ดํ•ด์ง

    • ์ถ”์ƒํ™” : ์ฝ”๋“œ๋ฅผ ํ™•์žฅํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•  ๋•Œ ์˜ํ–ฅ์„ ์ตœ์†Œํ™”

    • ์ˆœํ™˜์ฐธ์กฐ(์ฐธ์กฐํ•˜๋Š” ๋Œ€์ƒ์ด ์„œ๋กœ ๋ฌผ๋ ค ์žˆ์–ด์„œ ์ฐธ์กฐํ•  ์ˆ˜ ์—†๊ฒŒ ๋˜๋Š” ํ˜„์ƒ)๋ฅผ ๋ง‰์„ ์ˆ˜ ์žˆ์Œ



DI ์˜ˆ์ œ


  • Encoder.java
package com.company.di;

import java.util.Base64;

public class Encoder {

    public String encode(String message){

        return Base64.getEncoder().encodeToString(message.getBytes());

    }
}
  • UrlEncoder.java
package com.company.di;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class UrlEncoder {

    public String encode(String message){
        try {
            return URLEncoder.encode(message, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }

    }
}
  • Main
package com.company.di;

public class Main {

    public static void main(String[] args) {
        String url = "www.naver.com/books/it?page=10&size=20&name=spring-boot";

        // Base 64 encoding
        Encoder encoder = new Encoder();
        String result = encoder.encode(url);
        System.out.println(result);
        // ์ถœ๋ ฅ: d3d3Lm5hdmVyLmNvbS9ib29rcy9pdD9wYWdlPTEwJnNpemU9MjAmbmFtZT1zcHJpbmctYm9vdA==

        // URL encoding
        UrlEncoder urlEncoder = new UrlEncoder();
        String urlResult = urlEncoder.encode(url);
        System.out.println(urlResult);
        // ์ถœ๋ ฅ: www.naver.com%2Fbooks%2Fit%3Fpage%3D10%26size%3D20%26name%3Dspring-boot

    }
}


๋˜ ๋‹ค๋ฅธ ์ธ์ฝ”๋”ฉ์„ ํ•ด์•ผ ํ•˜๋ฉด ํด๋ž˜์Šค๋ฅผ ๋˜ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
-> ๋ถ„๋ฆฌ์‹œํ‚ค๊ธฐ ์œ„ํ•œ ์ถ”์ƒํ™”๋ฅผ ํ•ด ๋ณด์ž
-> Encoder์™€ UrlEncoder๋ฅผ ๋ณด๋ฉด ๋‘˜์ด ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค => ๊ทธ ๋ถ€๋ถ„์„ ๋ฝ‘์•„ iEncoder๋ผ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž.


  • IEncoder.java
package com.company.di;

public interface IEncoder {
    String encode(String message);
    // > ๋‹ค๋ฅธ์• ๋“ค์€ ์ด ์žŒํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„๋งŒ ํ•˜๋ฉด ๋จ!

}
  • UrlEncoder.java
package com.company.di;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class UrlEncoder implements IEncoder{

    public String encode(String message){
        try {
            return URLEncoder.encode(message, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }
}
  • Base64Encoder.java
package com.company.di;

import java.util.Base64;

public class Base64Encoder implements IEncoder{


    public String encode(String message){

        return Base64.getEncoder().encodeToString(message.getBytes());
        //return iEncoder.encode(message);
    }
}
  • Main.java
package com.company.di;

public class Main {

    public static void main(String[] args) {
        String url = "www.naver.com/books/it?page=10&size=20&name=spring-boot";

        // Base 64 encoding
        IEncoder encoder = new Base64Encoder();
        String result = encoder.encode(url);
        System.out.println(result);
        // ์ถœ๋ ฅ: d3d3Lm5hdmVyLmNvbS9ib29rcy9pdD9wYWdlPTEwJnNpemU9MjAmbmFtZT1zcHJpbmctYm9vdA==

        // URL encoding
        IEncoder urlEncoder = new UrlEncoder();
        String urlResult = urlEncoder.encode(url);
        System.out.println(urlResult);
        // ์ถœ๋ ฅ: www.naver.com%2Fbooks%2Fit%3Fpage%3D10%26size%3D20%26name%3Dspring-boot
    }
}


=>

  • Encoder.java
package com.company.di;

import java.util.Base64;

public class Encoder {

    private IEncoder iEncoder;
    public Encoder(){
        this.iEncoder = new Base64Encoder();
    }

    public String encode(String message){

        return iEncoder.encode(message);
    }
}
  • Main.java
package com.company.di;

public class Main {

    public static void main(String[] args) {
        String url = "www.naver.com/books/it?page=10&size=20&name=spring-boot";

        // Base 64 encoding
        Encoder encoder = new Encoder();
        String result = encoder.encode(url);
        System.out.println(result);
        // ์ถœ๋ ฅ: d3d3Lm5hdmVyLmNvbS9ib29rcy9pdD9wYWdlPTEwJnNpemU9MjAmbmFtZT1zcHJpbmctYm9vdA==

    }
}


์ด ์ƒํƒœ์—์„œ ๋‹ค์‹œ url ์ธ์ฝ”๋”ฉ์„ ํ•ด์•ผ ํ•œ๋‹ค๋ฉด Encoder.java ํŒŒ์ผ์„

private IEncoder iEncoder;
    public Encoder(){
        this.iEncoder = new UrlEncoder();
    }
    ...

๋กœ ์ˆ˜์ •ํ•ด์•ผ ํ•˜๊ณ , ๋˜ ๋‹ค์‹œ base64 ์ธ์ฝ”๋”ฉ์„ ํ•˜๋ ค๋ฉด ๋˜ ์ˆ˜์ •์„ ํ•ด์•ผํ•œ๋‹ค.
์ด๋Š” ๊ต‰์žฅํžˆ ๋น„ํšจ์œจ์ ์ด๋ฉฐ ๋ณธ์งˆ์˜ ํด๋ž˜์Šค๋ฅผ ๊ฑด๋“œ๋ฆฌ๋Š” ์ผ!
-> โ€˜DIโ€™ ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด Encoder.java์˜ ์ฝ”๋“œ๋ฅผ ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š์•„๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋จ


=> !DI๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์ž!


Encoder๋Š” ๊ฑด๋“œ๋ฆด ํ•„์š” ์—†์ด ๋„˜๊ฒจ์ฃผ๋Š” ์ฃผ์ž… ๊ฐ์ฒด๋งŒ ๋ณ€๊ฒฝ

Encoder encoder = new Encoder(new Base64Encoder());
// or
Encoder encoder = new Encoder(new UrlEncoder());
  • Encoder.java
package com.company.di;

import java.util.Base64;

public class Encoder {

   //  di
    private IEncoder iEncoder;

    public Encoder(IEncoder iEncoder){
        this.iEncoder = iEncoder;
    }

    public String encode(String message){

        //return Base64.getEncoder().encodeToString(message.getBytes());
        return iEncoder.encode(message);
    }
}
  • Main.java
package com.company.di;

public class Main {

    public static void main(String[] args) {
        String url = "www.naver.com/books/it?page=10&size=20&name=spring-boot";

        // Base 64 encoding
        Encoder encoder = new Encoder(new Base64Encoder());
        String result = encoder.encode(url);
        System.out.println(result);
        // ์ถœ๋ ฅ: d3d3Lm5hdmVyLmNvbS9ib29rcy9pdD9wYWdlPTEwJnNpemU9MjAmbmFtZT1zcHJpbmctYm9vdA==

    }
}

Categories:

Spring