객체 저장소
package com.fastcampus.ch3.diCopy2;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
class Car {}
class Truck extends Car {}
class SportsCar extends Car{}
class Engine {}
class AppContext {
Map map; //객체 저장소
AppContext(){
try {
Properties p = new Properties();
p.load(new FileReader("config.txt"));
//Properties에 저장된 내용을 Map에 저장
map = new HashMap(p);
//반복문으로 클래스 이름을 얻어서 객체를 생성해서 다시 map에 저장
for(Object key : map.keySet()){
Class clazz = Class.forName((String)map.get(key));
map.put(key, clazz.newInstance());
}
} catch (Exception e) {
e.printStackTrace();
}
}
Object getBean(String key){
return map.get(key);
}
}
public class Main2 {
public static void main(String[] args) throws Exception {
AppContext ac = new AppContext();
Car car = (Car)ac.getBean("car");
Engine engine = (Engine)ac.getBean("engine");
System.out.println("car = " + car);
System.out.println("engine = " + engine);
}
}
@Component
1. 패키지 내의 모든 클래스를 읽어서 Set에 저장.
2. 패키지 내에 @Component붙은 클래스 찾기.
3. @Component가 붙어있으면 객체를 생성해서 map에 저장.
package com.fastcampus.ch3.diCopy3;
import com.google.common.reflect.ClassPath;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.stereotype.Component;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
@Component class Car {}
@Component class Truck extends Car {}
@Component class SportsCar extends Car{}
//@Component
class Engine {}
class AppContext {
Map map; //객체 저장소
AppContext(){
map = new HashMap();
doComponentScan();
}
private void doComponentScan() {
try {
//1. 패키지내의 클래스 목록을 가져온다.
//2. 반복문으로 클래스를 하나씩 읽어와서 @Component이 붙어 있는지 확인
//3. @Component가 붙어있으면 객체를 생성해서 map에 저장
ClassLoader classLoader = AppContext.class.getClassLoader();
ClassPath classPath = ClassPath.from(classLoader);
Set<ClassPath.ClassInfo> set = classPath.getTopLevelClasses("com.fastcampus.ch3.diCopy3");
for(ClassPath.ClassInfo classInfo : set){
Class clazz = classInfo.load();
Component component = (Component)clazz.getAnnotation(Component.class);
if(component != null){
String id = StringUtils.uncapitalize(classInfo.getSimpleName());
map.put(id, clazz.newInstance());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Object getBean(String key){
return map.get(key);
}
}
public class Main3 {
public static void main(String[] args) throws Exception {
AppContext ac = new AppContext();
Car car = (Car)ac.getBean("car");
Engine engine = (Engine)ac.getBean("engine");
System.out.println("car = " + car);
System.out.println("engine = " + engine);
}
}