zoukankan      html  css  js  c++  java
  • Spring框架学习之第4节

    从ApplicaionContext应用上下文容器中获取bean和从bean工厂容器中有什么区别: 

    具体案例如下 

    结论:

    1、如果使用上下文ApplicationContext,则配置的bean如果是Singleton不管你用不用,都被实例化(好处是可以预先加载,用时就不加载,速度快,缺点就是耗内存)

    2、如果是BeanFactory,当你实例化该对象时候,配置的bean不会被马上实例化,当你使用的时候才实例(好处是节约内存,缺点就是速度有点慢)

    3、规定:一般没有特殊要求,应当使用ApplicationContext完成(90%),当内存确实太小时可以用beanFactory,比如移动设备或手持设备。

    项目结构:

    beans.xml

    <?xml version="1.0" encoding="utf-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    		xmlns:aop="http://www.springframework.org/schema/aop"
    		xmlns:tx="http://www.springframework.org/schema/tx"
    		xsi:schemaLocation="
    			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    
    <bean id="student" class="com.litao.ioc.Student">
    <property name="name" value="小猪" />
    </bean>
    </beans>
    			
    

    Student.java

    package com.litao.ioc;
    
    public class Student {
    	
    	private String name;
    	//Java对象都有一个默认无参构造函数
    	public Student(){
    		System.out.println("对象被创建");
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    }
    

    App1.java

    package com.litao.ioc;
    
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.core.io.ClassPathResource;
    
    public class App1 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    		//从ApplicationContext中取bean
    		//ApplicationContext ac = new ClassPathXmlApplicationContext("com/litao/ioc/beans.xml");
    		//当我们去实例化beans.xml,该文件中配置的bean被实例化(),如果该bean scope是singleton不管你有没有
    		//使用这个bean,该bean都会被实例化.
    		//从bean工厂取出Student
    		
    		//如果我们使用BeanFactory去获取bean,当你只是实例化该容器,那么容器的bean不会被实例化
    		//只有当你去使用getBean某个bean时,才会实时被创建
    		
    		BeanFactory factory = new XmlBeanFactory(
    				new ClassPathResource("com/litao/ioc/beans.xml"));
    		//运行时没有Student被创建
    		factory.getBean("student");
    		
    		//软件发展速度是慢于这个硬件的	
    		
    		
    	}
    
    }
    
  • 相关阅读:
    scrapy爬虫框架
    MongoDB分组查询,聚合查询,以及复杂查询
    mac重启iterm后不会自动加载.bash_profile
    Linux 之 2>&1
    linux下利用nohup后台运行jar文件包程序
    【intellij idea】汇总
    JAVA虚拟机体系结构
    JDK > JRE > JVM
    依赖注入(IOC) 详解
    mysql 忘记密码 登陆+修改密码
  • 原文地址:https://www.cnblogs.com/beautiful-code/p/5773999.html
Copyright © 2011-2022 走看看