โ ๋ฆฌํ๋ ์ ๊ณผ ๊ฐ์ฒด ์งํฅ
โ๏ธ ์์:
Class.forName("ํด๋์ค๋ช
") โ ์กด์ฌํ์ง ์๋ ํด๋์ค๋ ์ปดํ์ผ ํ์์ ํ์ธ ๋ถ๊ฐmethod.invoke() โ ์๋ชป๋ ์ธ์ ์ ๋ฌ ์ ์ปดํ์ผ๋ฌ๊ฐ ์ค๋ฅ๋ฅผ ์ก์ง ๋ชปํจ๐ ๋ฆฌํ๋ ์ ์ ๋จ์ฉ์ ์์กด์ฑ ์ฆ๊ฐ์ ์ฝ๋ ๋๋ ํ๋ฅผ ์ด๋ํ ์ ์์ต๋๋ค.
๐ค ๊ทธ๋ผ์๋ ๋ถ๊ตฌํ๊ณ ๋ฆฌํ๋ ์ ์ด ํ์ํ ์ด์
โ AOP (Aspect-Oriented Programming)
๋ถ๊ฐ ๋ก์ง์ ์ฝ์
ํด์ผ ํฉ๋๋ค.โ DI (Dependency Injection), ORM ๋ฑ์์์ ์ด๋ ธํ ์ด์ ๋ถ์
@Autowired, @Entity ๋ฑ์ ์ด๋
ธํ
์ด์
์ ๊ธฐ๋ฐ์ผ๋ก ํด๋์ค ๊ตฌ์กฐ๋ฅผ ๋ถ์ํ๊ณ โ ํ๋ฌ๊ทธ์ธ ๊ตฌ์กฐ, ๋ฐํ์ ๊ฐ์ฒด ์กฐ๋ฆฝ ๋ฑ ์ ์ฐํ ์ค๊ณ์๋ ์ ์ฉ
๐ก ๊ฒฐ๋ก
ํ๋ ์์ํฌ์ ์ํด ๊ฐ์ ์ ์ผ๋ก ํ์ฉ๋๋๋ก ์ค๊ณํ๋ ๊ฒ์ด ๊ฐ์ฅ ์ด์์ ์
๋๋ค.๐ ๋ฆฌํ๋ ์ ์ฃผ์ API ์์ฝ
| ๋ฉ์๋ | ์ค๋ช |
|---|---|
| getName() | ํด๋์ค ์ด๋ฆ ๋ฐํ |
| getDeclaredFields() | ๋ชจ๋ ํ๋ ๋ฐฐ์ด ๋ฐํ |
| getDeclaredMethods() | ๋ชจ๋ ๋ฉ์๋ ๋ฐฐ์ด ๋ฐํ |
| getDeclaredConstructor() | ๊ธฐ๋ณธ ์์ฑ์ ๋ฐํ |
| newInstance() | ๊ฐ์ฒด ์ธ์คํด์ค ์์ฑ |
| setAccessible(true) | private ๋ฉค๋ฒ ์ ๊ทผ ํ์ฉ |
โ ์ปค์คํ ์ด๋ ธํ ์ด์ ์ด๋?
โ @interface ์ ์
@interface ํค์๋๋ฅผ ์ฌ์ฉํด ์ ์ํฉ๋๋ค.์์ฑ์ ์๋ฏธํฉ๋๋ค.default ํค์๋๋ก ์ค์ ํ ์ ์์ต๋๋ค.import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@interface CustomInfo {
String author();
String date();
int version() default 1;
}
โ @Target ์ค์
@Target์ ์ด๋
ธํ
์ด์
์ด ์ ์ฉ๋ ์์น(๋์)๋ฅผ ์ง์ ํฉ๋๋ค.ElementType.TYPE์ ํด๋์ค, ์ธํฐํ์ด์ค, ์ด๊ฑฐํ ๋ฑ์ ์ ์ฉ ๊ฐ๋ฅํ๊ณ ,ElementType.METHOD๋ ๋ฉ์๋์ ์ ์ฉ ๊ฐ๋ฅํฉ๋๋ค.@Target({ElementType.TYPE, ElementType.METHOD})
โ @Retention ์ค์
@Retention์ ์ด๋
ธํ
์ด์
์ ์ ์ง ๋ฒ์๋ฅผ ์ง์ ํ๋ ๋ฉํ ์ด๋
ธํ
์ด์
์
๋๋ค.RUNTIME์ ์ค์ ํ๋ฉด ์คํ ์ค์๋ ์ด๋
ธํ
์ด์
์ ๋ณด๋ฅผ ๋ฆฌํ๋ ์
์ผ๋ก ์ฝ์ ์ ์์ต๋๋ค.SOURCE, CLASS, RUNTIME 3๊ฐ์ง ์ ์ฑ
์ด ์์ผ๋ฉฐ, ๊ฐ๊ฐ ์ ์ง ์์ ์ด ๋ค๋ฆ
๋๋ค.@Retention(RetentionPolicy.RUNTIME)
โน๏ธ retension meaning
= hold
โ ํด๋์ค์ ์ ์ฉ
@CustomInfo๋ฅผ ์ฌ์ฉํด ๋ฉํ ์ ๋ณด๋ฅผ ์ง์ ํฉ๋๋ค.author, date, version ์์ ๊ฐ์ ๋ชจ๋ ๋ช
์ํฉ๋๋ค.@CustomInfo(author = "John Doe", date = "2025-03-25", version = 2)
class Demo {
// ํด๋์ค ๋ด๋ถ ๋ก์ง ์๋ต
}
โ ๋ฉ์๋์ ์ ์ฉ
version์ ๊ธฐ๋ณธ๊ฐ์ ์ฌ์ฉํ๊ณ , author์ date๋ง ์ง์ ํฉ๋๋ค.@CustomInfo(author = "Jane Smith", date = "2025-03-26")
public void display() {
System.out.println("Display method executed.");
}
โ๏ธ ์ ์ฒด์ฝ๋
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@interface CustomInfo {
String author();
String date();
int version() default 1; // ๋ฉ์๋๊ฐ ์๋๋ผ ํ๋ ๊ฐ์
}
@CustomInfo(author = "j797", date = "2025-06-24", version = 2)
class Demo {
@CustomInfo(author = "j797", date = "2025-06-24")
public void display() {
System.out.println("Display method executed");
}
}
โ ๋ฆฌํ๋ ์ ์ผ๋ก ์ด๋ ธํ ์ด์ ์ฝ๊ธฐ
isAnnotationPresent()๋ก ์ด๋
ธํ
์ด์
์กด์ฌ ์ฌ๋ถ๋ฅผ ํ์ธํ๊ณ ,getAnnotation()์ผ๋ก ์ค์ ์ด๋
ธํ
์ด์
๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.Class<?> demoClass = demo.getClass();
if (demoClass.isAnnotationPresent(CustomInfo.class)) {
CustomInfo info = demoClass.getAnnotation(CustomInfo.class);
System.out.println("์์ฑ์: " + info.author());
System.out.println("์์ฑ์ผ: " + info.date());
System.out.println("๋ฒ์ : " + info.version());
}
isAnnotationPresent() ์คํํ ๋
โ ์คํธ๋ฆผ I/O ๊ฐ์
โ writeFile ๋ฉ์๋ ๊ตฌํ
try-with-resources ๊ตฌ๋ฌธ์ผ๋ก ์๋์ผ๋ก ํ์ผ์ ๋ซ๋๋ก ์ฒ๋ฆฌํฉ๋๋ค.public static void writeFile(String filename, String content) {
try (FileWriter writer = new FileWriter(filename)) {
writer.write(content);
} catch (IOException e) {
e.printStackTrace();
}
}
โ FileWriter ์ฌ์ฉ๋ฒ
public static void main(String[] args) {
writeFile("example.txt", "Hello, Java I/O!\nThis is a test.");
System.out.println("ํ์ผ ์์ฑ ์๋ฃ");
}
โน๏ธ try-with-resources
finally ์ญํ ์ ์ํํ๋ ๊ตฌ๋ฌธ.
๐ค ๋ง์ฝ try-with-resources ์์ด ์์ฑํด์ผ ํ๋ค๋ฉด
try-catch ๊ตฌ๋ฌธ ์์์ FileWriter๋ก ์์ฑํ ๋ค์ ์์ธ์ฒ๋ฆฌ๋ฅผ ํด์ฃผ๊ณ , ํด์ ๋ฅผ ์ํด finally ๊ตฌ๋ฌธ์์ ๋ค์ try-catch ๊ตฌ๋ฌธ์ ์ด์ด FileWriter๋ฅผ ํด์ ํ๊ณ ์์ธ์ฒ๋ฆฌ๋ฅผ ํด์ฃผ์ด์ผ ํ๋ค.
public static void writeFile(String filename, String content) {
FileWriter writer = null;
try {
writer = new FileWriter(filename);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
โ
try-with-resources ๊ตฌ๋ฌธ์ผ๋ก ๋ฐ๊พธ๊ธฐ
try ์์ ๊ดํธ๋ฅผ ์ด์ด writeFile ๋ฉ์๋๋ฅผ ๊ตฌํํ๋ฉด FileWriter๋ฅผ ์ฌ์ฉํด ํ
์คํธ๋ฅผ ์ฐ๊ณ ๋์ ์๋์ผ๋ก ํ์ผ์ ๋ซ๋๋ก ์ฒ๋ฆฌํ์ฌ finally ์ดํ์ ๋ด์ฉ์ ์๋ตํ ์ ์์ต๋๋ค.
public static void writeFile(String filename, String content) {
try (FileWriter writer = new FileWriter(filename)) {
writer.write(content);
} catch (IOException e) {
e.printStackTrace();
}
}
โ
append ์ด์ด์ฐ๊ธฐ ๋ชจ๋
FileWriter ๋ฅผ ์์ฑํ ๋ ํ์ผ์ด๋ฆ ๋ค์ true๋ฅผ ๋ฃ์ผ๋ฉด append ๋ชจ๋๊ฐ ํ์ฑํ๋์ด ์ด์ ์ ์ผ๋ ๋ด์ฉ์ ๋ฎ์ด์ฐ๊ธฐ ํ์ง ์๊ณ ์ถ๊ฐ๋ก ๋ด์ฉ์ ์ด์ด์ธ ์ ์๋ค.
try (FileWriter writer = new FileWriter(filename, true)) {
writer.write(content);
} catch (IOException e) {
e.printStackTrace();
}
โ readFile ๋ฉ์๋ ๊ตฌํ
public static void readFile(String filename) {
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
โ FileReader & BufferedReader ์ฌ์ฉ๋ฒ
๋ฌธ์ ๋จ์(char)๋ก ์ฝ๋ ์คํธ๋ฆผ์
๋๋ค.โ ํ์ผ ์ฝ๊ธฐ ์คํ
public static void main(String[] args) {
readFile("example.txt");
}
โ๏ธ ํตํฉ ์ฝ๋
import java.io.*;
public class Main {
public static void writeFile(String filename, String content) {
try (FileWriter writer = new FileWriter(filename, true)) {
writer.write(content);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void readFile(String filename) {
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String Line;
while ((Line = reader.readLine()) != null) {
System.out.println(Line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
writeFile("lunch-menu.txt", "lunch-menu");
writeFile("lunch-menu.txt", "๋ก๋ณถ์ด");
System.out.println("ํ์ผ ์์ฑ ์๋ฃ");
readFile("lunch-menu.txt");
}
}
โถ๏ธ
ํ์ผ ์์ฑ ์๋ฃ
lunch-menu๋ก๋ณถ์ด
โ NIO ๊ฐ์
โ ์ด๊ธฐํ ๋ฐ import
import java.nio.file.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
// NIO ์ค์ต ์ค๋น
}
}
โ writeFile ๋ฉ์๋ ์์ฑ
public static void writeFile() {
Path filePath = Paths.get("example.txt");
String content = "Hello, NIO World!";
// ์ฑ๋๊ณผ ๋ฒํผ๋ก ์ฐ๊ธฐ ๋ก์ง ์ถ๊ฐ
}
โ Channel ์ด๊ธฐ & Buffer ์ค๋น
try (FileChannel writeChannel = FileChannel.open(
filePath,
StandardOpenOption.CREATE,
StandardOpenOption.WRITE)) {
ByteBuffer buffer = ByteBuffer.allocate(1024);
// ๋ฒํผ์ ๋ฐ์ดํฐ ์ฐ๊ธฐ
}
โ ๋ฒํผ์ ๋ฐ์ดํฐ ์ฐ๊ธฐ
buffer.put(content.getBytes());
buffer.flip();
writeChannel.write(buffer);
System.out.println("File written successfully.");
โ readFile ๋ฉ์๋ ์์ฑ
public static void readFile() {
Path filePath = Paths.get("example.txt");
try (FileChannel readChannel = FileChannel.open(
filePath,
StandardOpenOption.READ)) {
ByteBuffer buffer = ByteBuffer.allocate(1024);
// ์ฝ๊ธฐ ๋ก์ง ์ถ๊ฐ ์์
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
โ ๋ฒํผ๋ฅผ ์ด์ฉํ ํ์ผ ์ฝ๊ธฐ
read()๋ก ํ์ผ ๋ด์ฉ์ ByteBuffer์ ์ฝ์ต๋๋ค.flip()์ผ๋ก ์ฝ๊ธฐ ๋ชจ๋๋ก ์ ํํ ๋ค, hasRemaining()๊ณผ get()์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ฌธ์๋ก ์ถ๋ ฅํฉ๋๋ค.clear()๋ก ๋ฒํผ๋ฅผ ์ด๊ธฐํํ ํ ๋ค์ read()๋ฅผ ๋ฐ๋ณตํฉ๋๋ค.int bytesRead = readChannel.read(buffer);
while (bytesRead != -1) {
buffer.flip();
while (buffer.hasRemaining()) {
System.out.print((char) buffer.get());
}
buffer.clear();
bytesRead = readChannel.read(buffer);
}
System.out.println("\nFile read successfully.");
โ๏ธ ํตํฉ ์ฝ๋
public class Main {
public static void writeFile(String filename, String content) {
Path filePath = Paths.get(filename);
System.out.println(filePath);
// try (FileWriter fileWriter = new FileWriter(filePath.toFile())) {
// fileWriter.write(content);
// } catch (IOException e) {
// e.printStackTrace();
// }
try (FileChannel writeChannel = FileChannel.open(filePath, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
ByteBuffer buffer = ByteBuffer.allocate(1024); //1024byte = 1kb
// BUFFER READ MODE
buffer.put(content.getBytes()); // ํ
์คํธ๋ฅผ .getBytes()๋ฅผ ํตํด buffer์ byte ๋จ์๋ก ๋ฃ๋๋ค.
System.out.println("Byte content: " + Arrays.toString(content.getBytes()));
// FILE WRITE MODE
buffer.flip(); // CREATE -> WRITE mode
writeChannel.write(buffer); // buffer ์ ๋ด์ฉ์ file์ write
System.out.println("ํ์ผ์ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ ์๋ฃํ์ต๋๋ค.");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void readFile(String filename) {
Path filePath = Paths.get(filename);
try (FileChannel readChannel = FileChannel.open(filePath, StandardOpenOption.READ)) {
ByteBuffer buffer = ByteBuffer.allocate(1024);
int bytesRead = readChannel.read(buffer);
while (bytesRead != -1) {
buffer.flip();
String chunk = StandardCharsets.UTF_8.decode(buffer).toString();
System.out.print(chunk);
buffer.clear();
bytesRead = readChannel.read(buffer);
// while (buffer.hasRemaining()) {
// System.out.println((char) buffer.get());
// }
// buffer.clear();
// bytesRead = readChannel.read(buffer);
// System.out.println(bytesRead);
}
System.out.println("\nํ์ผ ์ฝ๊ธฐ๊ฐ ์๋ฃ๋์์ต๋๋ค.");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// writeFile("lunch-menu.txt", "์ค๋์ ์ ์ฌ ๋ฉ๋ด๋ ๋ญ๊น์?");
readFile("lunch-menu.txt");
}
}
โ๏ธ ์คํธ๋ฆผ I/O vs NIO ๋น๊ต
| ๊ตฌ๋ถ | ์คํธ๋ฆผ I/O | NIO (New I/O) |
|---|---|---|
| ์ฒ๋ฆฌ ๋จ์ | ๋ฐ์ดํธ ๋๋ ๋ฌธ์ ๋จ์๋ก ์์ฐจ ์ฒ๋ฆฌ | ๋ฒํผ(Buffer)๋ฅผ ์ฌ์ฉํ ๋ธ๋ก ๋จ์ ์ฒ๋ฆฌ |
| ์ฑ๋ฅ | ๊ฐ๋จํ ์ ์ถ๋ ฅ์ ์ ํฉ | ๋์ฉ๋ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ๋ฐ ๋ฉํฐ I/O ์ํฉ์ ์ ๋ฆฌ |
| ์ฃผ์ API | FileInputStream, FileOutputStream, FileReader, FileWriter | Path, FileChannel, ByteBuffer ๋ฑ |