Skip to content

Commit b98c8c3

Browse files
committed
beans instanciated, and inserted into container
0 parents  commit b98c8c3

File tree

5 files changed

+93
-0
lines changed

5 files changed

+93
-0
lines changed

src/main/java/si/inspirited/Main.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main.java.si.inspirited;
2+
3+
import org.springframework.beans.factory.BeanFactory;
4+
5+
public class Main {
6+
public static void main(String[] args) {
7+
8+
BeanFactory beanFactory = new BeanFactory();
9+
beanFactory.instantiate("main.java.si.inspirited");
10+
}
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main.java.si.inspirited;
2+
3+
import org.springframework.beans.factory.stereotype.Component;
4+
5+
@Component
6+
public class ProductService {
7+
8+
private PromotionsService promotionsService;
9+
10+
public PromotionsService getPromotionsService() {
11+
return promotionsService;
12+
}
13+
14+
public void setPromotionsService(PromotionsService promotionsService) {
15+
this.promotionsService = promotionsService;
16+
}
17+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package main.java.si.inspirited;
2+
3+
import org.springframework.beans.factory.stereotype.Component;
4+
5+
@Component
6+
public class PromotionsService {
7+
8+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.springframework.beans.factory;
2+
3+
import org.springframework.beans.factory.stereotype.Component;
4+
5+
import java.io.File;
6+
import java.net.URL;
7+
import java.util.Enumeration;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
public class BeanFactory {
12+
13+
private Map<String, Object> singletons = new HashMap();
14+
15+
public Object getBean(String beanName){
16+
return singletons.get(beanName);
17+
}
18+
19+
public void instantiate(String basePackage) {
20+
try {
21+
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
22+
String path = basePackage.replace('.', '/'); //"main.java.si.inspirited" -> "main/java/si/inspirited"
23+
Enumeration<URL> resources = classLoader.getResources(path);
24+
25+
while (resources.hasMoreElements()) {
26+
URL resource = resources.nextElement();
27+
28+
File file = new File(resource.toURI());
29+
for (File classFile : file.listFiles()) {
30+
String fileName = classFile.getName();//ProductService.class
31+
32+
if (fileName.endsWith(".class")) {
33+
String className = fileName.substring(0, fileName.lastIndexOf("."));
34+
Class classObject = Class.forName(basePackage + "." + className);
35+
36+
if (classObject.isAnnotationPresent(Component.class)) {
37+
System.out.println("Component: " + classObject);
38+
}
39+
Object instance = classObject.getDeclaredConstructor().newInstance();//=new CustomClass()
40+
String beanName = className.substring(0, 1).toLowerCase() + className.substring(1);
41+
singletons.put(beanName, instance);
42+
}
43+
}
44+
}
45+
}catch (Exception e) {e.printStackTrace();}
46+
System.out.println(singletons);
47+
}
48+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.springframework.beans.factory.stereotype;
2+
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.RetentionPolicy;
5+
6+
@Retention(RetentionPolicy.RUNTIME)
7+
public @interface Component {
8+
9+
}

0 commit comments

Comments
 (0)