1.准备工作

  下载Spring:http://repo.spring.io/libs-release-local/org/springframework/spring/

    

   选择需要下载的版本

    选择dist.zip结尾的下载即可。

   下载完后将其解压,进入libs文件夹,libs文件夹下有很多jar文件.

   目前阶段只需要将 spring-core-x.x.x.RELEASE.jar 、spring-beans-x.x.x.RELEASE.jar

    spring-context-x.x.x.RELEASE.jar、 spring-expression-x.x.x.RELEASE.jar导入到项目中。

   要是想全部导入也可以。

   下载commons-Logging.jar

   下载地址:http://commons.apache.org/proper/commons-logging/download_logging.cgi

   点击下载。

   将解压后的commons-logging.jar导入项目。

    

2.核心容器(BeanFactory,ApplicationContext)

  BeanFactory:BeanFactory是org.springframework.beans.facytory . BeanFactory 接口定义.是基础的IOC(控制反转)容器。

  控制反转即控制权发生了反转,例如创建一个对象,一般都是我们通过new来创建,而发生控制反转后创建由

  容器来创建(容器会根据配置文件进行创建),不用我们显示的new一个对象。

  可以将BeanFactory看作一个管理bena的工厂类,负责初始化等操作。

   

  ApplicationContext:ApplicationContext是BeanFactory的子接口,除了包含BeanFactory中所有功能外还包含

  国际化、资源访问、事件传播等方面的支持。

  ApplicationContext实例创建方法:

    2.1.ClassPathXmlApplication

    ClasspathXmlApplication会从类路径中寻找指定的xml文件并加载。(一般采用这种方式)

ApplicationContext applicationContext = new ClassPathApplicationContext("xxx.xml");

  

    2.2.FileSystemXmlApplicationContext

    FileSystemXmlApplication是指定绝对路径来进行查找,这种方式不灵活使用较少。

ApplicationContext ac = new FileSystemXmlApplicationContext("C:text/.../xxx.xml");

    ApplicationContext中有一个方法:Object getBean(String name);通过name(xml文件中的id属性)获取指定类。

  2.3 小例子

  首先创建一个web项目。

  2.3.1我们创建一个Animal接口,并定义一个say()方法。

public interface Animal {
public void say();
}

  

  2.3.2再创建一个类Cat实现Animal接口

public class Cat implements Animal{

    @Override
public void say() {
// TODO Auto-generated method stub
System.out.println("喵喵喵");
}
}

  

  2.3.3在src目录下创建beans.xml文件(一般命名为beans.xml,也有叫applicatinContex.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-4.3.xsd">
<!-- 一将指定类配置给 Spring,让 Spring 创建其对象的实例一 -->
<bean id = "cat" class = "com.ioc.Cat"> </bean> <!-- 设置id及对应类 -->
</beans>

  2.3.4测试IOC容器实例化对象

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
Animal cat = (Animal)ac.getBean("cat");//cat为配置文件中id属性的值,通过配置文件的id获取对应类
cat.say();
}
}
运行结果:
二月 09, 2019 5:07:47 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@46f7f36a: startup date [Sat Feb 09 17:07:47 CST 2019]; root of context hierarchy
二月 09, 2019 5:07:47 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
喵喵喵

上例我们没有显示的new,而是通过容器创建了一个对象,控制发生了反转从自己控制变成了容器控制。

容器创建时默认是单例模式,即每次获取的都是一个对象。要想每次获取的对象都是不同的对象可按如下设置:

<bean id = "cat" class = "com.ioc.Cat" scope = "prototype”> </bean>

3.依赖注入

  问题引出:假如当前有一个Person类,person要养宠物,于是我们可以这样写;

public Person{
Dog dog;
Cat cat;
...
...
}

person每想养一个宠物就要修改代码为其添加一个宠物,这样很麻烦,而且扩展性解耦性都不好。

于是我们首先可以将Dog、Cat改为Animal,这样比之前好一些了,所有宠物都是Animal。

但这样在需求有变时还是需要修改一些内容,这样的确比之前的想法方便了一些,但有没有更方便的方法呢?

如果Animal的属性是通过.xml文件中的设置来进行赋值的呢?这样是不是更方便了,当需求改变时在xml修改即可。

我们就在原有例子的基础上来进行这个实验:

  3.1创建一个Dog类,实现Animal接口

public class Dog implements Animal{

    @Override
public void say() {
// TODO Auto-generated method stub
System.out.println("汪汪汪");
}
}

  

  3.2创建Person类

public class Person {
private Animal animal;//养的宠物 public Animal getAnimal() {
return animal;
} public void setAnimal(Animal animal) {//set方法一定要有
this.animal = animal;
} public void palyAnimal() {//设置一个玩宠物的方法
animal.say();//玩宠物宠物发出来对应的声音
}
}

  3.3beans.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-4.3.xsd">
<!-- 一将指定类配置给 Spring,让 Spring 创建其对象的实例一 -->
<bean id = "cat" class = "com.ioc.Cat"> </bean>
<bean id = "person" class = "com.ioc.Person"> <!--设置Person类及其对应id-->
<property name="animal" ref="cat"></property> <!--设置属性 name为属性名 ref为将bean定义的实例注入到属性(id = cat 注入)-->
</bean>
</beans>

  3.4测试

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
Person person = (Person)ac.getBean("person");
person.palyAnimal();
}
}
运行结果:
二月 09, 2019 5:31:01 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@46f7f36a: startup date [Sat Feb 09 17:31:01 CST 2019]; root of context hierarchy
二月 09, 2019 5:31:01 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
喵喵喵

