package net.spring.test;
import java.lang.reflect.Method;
import net.spring.util.LogInitiate;
import net.spring.util.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
* @author chengli 无侵入式的可扩展框架:无需代码中涉及Spring类,即可纳入Spring容器进行管理
* 核心类BeanFactory作为依赖注入机制的实现基础
*/
public class QuickStart {
public static void main(String[] args) {
// BasicConfigurator.configure();
LogInitiate.initialize();
Logger logger = new Logger(QuickStart.class);
try {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"bean.xml");
// 从ctx中读取的配置信息区分大小写
// Spring通过依赖注入机制,将依赖关系从编码中脱离,从而大大降低组件之间的耦合
// 通过接口来将调用者和实现者分离
Action action = (Action) ctx.getBean("UpperAction");
logger.info(action.execute(" Mis JohSon"));
action = (Action) ctx.getBean("LowerAction");
logger.info(action.execute(" Mis JohSon"));
} catch (Exception e) {
// e.printStackTrace();
logger.error(e.getMessage());
}
}
public Object reflection(String className,String methodName ,String nameValue) {
Class cls;
Object obj = null;
try {
cls = Class.forName(className);
Method mtd = cls.getMethod(methodName, new Class[] { String.class });
obj = (Object) cls.newInstance();
mtd.invoke(obj, new Object[] { nameValue });
return obj;
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
}