스프링으로 개발을 할 때, 우리는 기본적으로 Service과 Repository 라는 컴포넌트를 만들고
Service에 @Autowired 라는 어노테이션을 통해 Repository를 주입시킵니다.
이러한 과정을 DI(Dependency Injection) 즉, 의존성 주입이라고 합니다.
다음과 같은 테스트 클래스를 작성해 확인해봅시다.
package com.example.demo;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class BookServiceTest {
@Autowired
BookService bookService;
@Test
public void di() {
// 스프링이 bookRepository를 생성하여, bookService에 DI 해줌
Assert.assertNotNull(bookService);
Assert.assertNotNull(bookService.bookRepository);
}
}
테스트가 정상 통과가 되고, bookService와 bookRepository가 null 이 아님을 알 수 있습니다.
그렇다면, 스프링은 DI를 어떻게 하는 것일까요?
리플렉션(Reflection)
1. 구체적인 클래스 타입을 알지 못해도 그 클래스의 메서드, 타입, 변수들을 접근할 수 있도록 해주는 자바 API이며,
2. 컴파일 시간이 아닌 실행 시간에 동적으로 특정 클래스의 정보를 추출해낼 수 있는 프로그래밍 기법입니다.
자바는 컴파일 시점에 타입을 결정하는 정적 언어인데, 동적으로 클래스를 사용해야 할 때 리플렉션이 사용됩니다.
즉, 작성 시점에는 어떤 클래스를 사용해야 할 지 모르지만 런타임 시점에서 클래스를 실행해야 할 경우에 사용됩니다.
대표적인 예시로, 스프링이 리플렉션을 이용하여 런타임 시에 개발자가 등록한 빈을 어플리케이션에서 가져와서 사용할 수 있도록 합니다.
- 스프링 프레임워크 : DI에 사용합니다.
- MVC : View에서 넘어오는 데이터를 객체에 바인딩할 때 사용합니다.
- Hibernate : @Entity 클래스에 setter가 없으면 해당 필드에 값을 바로 주입합니다.
- JUnit : ReflectionUtils 라는 클래스를 내부적으로 정의하여 사용합니다.
리플렉션 API 1 : 클래스 정보 조회
자바에서 리플렉션은 Class<?> 인스턴스로 접근이 가능합니다. (리플렉션이 제공하는 API입니다.)
https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html
아래의 예제를 통해, 리플렉션을 어떻게 사용하는지 알아보겠습니다.
public class Book {
private static String a = "aBOOK";
private static final String b = "bBOOK";
private String c = "cBook";
public String d = "dBOOK";
protected String e = "eBOOK";
public Book() {
}
public Book(String c) {
this.c = c;
}
public Book(String c, String d, String e) {
this.c = c;
this.d = d;
this.e = e;
}
private void privateMethod() {
System.out.println("privateMethod");
}
public void publicMethod() {
System.out.println("publicMethod");
}
public int sum(int l, int r) {
return l + r;
}
}
리플렉션을 사용하여, 클래스가 가지고 있는 '필드, 메서드, 생성자, 접근 지시자'와 같은 다양한 정보들에 접근할 수 있다.
public class Reflect {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
// 생성한 객체에 접근하려면, class가 필요하다. 클래스 인스턴스에 접근하는 방법은 3가지이다.
// 1. 타입을 통해 클래스 객체를 가져오는 방법
// 클래스 로딩이 끝나면, 클래스 타입의 인스턴스를 만들어서 heap에 저장하므로 인스턴스를 가져올 수 있음
Class<Book> bookClass = Book.class;
// 2. 인스턴스를 통해 클래스를 가져오는 방법
// 인스턴스가 이미 만들어져 있는 경우, getClass() 를 사용하여 가져올 수 있음
Book book = new Book();
Class<? extends Book> aClass = book.getClass();
// 3. 경로를 통해 클래스 객체를 가져오는 방법
// 클래스가 없다면? ClassNotFoundException 발생
Class.forName("hello.domain.Book");
////////////////////////////////////////////////////////////
//== 필드 목록 가져오기 ==//
System.out.println("----모든 필드 목록 가져오기----");
Arrays.stream(bookClass.getDeclaredFields()).forEach(System.out::println);
System.out.println();
System.out.println("----public 필드 목록 가져오기----");
Arrays.stream(bookClass.getFields()).forEach(System.out::println);
System.out.println();
System.out.println("----필드가 가지고 있는 값 목록 가져오기 (값을 가져올 때는 객체가 있어야함)----");
Arrays.stream(bookClass.getDeclaredFields()).forEach(f -> {
try {
f.setAccessible(true); // reflection으로 접근 지시자를 무시할 수 있다.
System.out.printf("%s %s \n", f, f.get(book));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
});
System.out.println();
// 메서드 목록 가져오기
System.out.println("----모든 메서드 목록 가져오기----");
// 직접 정의한 public 메서드 외에도, Object 로부터 상속받은 메서드도 출력됨
// private 메서드도 출력하려면 getDeclaredMethods()
Arrays.stream(bookClass.getMethods()).forEach(System.out::println);
System.out.println();
// 생성자 목록 가져오기
System.out.println("----모든 생성자 목록 가져오기----");
Arrays.stream(bookClass.getDeclaredConstructors()).forEach(System.out::println);
System.out.println();
// 접근 지시자 확인하기
System.out.println("----접근 지시자 확인하기----");
Arrays.stream(bookClass.getDeclaredFields()).forEach(f -> {
int modifiers = f.getModifiers();
System.out.print(f + "->");
System.out.print("private? " + Modifier.isPrivate(modifiers));
System.out.println(", static?" + Modifier.isStatic(modifiers));
});
}
}
출력 결과는 다음과 같습니다.
----모든 필드 목록 가져오기----
private static java.lang.String hello.domain.Book.a
private static final java.lang.String hello.domain.Book.b
private java.lang.String hello.domain.Book.c
public java.lang.String hello.domain.Book.d
protected java.lang.String hello.domain.Book.e
----public 필드 목록 가져오기----
public java.lang.String hello.domain.Book.d
----필드가 가지고 있는 값 목록 가져오기 (값을 가져올 때는 객체가 있어야함)----
private static java.lang.String hello.domain.Book.a aBOOK
private static final java.lang.String hello.domain.Book.b bBOOK
private java.lang.String hello.domain.Book.c cBook
public java.lang.String hello.domain.Book.d dBOOK
protected java.lang.String hello.domain.Book.e eBOOK
----모든 메서드 목록 가져오기----
public int hello.domain.Book.sum(int,int)
public void hello.domain.Book.publicMethod()
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
----모든 생성자 목록 가져오기----
public hello.domain.Book(java.lang.String,java.lang.String,java.lang.String)
public hello.domain.Book(java.lang.String)
public hello.domain.Book()
----접근 지시자 확인하기----
private static java.lang.String hello.domain.Book.a->private? true, static?true
private static final java.lang.String hello.domain.Book.b->private? true, static?true
private java.lang.String hello.domain.Book.c->private? true, static?false
public java.lang.String hello.domain.Book.d->private? false, static?false
protected java.lang.String hello.domain.Book.e->private? false, static?false
또한, 리플렉션으로 클래스가 가지고 있는 '부모 클래스, 인터페이스' 와 같은 다양한 정보들에도 접근할 수 있습니다.
public interface MyInterFace {
}
public interface BookInterFace {
}
public class MyBook extends Book implements MyInterFace, BookInterFace {
}
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Class<MyBook> myBookClass = MyBook.class;
System.out.println("----부모 클래스 목록 가져오기----");
System.out.println(myBookClass.getSuperclass());
System.out.println();
System.out.println("----모든 인터페이스 목록 가져오기----");
Arrays.stream(myBookClass.getInterfaces()).forEach(System.out::println);
System.out.println();
}
}
출력 결과는 다음과 같습니다.
----부모 클래스 목록 가져오기----
class hello.domain.Book
----모든 인터페이스 목록 가져오기----
interface hello.reflections.MyInterFace
interface hello.reflections.BookInterFace
Reflection과 Annotation
리플렉션으로 클래스가 가지고 있는 어노테이션 정보 또한 확인할 수 있습니다.
하지만 Book 클래스가 가지고 있는 어노테이션 정보를 확인하였을 때, 실제 결과는 아무것도 출력되지 않습니다.
public @interface MyAnnotation {
}
import hello.domain.Book;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
System.out.println("----모든 어노테이션 목록 가져오기----");
Arrays.stream(Book.class.getAnnotations()).forEach(System.out::println);
System.out.println();
}
}
출력 결과는 다음과 같습니다.
----모든 어노테이션 목록 가져오기----
어노테이션은 근본적으로 주석과 같은 취급을 받기 때문에, 어노테이션 정보가 클래스, 소스까지는 남지만 바이트 코드를 로딩할 때 메모리 상에 남지 않게 됩니다. (즉, 어노테이션 정보는 빼고 읽어오게 됩니다.)
@Retention
그래서 런타임까지 어노테이션 정보를 유지하고 싶다면, @Retention 어노테이션을 사용하면 됩니다.
// Retention의 기본값은 RetentionPolicy.CLASS
@Retention(RetentionPolicy.RUNTIME) // 런타임까지 어노테이션을 유지한다.
public @interface MyAnnotation {
}
아까의 Book 클래스에 어노테이션을 추가하고 확인한 결과는 다음과 같습니다.
----모든 어노테이션 목록 가져오기----
@hello.reflections.MyAnnotation()
@Target
어노테이션을 적용할 수 있는 범위를 설정할 수도 있습니다.
@Target({ElementType.TYPE, ElementType.FIELD}) // 어노테이션을 사용할 수 있는 곳은 클래스와 필드
@Retention(RetentionPolicy.RUNTIME) // 런타임까지 어노테이션을 유지한다.
public @interface MyAnnotation {
}
아래 예제에서 MyAnnotation을 생성자나 메서드에 적용하려고 하면, 에러가 발생합니다.
@Inherited
어노테이션을 하위 클래스까지 전달할 것인지, 상속 여부를 결정할 수도 있습니다.
아래 예제를 보면, Book 클래스는 MyAnnotation이 적용되어 있으며,
MyBook클래스는 BookAnnotation이 적용되어 있음과 동시에 Book 클래스를 상속받고 있습니다.
@Inherited // 상속이 가능하도록 한다
@Target({ElementType.TYPE, ElementType.FIELD}) // 어노테이션을 사용할 수 있는 곳은 클래스와 필드
@Retention(RetentionPolicy.RUNTIME) // 런타임까지 어노테이션을 유지한다.
public @interface MyAnnotation {
}
@Retention(RetentionPolicy.RUNTIME)
public @interface BookAnnotation {
}
@MyAnnotation
public class Book {
}
@BookAnnotation
public class MyBook extends Book implements MyInterFace, BookInterFace {
}
이 상태에서 MyBook 클래스의 어노테이션 목록을 출력하면, Book 클래스에 적용된 MyAnnotation도 같이 하위 클래스인 MyBook 클래스에 적용되어 출력되게 됩니다 . 부모 클래스가 아닌, 해당(자식) 클래스에 명시된 어노테이션 목록만 따로 출력할 수도 있습니다.
public class Main {
public static void main(String[] args) {
System.out.println("----모든 어노테이션 목록 가져오기----");
Arrays.stream(MyBook.class.getAnnotations()).forEach(System.out::println);
System.out.println();
System.out.println("----명시된 어노테이션 목록 가져오기----");
Arrays.stream(MyBook.class.getDeclaredAnnotations()).forEach(System.out::println);
System.out.println();
}
}
출력 결과는 다음과 같습니다.
----모든 어노테이션 목록 가져오기----
@hello.reflections.MyAnnotation()
@hello.reflections.BookAnnotation()
----명시된 어노테이션 목록 가져오기----
@hello.reflections.BookAnnotation()
리플렉션 API 2 : 클래스 정보 수정 및 실행
리플렉션으로 클래스의 정보를 가져오는 것 뿐만 아니라,
1. 생성자로 인스턴스를 만들 수 있고
2. 필드 값을 가져오거나 수정할 수 있으며
3. 메서드를 실행할 수 있다.
public class Main2 {
// Reflection 으로 클래스 정보 가져올 수 있다. 또한,
// 1. 생성자로 인스턴스를 만들 수 있다
// 2. 필드 값을 가져오거나 수정할 수 있다
// 3. 메서드를 실행할 수 있다.
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException,
InvocationTargetException, InstantiationException, NoSuchFieldException, IllegalAccessException {
Class<?> bookClass = Class.forName("hello.domain.Book");
// 1. 생성자를 사용하여 인스턴스 생성하기
Constructor<?> constructor = bookClass.getConstructor(null); // 기본 생성자 사용하여 인스턴스 만들겠다고 선언
Book book = (Book) constructor.newInstance();
System.out.println(book);
// 2. static 필드 값 가져오기 Field.get(object)
Field a = Book.class.getDeclaredField("a");
a.setAccessible(true); // private Scope. 접근 지시자 무시하도록 설정
System.out.println(a.get(null)); // static 변수이므로, 특정 인스턴스를 넘겨줄 것이 없다. 그러므로 null을 넘긴다.
// 2. static 필드 값 수정하기 Field.set(object, value)
a.set(null, "abook");
System.out.println(a.get(null));
// 2. 필드 값 가져오기 Field.get(object)
// 특정 인스턴스가 가지고 있는 값을 가져오는 것이기 때문에, 인스턴스가 미리 만들어져 있어야한다.
Field d = Book.class.getDeclaredField("d");
System.out.println(d.get(book)); // 미리 만들어져 있던 Book 클래스 객체(book)을 사용하여 해당 객체의 필드 값을 가져옴.
// 2. 필드 값 수정하기
d.set(book, "dbook");
System.out.println(d.get(book));
// 3. 메서드 실행하기 Method.invoke(object, params)
Method privateMethod = Book.class.getDeclaredMethod("privateMethod");
privateMethod.setAccessible(true);
privateMethod.invoke(book);
// 3. 파라미터와 리턴 값이 있는 메서드 실행하기 Method.invoke(object, params)
Method sum = Book.class.getDeclaredMethod("sum", int.class, int.class);
int result = (int) sum.invoke(book, 10, 20);
System.out.println(result);
}
}
리플렉션을 사용하여 DI 프레임워크 만들기
목표 : @Inject 어노테이션을 만들어서 필드 주입해주는 컨테이너 서비스 만들기
1. Inject.java
package me.reflection.di;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Inject {
}
어노테이션은 @Retention을 RUNTIME으로 설정해줘야 합니다.
2. ContainerService.java
package me.reflection.di;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
public class ContainerService {
public static <T> T getObject(Class<T> classType) {
T instance = createInstance(classType);
Arrays.stream(classType.getDeclaredFields()).forEach(f -> {
if (f.getAnnotation(Inject.class) != null) {
Object fieldInstance = createInstance(f.getType());
f.setAccessible(true);
try {
f.set(instance, fieldInstance);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
});
return instance;
}
private static <T> T createInstance(Class<T> classType) {
try {
return classType.getConstructor(null).newInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { // 멀티 catch 블럭
throw new RuntimeException(e);
}
}
}
ContainerService에서 getObject 메서드는 클래스타입을 인자값으로 받아 instance를 생성 후 그 안에 필드들을 순회하며 getAnnotation()으로 Inject.class 가 있는지 탐색 후 탐색이 될 경우 해당 필드에 인스턴스를 생성해 주입시킵니다.
3. 해당 프로젝트 install
mvn install 명령어로 ContainerService를 설치해줍니다.
4. 생성한 라이브러리 사용 - 신규 프로젝트 생성
- 테스트용 Service, Repository 구현하기
/* AccountRepository */
public class AccountRepository {
public void save() {
System.out.println("Repo.save");
}
}
/* AccountService */
public class AccountService {
@Inject
AccountRepository accountRepository;
public void join() {
System.out.println("Service.join");
accountRepository.save();
}
}
- 사용하기
public class Test {
public static void main(String[] args) {
AccountService accountService = ContainerService.getObject(AccountService.class);
accountService.join();
}
// 실행결과
// Service.join
// Repo.save
}
리플렉션 정리
1. 리플렉션 사용시 주의점
- 지나친 사용은 성능 이슈를 야기할 수 있기에 반드시 필요할 때만 사용해야 합니다.
- 컴파일 타임에 확인되지 않고 런타임 시에만 발생하는 문제를 만들 가능성이 있습니다.
- 접근 지시자를 무시할 수 있습니다.
2. 스프링에서의 활용
- 의존성 주입(DI)
- MVC 뷰에서 넘어온 데이터를 객체에 바인딩 할 때 사용됩니다.
3. 하이버네이트
- @Entity 클래스에 Setter가 없다면 리플렉션을 사용합니다.
4. JUnit
- ReflectionUtils 라는 클래스를 내부적으로 정의하여 사용합니다.
'Java' 카테고리의 다른 글
좋은 객체 지향 설계의 5가지 원칙(SOLID) (0) | 2023.02.15 |
---|---|
static 살짝 알아보기 (2) | 2023.02.04 |
abstract class (0) | 2022.07.15 |
Features of Java (0) | 2022.07.04 |
JVM (0) | 2022.07.02 |