├── README.md
├── pom.xml
└── src
├── main
└── java
│ └── com
│ └── springstudy
│ └── simplespring
│ ├── BeanDefinition.java
│ ├── BeanDefinitionHolder.java
│ ├── BeanFactory.java
│ ├── PropertyValue.java
│ └── PropertyValues.java
└── test
└── java
└── com
└── springstudy
└── simplespring
├── HelloWorld.java
└── TestIoC.java
/README.md:
--------------------------------------------------------------------------------
1 | # Simple Spring
2 | A simple Spring framwork.
3 |
4 | ## Feature
5 | **IoC Feature:**
6 | * [Basic bean](https://github.com/Yikun/simple-spring/blob/master/src/test/java/com/springstudy/simplespring/TestIoC.java#L12)
7 | * [Bean with property](https://github.com/Yikun/simple-spring/blob/master/src/test/java/com/springstudy/simplespring/TestIoC.java#L29)
8 |
9 | ## Download
10 | ```bash
11 | git clone https://github.com/Yikun/simple-spring.git
12 | ```
13 |
14 | ## Test
15 | ```bash
16 | mvn test
17 | ```
18 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | com.springstudy
6 | simple-spring
7 | 0.0.1-SNAPSHOT
8 | jar
9 |
10 | simple-spring
11 | http://maven.apache.org
12 |
13 |
14 | UTF-8
15 |
16 |
17 |
18 |
19 | junit
20 | junit
21 | 4.7
22 | test
23 |
24 |
25 |
26 |
27 |
28 |
29 | org.apache.maven.plugins
30 | maven-compiler-plugin
31 | 3.1
32 |
33 | 1.6
34 | 1.6
35 | UTF-8
36 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/src/main/java/com/springstudy/simplespring/BeanDefinition.java:
--------------------------------------------------------------------------------
1 | package com.springstudy.simplespring;
2 |
3 | public class BeanDefinition {
4 | private Class beanClass;
5 | private String beanClassName;
6 | private PropertyValues propertyValues;
7 |
8 | public Class getBeanClass() {
9 | return beanClass;
10 | }
11 |
12 | public String getBeanClassName() {
13 | return beanClassName;
14 | }
15 |
16 | public void setBeanClassName(String beanClassName) {
17 | this.beanClassName = beanClassName;
18 | // 根据类名设置类
19 | try {
20 | this.beanClass = Class.forName(beanClassName);
21 | } catch (ClassNotFoundException e) {
22 | e.printStackTrace();
23 | }
24 | }
25 |
26 | public PropertyValues getPropertyValues() {
27 | return propertyValues;
28 | }
29 |
30 | public void setPropertyValues(PropertyValues pvs) {
31 | this.propertyValues = pvs;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/com/springstudy/simplespring/BeanDefinitionHolder.java:
--------------------------------------------------------------------------------
1 | package com.springstudy.simplespring;
2 |
3 | public class BeanDefinitionHolder {
4 | private final BeanDefinition beanDefinition;
5 | private final String beanName;
6 |
7 | public BeanDefinitionHolder(String beanName, BeanDefinition beanDefinition) {
8 | this.beanName = beanName;
9 | this.beanDefinition = beanDefinition;
10 | }
11 |
12 | public BeanDefinition getBeanDefinition() {
13 | return this.beanDefinition;
14 | }
15 |
16 | public String getBeanName() {
17 | return this.beanName;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/springstudy/simplespring/BeanFactory.java:
--------------------------------------------------------------------------------
1 | package com.springstudy.simplespring;
2 |
3 | import java.lang.reflect.Field;
4 | import java.util.Map;
5 | import java.util.concurrent.ConcurrentHashMap;
6 |
7 | public class BeanFactory {
8 | // 用于存储bean的单例实体
9 | private final Map singletonObjects = new ConcurrentHashMap(64);
10 | // 用于存储beanDefinition
11 | private final Map beanDefinitionMap = new ConcurrentHashMap();
12 |
13 | // 注册BeanDefinition
14 | public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
15 | beanDefinitionMap.put(beanName, beanDefinition);
16 | }
17 |
18 | // 获取bean,如果不存在则创建
19 | public Object getBean(String beanName) {
20 | Object bean = getSingleton(beanName);
21 | if(bean == null) {
22 | try {
23 | bean = doCreateBean(beanName, beanDefinitionMap.get(beanName));
24 | } catch (Exception e) {
25 | // TODO Auto-generated catch block
26 | e.printStackTrace();
27 | }
28 | singletonObjects.put(beanName, bean);
29 | }
30 | return bean;
31 | }
32 |
33 | private Object getSingleton(String beanName) {
34 | return this.singletonObjects.get(beanName);
35 | }
36 |
37 | private Object doCreateBean(final String beanName, final BeanDefinition beanDefinition) throws Exception {
38 | Object bean = createBeanInstance(beanDefinition);
39 | applyPropertyValues(bean, beanDefinition);
40 | return bean;
41 | }
42 |
43 | private Object createBeanInstance(final BeanDefinition beanDefinition) throws Exception {
44 | Object bean;
45 | bean = beanDefinition.getBeanClass().newInstance();
46 | return bean;
47 | }
48 |
49 | private void applyPropertyValues(Object bean, BeanDefinition beanDefinition) throws Exception{
50 | PropertyValues pvs = beanDefinition.getPropertyValues();
51 | if(pvs != null) {
52 | for(PropertyValue pv : pvs.getPropertyValueList()) {
53 | Field field = bean.getClass().getDeclaredField(pv.getName());
54 | field.setAccessible(true);
55 | field.set(bean, pv.getValue());
56 | }
57 | }
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/main/java/com/springstudy/simplespring/PropertyValue.java:
--------------------------------------------------------------------------------
1 | package com.springstudy.simplespring;
2 |
3 | public class PropertyValue {
4 | private final String name;
5 | private final Object value;
6 |
7 | public PropertyValue(String name, Object value) {
8 | this.name = name;
9 | this.value = value;
10 | }
11 |
12 | public String getName() {
13 | return name;
14 | }
15 |
16 | public Object getValue() {
17 | return value;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/springstudy/simplespring/PropertyValues.java:
--------------------------------------------------------------------------------
1 | package com.springstudy.simplespring;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | public class PropertyValues {
7 | private final List propertyValueList;
8 |
9 | public PropertyValues() {
10 | this.propertyValueList = new ArrayList();
11 | }
12 |
13 | public void addPropertyValue(PropertyValue pv) {
14 | propertyValueList.add(pv);
15 | }
16 |
17 | public List getPropertyValueList() {
18 | return propertyValueList;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/test/java/com/springstudy/simplespring/HelloWorld.java:
--------------------------------------------------------------------------------
1 | package com.springstudy.simplespring;
2 |
3 | public class HelloWorld {
4 | private String msg = "Hello World!";
5 |
6 | public String sayHello() {
7 | return msg;
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/test/java/com/springstudy/simplespring/TestIoC.java:
--------------------------------------------------------------------------------
1 | package com.springstudy.simplespring;
2 |
3 | import static org.junit.Assert.assertEquals;
4 |
5 | import org.junit.Test;
6 |
7 | import com.springstudy.simplespring.BeanDefinitionHolder;
8 | import com.springstudy.simplespring.BeanFactory;
9 |
10 | public class TestIoC {
11 | @Test
12 | public void testIoC() {
13 | // 1. 创建beanFactory
14 | BeanFactory beanFactory = new BeanFactory();
15 |
16 | // 2. 注册bean
17 | BeanDefinition bd = new BeanDefinition();
18 | bd.setBeanClassName("com.springstudy.simplespring.HelloWorld");
19 |
20 | BeanDefinitionHolder bdh = new BeanDefinitionHolder("helloWorld", bd);
21 | beanFactory.registerBeanDefinition(bdh.getBeanName(), bdh.getBeanDefinition());
22 |
23 | // 3. 获取bean
24 | HelloWorld hello = (HelloWorld) beanFactory.getBean("helloWorld");
25 | assertEquals("Hello World!", hello.sayHello());
26 | }
27 |
28 | @Test
29 | public void testIoCProperty() {
30 | // 1. 创建beanFactory
31 | BeanFactory beanFactory = new BeanFactory();
32 |
33 | // 2. 注册bean
34 | BeanDefinition bd = new BeanDefinition();
35 | bd.setBeanClassName("com.springstudy.simplespring.HelloWorld");
36 |
37 | // 注入Property
38 | PropertyValues pvs = new PropertyValues();
39 | pvs.addPropertyValue(new PropertyValue("msg", "Hello IoC Property!"));
40 | bd.setPropertyValues(pvs);
41 |
42 | BeanDefinitionHolder bdh = new BeanDefinitionHolder("helloWorld", bd);
43 | beanFactory.registerBeanDefinition(bdh.getBeanName(), bdh.getBeanDefinition());
44 |
45 | // 3. 获取bean
46 | HelloWorld hello = (HelloWorld) beanFactory.getBean("helloWorld");
47 | assertEquals("Hello IoC Property!", hello.sayHello());
48 | }
49 | }
50 |
--------------------------------------------------------------------------------