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==
}
}