如果不想养猫了,只需将beans.xml文件中的ref修改下即可。

1.1(Spring学习笔记)Spring基础(BeanFactory、ApplicationContext 、依赖注入)的更多相关文章

  1. Spring学习笔记--spring+mybatis集成

    前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...

  2. Spring学习笔记—Spring之旅

    1.Spring简介     Spring是一个开源框架,最早由Rod Johnson创建,并在<Expert One-on-One:J2EE Design and Development> ...

  3. Spring 学习笔记 IoC 基础

    Spring IoC Ioc 是什么 IoC -- Inversion of Control(控制反转)什么是控制?什么是反转? 控制反转了什么? 在很早之前写项目不用 Spring 的时候,都是在 ...

  4. Spring学习笔记1——基础知识 (转)

    1.在java开发领域,Spring相对于EJB来说是一种轻量级的,非侵入性的Java开发框架,曾经有两本很畅销的书<Expert one-on-one J2EE Design and Deve ...

  5. Spring学习笔记1——基础知识

    1.在java开发领域,Spring相对于EJB来说是一种轻量级的,非侵入性的Java开发框架,曾经有两本很畅销的书<Expert one-on-one J2EE Design and Deve ...

  6. Spring学习笔记之基础、IOC、DI(1)

    0.0 Spring基本特性 Spring是一个开源框架:是基于Core来架构多层JavaEE系统 1.0 IOC 控制反转:把对象的创建过程交给spring容器来做. 1.1 application ...

  7. Spring学习笔记--Spring IOC

    沿着我们上一篇的学习笔记,我们继续通过代码学习IOC这一设计思想. 6.Hello类 第一步:首先创建一个类Hello package cn.sxt.bean; public class Hello ...

  8. Spring学习笔记--Spring配置文件和依赖注入

    Spring配置文件 1.alias:设置别名,为bean设置别名,并且可以设置多个别名; <!-- 设置别名 --> <alias name="user" al ...

  9. Spring学习笔记-Spring之旅-01

    使用Spring简化JAVA开发 Spring的四种关键策略: ●基于POJO的轻量级和最小侵入式编程: ●通过依赖注入(DI)和面向接口实现松耦合: ●基于切面(AOP)和惯例进行声明式编程. ●通 ...

  10. Spring学习笔记——Spring依赖注入原理分析

    我们知道Spring的依赖注入有四种方式,各自是get/set方法注入.构造器注入.静态工厂方法注入.实例工厂方法注入 以下我们先分析下这几种注入方式 1.get/set方法注入 public cla ...

随机推荐

  1. is

    MyType a = null; if (a is MyType) == False

  2. java实现ssm框架的crud

    上一篇博客写了通过表名获取数据库表结构的demo,现在我以此为基础实现了一个简单的通过数据库表结构生成对应的实体,通过读取mapper接口文件.mapping映射文件. service映射文件模板,替 ...

  3. [ZOJ2341]Reactor Cooling解题报告|带上下界的网络流|无源汇的可行流

    Reactor Cooling The terrorist group leaded by a well known international terrorist Ben Bladen is bul ...

  4. [bzoj3238][Ahoi2013]差异——后缀自动机

    Brief Description Algorithm Design 下面给出后缀自动机的一个性质: 两个子串的最长公共后缀,位于这两个串对应的状态在parent树上的lca状态上.并且最长公共后缀的 ...

  5. 自己申请了苹果的ID号,如何输入到平板上,从而换掉原先的其他账号呢?

    刚买了Ipad平板电脑,一直是用商家给我设置的ID,但是时间一长,我希望用自己的ID来玩我的平板,便于下载程序,更新程序,不用每次去问人家密码是多少. 申请IPAD ID 的网站是:http://ww ...

  6. VC6.0显示行号的插件

    VC6.0显示行号的插件,很好很强大的显行号插件,使用VC编程的朋友再也不用烦恼VC6.0没有行号的编程环境了. VC显示行号插件使用说明:1. 如果你的VC安装在C盘,请拷贝文件VC6LineNum ...

  7. 【Mysql优化】索引碎片与维护

    在长期的数据更改过程中, 索引文件和数据文件,都将产生空洞,形成碎片.(不停的删除修改导致) 解决办法: (1)我们可以通过一个nop操作(不产生对数据实质影响的操作), 来修改表. 比如: 表的引擎 ...

  8. pinctrl框架

    pinctrl框架是linux系统为统一各SOC厂家pin管理,目的是为了减少SOC厂家系统移植工作量. 通常通过设备树初始化pinctrl,并提供调用io接口,以下为全志A64平台的实例: 在dri ...

  9. Log4Net的WebApplication使用

    一.Log4Net的WebApplication使用 1.首先使用nuget 添加log4Net 到WebApplication项目中 log4j每个符号的具体含义:%d %5p %c{1}:%L - ...

  10. js的变量的有效域

    function test(o) { var i=0; if(typeof o=="object") { var j=1; for(var k=0;k<10;k++) { c ...