Skip to content

Commit dd2bf40

Browse files
committed
first
0 parents  commit dd2bf40

File tree

5 files changed

+169
-0
lines changed

5 files changed

+169
-0
lines changed

pom.xml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<groupId>us.codecraft</groupId>
4+
<artifactId>tiny-ioc</artifactId>
5+
<version>0.1.1-SNAPSHOT</version>
6+
<modelVersion>4.0.0</modelVersion>
7+
<packaging>jar</packaging>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
11+
</properties>
12+
<name>tiny-ioc</name>
13+
<description>
14+
A tinu ioc container for study.
15+
</description>
16+
<url>https://github.com/code4craft/xsoup/</url>
17+
<developers>
18+
<developer>
19+
<id>code4craft</id>
20+
<name>Yihua huang</name>
21+
<email>code4crafer@gmail.com</email>
22+
</developer>
23+
</developers>
24+
<licenses>
25+
<license>
26+
<name>The MIT License</name>
27+
<distribution>repo</distribution>
28+
</license>
29+
</licenses>
30+
31+
<dependencies>
32+
<dependency>
33+
<groupId>junit</groupId>
34+
<artifactId>junit</artifactId>
35+
<version>4.7</version>
36+
<scope>test</scope>
37+
</dependency>
38+
</dependencies>
39+
40+
<build>
41+
<plugins>
42+
<plugin>
43+
<groupId>org.apache.maven.plugins</groupId>
44+
<artifactId>maven-compiler-plugin</artifactId>
45+
<version>3.1</version>
46+
<configuration>
47+
<source>1.6</source>
48+
<target>1.6</target>
49+
<encoding>UTF-8</encoding>
50+
</configuration>
51+
</plugin>
52+
<plugin>
53+
<groupId>org.apache.maven.plugins</groupId>
54+
<artifactId>maven-resources-plugin</artifactId>
55+
<version>2.6</version>
56+
<configuration>
57+
<encoding>UTF-8</encoding>
58+
</configuration>
59+
</plugin>
60+
<plugin>
61+
<groupId>org.apache.maven.plugins</groupId>
62+
<artifactId>maven-source-plugin</artifactId>
63+
<version>2.2.1</version>
64+
<executions>
65+
<execution>
66+
<id>attach-sources</id>
67+
<goals>
68+
<goal>jar</goal>
69+
</goals>
70+
</execution>
71+
</executions>
72+
</plugin>
73+
<plugin>
74+
<groupId>org.apache.maven.plugins</groupId>
75+
<artifactId>maven-javadoc-plugin</artifactId>
76+
<version>2.9.1</version>
77+
<configuration>
78+
<encoding>UTF-8</encoding>
79+
</configuration>
80+
<executions>
81+
<execution>
82+
<id>attach-javadocs</id>
83+
<goals>
84+
<goal>jar</goal>
85+
</goals>
86+
</execution>
87+
</executions>
88+
</plugin>
89+
</plugins>
90+
</build>
91+
92+
93+
94+
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package us.codecraft.tinyioc;
2+
3+
/**
4+
* @author yihua.huang@dianping.com
5+
*/
6+
public class BeanDefinition {
7+
8+
private Object bean;
9+
10+
public BeanDefinition(Object bean) {
11+
this.bean = bean;
12+
}
13+
14+
public Object getBean() {
15+
return bean;
16+
}
17+
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package us.codecraft.tinyioc;
2+
3+
import java.util.Map;
4+
import java.util.concurrent.ConcurrentHashMap;
5+
6+
/**
7+
* @author yihua.huang@dianping.com
8+
*/
9+
public class BeanFactory {
10+
11+
private Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>();
12+
13+
public Object getBean(String name) {
14+
return beanDefinitionMap.get(name).getBean();
15+
}
16+
17+
public void registerBeanDefinition(String name, BeanDefinition beanDefinition) {
18+
beanDefinitionMap.put(name, beanDefinition);
19+
}
20+
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package us.codecraft.tinyioc;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* @author yihua.huang@dianping.com
7+
*/
8+
public class BeanFactoryTest {
9+
10+
@Test
11+
public void test() {
12+
// 1.初始化beanfactory
13+
BeanFactory beanFactory = new BeanFactory();
14+
15+
// 2.注入bean
16+
BeanDefinition beanDefinition = new BeanDefinition(new HelloWorldService());
17+
beanFactory.registerBeanDefinition("helloWorldService", beanDefinition);
18+
19+
// 3.获取bean
20+
HelloWorldService helloWorldService = (HelloWorldService) beanFactory.getBean("helloWorldService");
21+
helloWorldService.helloWorld();
22+
23+
24+
}
25+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package us.codecraft.tinyioc;
2+
3+
/**
4+
* @author yihua.huang@dianping.com
5+
*/
6+
public class HelloWorldService {
7+
8+
public void helloWorld(){
9+
System.out.println("Hello World!");
10+
}
11+
}

0 commit comments

Comments
 (0)