Skip to content

Commit 42a6b58

Browse files
committed
✨ Add: code for dependency injection framework
1 parent 16cf1d7 commit 42a6b58

File tree

7 files changed

+112
-0
lines changed

7 files changed

+112
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.github.imsejin;
2+
3+
@FunctionalInterface
4+
public interface MyInterface {
5+
6+
String sell();
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.github.imsejin.annotation;
2+
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.RetentionPolicy;
5+
6+
@Retention(RetentionPolicy.RUNTIME)
7+
public @interface Inject {
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.github.imsejin.model;
2+
3+
import io.github.imsejin.MyInterface;
4+
5+
public class MyBook extends Book implements MyInterface {
6+
7+
@Override
8+
public String sell() {
9+
return "Sale this book";
10+
}
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.github.imsejin.service;
2+
3+
import io.github.imsejin.annotation.Inject;
4+
5+
import java.lang.reflect.Field;
6+
import java.lang.reflect.InvocationTargetException;
7+
8+
public class ContainerService {
9+
10+
public static <T> T getObject(Class<T> type) {
11+
T rootInstant = instantiate(type);
12+
for (Field field : type.getDeclaredFields()) {
13+
Inject annotation = field.getAnnotation(Inject.class);
14+
if (annotation == null) continue;
15+
16+
Object childInstant = instantiate(field.getType());
17+
field.setAccessible(true);
18+
try {
19+
field.set(rootInstant, childInstant);
20+
} catch (IllegalAccessException e) {
21+
throw new RuntimeException(e);
22+
}
23+
}
24+
25+
return rootInstant;
26+
}
27+
28+
private static <T> T instantiate(Class<T> type) {
29+
try {
30+
return type.getConstructor().newInstance();
31+
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
32+
throw new RuntimeException(e);
33+
}
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package io.github.imsejin.repo;
2+
3+
public class BookRepository {
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.github.imsejin.service;
2+
3+
import io.github.imsejin.annotation.Inject;
4+
import io.github.imsejin.repo.BookRepository;
5+
6+
public class BookService {
7+
8+
@Inject
9+
private BookRepository repository;
10+
11+
public BookRepository getRepository() {
12+
return repository;
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.github.imsejin.service;
2+
3+
import io.github.imsejin.repo.BookRepository;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
8+
class ContainerServiceTest {
9+
10+
@Test
11+
void getBookRepository() {
12+
// given
13+
BookRepository bookRepository = ContainerService.getObject(BookRepository.class);
14+
15+
// then
16+
assertThat(bookRepository).isNotNull();
17+
}
18+
19+
@Test
20+
void getBookService() {
21+
// given
22+
BookService bookService = ContainerService.getObject(BookService.class);
23+
24+
// then
25+
assertThat(bookService).isNotNull();
26+
assertThat(bookService.getRepository()).isNotNull();
27+
}
28+
29+
}

0 commit comments

Comments
 (0)