1.不使用Spring的实例:

01.Animal接口对应的代码:

  1. package cn.pb.dao;
  2.  
  3. /**
  4. * 动物接口
  5. */
  6. public interface Animal {
  7. //吃饭
  8. String eat();
  9. //睡觉
  10. void sleep();
  11. }

02.Animal接口的实现类Dog对应的代码:

  1. package cn.pb.dao.impl;
  2. /**
  3. * animal的实现类
  4. */
  5.  
  6. import cn.pb.dao.Animal;
  7.  
  8. public class Dog implements Animal{
  9. /**
  10. * 无参构造 验证什么时候实例被创建
  11. */
  12. public Dog(){
  13. System.out.println("dog被实例化了!");
  14. }
  15.  
  16. public String eat() {
  17. System.out.println("吃饭的方法");
  18. return null;
  19. }
  20.  
  21. public void sleep() {
  22. System.out.println("睡觉的方法");
  23. }
  24.  
  25. }

03.测试的代码:

  1. @Test
  2. public void test01(){
  3. //之前的一种方式 耦合的!
  4. Animal animal=new Dog();
  5. animal.eat();
  6. animal.sleep();
  7. }

2.使用spring解耦的方式 创建applicationContext.xml文件 放在src的根目录下

01.Animal接口对应的代码:

  1. package cn.pb.dao;
  2.  
  3. /**
  4. * 动物接口
  5. */
  6. public interface Animal {
  7. //吃饭
  8. String eat();
  9. //睡觉
  10. void sleep();
  11. }

02.Animal接口的实现类Dog对应的代码:

  1. package cn.pb.dao.impl;
  2. /**
  3. * animal的实现类
  4. */
  5.  
  6. import cn.pb.dao.Animal;
  7.  
  8. public class Dog implements Animal{
  9. /**
  10. * 无参构造 验证什么时候实例被创建
  11. */
  12. public Dog(){
  13. System.out.println("dog被实例化了!");
  14. }
  15.  
  16. public String eat() {
  17. System.out.println("吃饭的方法");
  18. return null;
  19. }
  20.  
  21. public void sleep() {
  22. System.out.println("睡觉的方法");
  23. }
  24.  
  25. }

03.applicationContext.xml配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd">
  7.  
  8. <!--接收程序给我们的bean对象
  9. id就是我们的一个标识
  10. class是对应的实现类,class不能是接口
  1.   lazy-init="true" 默认是false 按需加载,就是在getBean的时候才会创建实例
  1.   -->
  1. <bean id="dog" class="cn.pb.dao.impl.Dog" ></bean> </beans>

04.测试的代码:

001.applicationContext.xml放在项目的根路径下面

  1. @Test
  2. public void test02(){
  3. /*
  4. * 使用spring 对象交给容器来创建 解耦
  5. * 01.引入jar
  6. * 02.创建容器applicationContext.xml
  7. * 03.加载spring的配置文件 创建容器 会把容器中所有的bean实例化
  8. * 04.然后从容器中取Bean
  9. */
  10. ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
  11.  
  12. System.out.println("**************");
  13. //因为我们在容器中定义了ID 根据id找到对应的类
  14. Animal dog=(Dog)context.getBean("dog");
  15. dog.eat();
  16. dog.sleep();
  17. }

002.applicationContext.xml放在项目的根路径下面

  1. @Test
  2. public void test03(){
  3. /**
  4. * 默认applicationContext.xml放在项目的根路径下面
  5. * 也可以放在电脑指定的盘符下d:/applicationContext.xml
  6. * 使用new FileSystemXmlApplicationContext来创建对象
  7. */
  8.  
  9. ApplicationContext context=new FileSystemXmlApplicationContext("d:/applicationContext.xml");
  10. System.out.println("*************************");
  11. //因为我们在容器中定义了ID 根据id找到对应的类
  12. Animal dog=(Animal) context.getBean("dog");
  13. dog.eat();
  14. dog.sleep();
  15. }

003.使用BeanFactory来创建容器的时候,不会实例化容器中所有的Bean,

在getBean()才创建对应Bean的对象,按需加载。

  1. @Test
  2. public void test04(){
  3. /*
  4. * 使用BeanFactory来创建容器的时候,不会实例化容器中的Bean
  5. * 在getBean()才创建对应Bean的对象
  6. */
  7. BeanFactory context=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
  8. System.out.println("*************************");
  9. //因为我们在容器中定义了ID 根据id找到对应的类
  10. Animal dog=(Animal) context.getBean("dog");
  11. dog.eat();
  12. dog.sleep();
  13. }

05.在spring的核心配置文件中 所有的bean默认都是单例模式:

001.applicationContext.xml配置文件代码:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd">
  7.  
  8. <!--接收程序给我们的bean对象
  9. id就是我们的一个标识
  10. class是对应的实现类,class不能是接口
  11. -->
  12.  
  13. <!--配置我们的student对象 lazy-init="true" 默认是false 按需加载-->
  14. <bean id="student" class="cn.pb.bean.Student" lazy-init="true">
  15. <property name="name" value="小黑"></property>
  16. <property name="age" value="18"></property>
  17. </bean>
  18.  
  19. <!-- 在spring的核心配置文件中 所有的bean默认都是单例模式
  20. scope="singleton" 默认
  21. scope="prototype" 原型
  22. -->
  23. <bean id="student2" class="cn.pb.bean.Student" scope="singleton">
  24. <property name="age" value="40"/>
  25. <property name="name" value="小黑2"/>
  26. </bean>
  27.  
  28. </beans>

