zoukankan      html  css  js  c++  java
  • Java Web 深入分析(10) Spring 实践

    Spring helloworld

    http://wiki.jikexueyuan.com/project/spring/hello-world-example.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 容器原理 (官方文档截图)

    Spring BeanFactory

    它是最简单的容器,给 DI 提供了基本的支持,它用 org.springframework.beans.factory.BeanFactory 接口来定义。BeanFactory 或者相关的接口,如 BeanFactoryAware,InitializingBean,DisposableBean,在 Spring 中仍然存在具有大量的与 Spring 整合的第三方框架的反向兼容性的目的。

    Spring ApplicationContext

    该容器添加了更多的企业特定的功能,例如从一个属性文件中解析文本信息的能力,发布应用程序事件给感兴趣的事件监听器的能力。该容器是由 org.springframework.context.ApplicationContext 接口定义。

  • 相关阅读:
    C#和sqlserver中生成新的32位GUID
    IIS7下swfupload上传大文件出现404错误
    jQuery 判断是否为数字的方法 及 转换数字函数
    js数组与字符串的相互转换方法
    jquery 中如何将数组转化为json字符串,然后再转化回来?
    Firemonkey Android 虚拟机
    Eclipse apk 签名
    win10 修改hosts
    eclipse 预览Android界面报错
    夜神模拟器
  • 原文地址:https://www.cnblogs.com/humi/p/8414713.html
Copyright © 2011-2022 走看看