Spring helloworld
【http://wiki.jikexueyuan.com/project/spring/hello-world-example.html】 html
HelloWorld.java
package helloworld; public class HelloWorld { private String message; public void getMessage() { System.out.println("message="+this.message); } public void setMessage(String message) { this.message = message; } }
MainApp.java
package helloworld; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.core.io.ClassPathResource; public class MainApp { public static void main(String[] args) { // ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");//1 ApplicationContext // XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("Beans.xml"));//2 XmlBeanFactory FileSystemXmlApplicationContext appContext =new FileSystemXmlApplicationContext("D:/PZHCRec/tobeBetter/HTML/jwebwork/spring_0204_jikexueyuan/src/Beans.xml"); HelloWorld pojo = (HelloWorld) appContext.getBean("HelloWorld");//3 FileSystemXmlApplicationContext pojo.getMessage(); } }
Beans.xml
package helloworld; public class HelloWorld { private String message; public void getMessage() { System.out.println("message="+this.message); } public void setMessage(String message) { this.message = message; } }
IoC容器
Spring IoC容器是Spring 的核心,管理着javaBean對象的建立到銷燬整個生命週期。Spring經過與元數據來進行實例化配置對象,而元數據能夠是Java代碼、xml、註解。SpringIOC 容器原理 (官方文檔截圖) java
Spring BeanFactory
它是最簡單的容器,給 DI 提供了基本的支持,它用 org.springframework.beans.factory.BeanFactory 接口來定義。BeanFactory 或者相關的接口,如 BeanFactoryAware,InitializingBean,DisposableBean,在 Spring 中仍然存在具備大量的與 Spring 整合的第三方框架的反向兼容性的目的。web
Spring ApplicationContext
該容器添加了更多的企業特定的功能,例如從一個屬性文件中解析文本信息的能力,發佈應用程序事件給感興趣的事件監聽器的能力。該容器是由 org.springframework.context.ApplicationContext 接口定義。spring