1.不使用Spring的实例:

01.Animal接口对应的代码:

package cn.pb.dao;

/**
* 动物接口
*/
public interface Animal {
//吃饭
String eat();
//睡觉
void sleep();
}

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

package cn.pb.dao.impl;
/**
* animal的实现类
*/ import cn.pb.dao.Animal; public class Dog implements Animal{
/**
* 无参构造 验证什么时候实例被创建
*/
public Dog(){
System.out.println("dog被实例化了!");
} public String eat() {
System.out.println("吃饭的方法");
return null;
} public void sleep() {
System.out.println("睡觉的方法");
} }

03.测试的代码:

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

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

01.Animal接口对应的代码:

package cn.pb.dao;

/**
* 动物接口
*/
public interface Animal {
//吃饭
String eat();
//睡觉
void sleep();
}

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

package cn.pb.dao.impl;
/**
* animal的实现类
*/ import cn.pb.dao.Animal; public class Dog implements Animal{
/**
* 无参构造 验证什么时候实例被创建
*/
public Dog(){
System.out.println("dog被实例化了!");
} public String eat() {
System.out.println("吃饭的方法");
return null;
} public void sleep() {
System.out.println("睡觉的方法");
} }

03.applicationContext.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--接收程序给我们的bean对象
id就是我们的一个标识
class是对应的实现类,class不能是接口
  lazy-init="true" 默认是false 按需加载,就是在getBean的时候才会创建实例
  -->
<bean id="dog" class="cn.pb.dao.impl.Dog" ></bean> </beans>

04.测试的代码:

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

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

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

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

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

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

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

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

001.applicationContext.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--接收程序给我们的bean对象
id就是我们的一个标识
class是对应的实现类,class不能是接口
--> <!--配置我们的student对象 lazy-init="true" 默认是false 按需加载-->
<bean id="student" class="cn.pb.bean.Student" lazy-init="true">
<property name="name" value="小黑"></property>
<property name="age" value="18"></property>
</bean> <!-- 在spring的核心配置文件中 所有的bean默认都是单例模式
scope="singleton" 默认
scope="prototype" 原型
-->
<bean id="student2" class="cn.pb.bean.Student" scope="singleton">
<property name="age" value="40"/>
<property name="name" value="小黑2"/>
</bean> </beans>

002.验证代码:

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

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. HDOJ2084数塔问题

    数塔问题 题目要求从顶层走究竟层.若每一步仅仅能走到相邻的结点,求经过的结点的数字之和最大值. 非常经典的DP,能够这样考虑,要求从塔顶到塔底最大路径之和.计算时能够考虑自底向上,走最后一步所选的数一 ...

  2. python thrift hbase安装连接

    默认已装好 hbase,我的版本是hbase-0.98.24,并运行 python 2.7.x 步骤: sudo apt-get install automake bison flex g++ git ...

  3. 一、Silverlight中使用MVVM(一)——基础

    如果你不知道MVVM模式,我建议你先了解一下MVVM模式,至少要知道实现该模式的意图是什么. 那么我主要通过我认为是已经算是比较简单的例子进行讲解这个模式,当然后面我们会在这个例子的基础上一步一步的进 ...

  4. 高特权级代码段转向低特权级代码段(利用 ret(retf) 指令实现 jmp from ring0 to ring3)

    [0]写在前面 0.1)本代码旨在演示 从 ring0 转移到 ring3(即,从高特权级 转移到 低特权级) 0.2)本文 只对 与 门相关的 代码进行简要注释,言简意赅: 0.3)文末的个人总结是 ...

  5. windows下MySQL 5.7+ 解压缩版安装配置方法(转,写的很简单精辟 赞)

    方法来自伟大的互联网. 1.去官网下载.zip格式的MySQL Server的压缩包,根据需要选择x86或x64版.注意:下载是需要注册账户并登录的. 2.解压缩至你想要的位置. 3.复制解压目录下m ...

  6. 2017-01-20_dp测试

    题目:http://files.cnblogs.com/files/shenben/2017-01-20problems.pdf 数据包(含解题报告):http://files.cnblogs.com ...

  7. EasyPlayerPro Windows播放器进行本地对讲喊话音频采集功能实现

    需求 在安防行业应用中,除了在本地看到摄像机的视频和进行音频监听外,还有一个重要的功能,那就是对讲. EasyPlayerPro-win为了减轻二次开发者的工作量,将本地音频采集也进行了集成: 功能特 ...

  8. 九度OJ 1011:最大连续子序列 (DP)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5615 解决:2668 题目描述:     给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, N ...

  9. http://blog.csdn.net/wh211212/article/details/53005321

    http://blog.csdn.net/wh211212/article/details/53005321

  10. DB Migrations更新数据库命令

    在项目迭代的过程中,数据库结构常常需要跟随业务需求的变化做出调整,尤其在迭代的初期阶段,加一个字段减一个字段的需求更是家常便饭.在小型团队中,往往是负责开发功能模块的程序员在完成本地开发环境数据库的变 ...