002.验证代码:

  1. /**
  2. * 验证单例模式
  3. * 01.默认是单例 调用同一个对象 输出true
  4. * 02.之后再xml文件中的student2 增加属性scope="prototype"
  5. * 03.再次验证 两个对象肯定不一致 输出false
  6. */
  7. @Test
  8. public void studentTest5(){
  9. ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
  10. System.out.println("*******************************************");
  11. Student student = (Student) context.getBean("student2");
  12. System.out.println(student);
  13. Student student2 = (Student) context.getBean("student2"); //再次获取
  14. System.out.println(student==student2);
  15.  
  16. }

Spring笔记02(3种加载配置文件的方式)的更多相关文章

  1. SpringMvc的创建流程以及2种加载配置文件的方式

    1.首先创建个web项目,第一步导入相应的jar包,并且buildtoPath 2.用elipse或myeclipse点击进入web.xml中 按住 Alt+ / 有个提示 找到前面带 #Dispat ...

  2. Spring 加载配置文件的方式

    我们常用的加载context文件的方法有如下三个: 1.FileSystemXmlApplicationContext 这个方法是从文件绝对路径加载配置文件,例如: ApplicationContex ...

  3. Spring 创建 IOC 容器时加载配置文件的几种方式

    一.ClassPathXmlApplicationContext 类路径加载 1. 使用 classpath 路径,classpath 前缀加不加都可以. ApplicationContext act ...

  4. Spring中加载配置文件的方式

    原文:http://blog.csdn.net/snowjlz/article/details/8158560 Spring 中加载XML配置文件的方式,好像有3种, XML是最常见的Spring 应 ...

  5. Java 加载配置文件的方式

    一 使用原生方式读取配置文件 1 文件系统加载 Java代码   InputStream in = new FileInputStream("config.properties") ...

  6. UE4:四种加载资源的方式

    转自:https://blog.csdn.net/zhangxsv123/article/details/79707686 第一种: 如果该蓝图有C++类(或者说是从C++类创建的蓝图),直接进行加载 ...

  7. angularJS1笔记-(19)-angular异步加载包的方式

    我们平时写的导入包的方式都是同步方式,有时候会显得过于卡顿,这样我们就可以使用异步加载的方式. script.js方式: 执行结果为: 异步加载还可以加载多个即为script([,,,],functi ...

  8. 两种加载dll的方式

    通过链接lib文件加载dll的话,使用过程中没法动态切换 通过loadlibrary函数动态加载的话,可以动态切换

  9. React Native两种加载图片的方式

    1 加载网络图片 通过uri就可以加载网络图片 <Image source={{uri:'http://facebook.github.io/react/img/logo_og.png'}} s ...

随机推荐

  1. robotframe使用之滚动条

    方法一:Excute JavaScript window.scrollTo(0,document.body.scrollHeight); 方法二:Execute javascript document ...

  2. android:scrollbar的一些属性

    1. activity_maim.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android ...

  3. 电容有什么作用?为什么cpu电源引脚都并联一个电容?

    管理 随笔- 17  文章- 1  评论- 1  电容有什么作用?为什么cpu电源引脚都并联一个电容?   正文: 参考资料:http://blog.sina.com.cn/s/blog_7880d3 ...

  4. 26最小公倍数 lowest common multiple

    题目描述 正整数A和正整数B 的最小公倍数是指 能被A和B整除的最小的正整数值,设计一个算法,求输入A和B的最小公倍数. 输入描述:输入两个正整数A和B. 输出描述:输出A和B的最小公倍数. 输入例子 ...

  5. js new一个函数和直接调用函数的差别

    用new和调用一个函数的差别:假设函数返回值是一个值类型(Number.String.Boolen)时,new函数将会返回这个函数的实例对象.而假设这个函数的返回值是一个引用类型(Object.Arr ...

  6. UVALive 6530 Football (水

    题目链接:点击打开链 #include <cstdio> #include <vector> #include <algorithm> using namespac ...

  7. 前端编程提高之旅(三)----浏览器兼容之IE6

    在爱奇艺实习期间,乐帝主要负责移动端活动页面的制作,因为移动浏览器是随着智能手机兴起的,这就决定了移动端不会重蹈浏览器兼容问题的覆辙.一開始就比較好的支持web标准,而纵观整个互联网行业,移动web开 ...

  8. 手动删除引用nuget如何还原

    1.不小心从项目的引用中删除了nuget安装的程序集; 2.从其他地方复制的packages.config到当前项目; 这两种情况 在解决方案中是无法通过还原nuget来还原程序集的,可以通过以下的方 ...

  9. IDEA下使用Jetty进行Debug模式调试

    过程例如以下: (1)找到选项卡中的 –Run– 然后找到 –Edit Configurations (2)点击下图中绿色的plus–找到Maven点进去 (3)依照下边的方式在Command lin ...

  10. 开源监控系统Prometheus介绍

    前言 Prometheus是CNCF的一个开源项目,Google BorgMon监控系统的开源版本,是一个系统和服务的监控系统.周期性采集metrics指标,匹配规则和展示结果,以及触发某些条件的告警 ...