第一种使用beanwrapper
Object obj=Class.forName("com.gc.action.HelloWorld").newInstance();
BeanWrapper bw=new BeanWrapperImpl(helloWorld);
bw.setPeopertyValue("msg","HelloWorld");
第二种,使用BeanFactory
InputStream is=new FileInputStream("config.xml");
或者是
ClassPathResource res=new ClassPathResource("config.xml");
XmlBeanFactory factory=new XmlBeanFactory(is);
HelloWorld helloWorld=(HelloWorld)factory.getBean("HelloWorld");
第三种,使用ApplicationContext
ApplicationContext act=new FileSystemXmlApplicationContext("config.xml");
HelloWorld helloWorld=(HelloWorld)act.getBean("HelloWorld");
其中ApplicationContext有以下几种:
AnnotationConfigApplicationContext:从一个或多个基于java的配置类中加载Spring应用上下文。
AnnotationConfigWebApplicationContext:从一个或多个基于java的配置类加载Spring Web上下文
ClassPathXmlApplicationContext:从类路径下的一个或多个XML配置文件中加载上下文定义,把应用上下文的定义文件作为类的资源。
FileSystemXMLApplicationContext:从文件系统下的一个或多个XML配置文件中加载上下文定义。