Skip to content

Commit db3b08f

Browse files
committed
[Bean-Factory] init simple bean factory
0 parents  commit db3b08f

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.m1a2st.bean;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* @Author m1a2st
8+
* @Date 2023/6/29
9+
* @Version v1.0
10+
*/
11+
public class BeanFactory {
12+
13+
private Map<String, Object> beanMap = new HashMap<>();
14+
15+
public void registerBean(String beanName, Object bean) {
16+
beanMap.put(beanName, bean);
17+
}
18+
19+
public Object getBean(String beanName) {
20+
return beanMap.get(beanName);
21+
}
22+
}

src/main/test/java/BasicTest.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
/**
5+
* @Author m1a2st
6+
* @Date 2023/6/29
7+
* @Version v1.0
8+
*/
9+
public class BasicTest {
10+
11+
@Test
12+
public void basic_test(){
13+
Assertions.assertTrue(true);
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.m1a2st.bean;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertNotNull;
7+
8+
/**
9+
* @Author m1a2st
10+
* @Date 2023/6/29
11+
* @Version v1.0
12+
*/
13+
public class BeanFactoryTest {
14+
15+
@Test
16+
public void test_bean_factory() {
17+
BeanFactory beanFactory = new BeanFactory();
18+
beanFactory.registerBean("helloService", new HelloService());
19+
HelloService helloService = (HelloService) beanFactory.getBean("helloService");
20+
assertNotNull(helloService);
21+
assertEquals(helloService.sayHello(), "Hello");
22+
}
23+
24+
static class HelloService {
25+
public String sayHello() {
26+
return "Hello";
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)