1、基本定义

IOC: 其思想是反转资源获取的方向,传统的资源查找方式要求组件向容器发起请求查找资源,作为回应,容器适时的返回资源,而应用了

IOC之后,容器主动将资源推送给它所管理的组件,组件索要做的只是选择一种合适的方式接受资源,这种行为也被称为查找的被动形式。

DI: IOC 的另一种表述方式,即组件以一些预定好的方式(例如Action中属性的setter方法)接受来自容器的资源注入

2、原理

3、Bean的配置

Bean:通过全类名(反射)、通过工厂方法(静态工厂方法&实例工厂方法)、FactoryBean

IOC容器:BeanFactory& ApplicationContext 概述

依赖注入的方式:属性注入、构造器注入

3.1、IOC容器的实现

   Spring提供了两种类型的IOC容器

     BeanFactory : IOC容器的基本实现,面向Spring本身。

   ApplicationContext : 提供了 更多的高级特性,是BeanFactory的子接口,面向开发者。

              主要实现类 {ClassPathXmlApplicationContext : 从类路径下加载文件,FileSystemXmlApplicationContext : 从文件系统中加载配置文件}

        //1、创建spring 的 ioc 容器,配置文件在类路径下
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //2、ioc 容器中获取 Bean实例,利用id 定位到IOC容器中的bean
HelloWorld helloWorld = (HelloWorld)ctx.getBean("helloWorld");
    <bean id="helloWorld" class="com.spring.beans.HelloWorld">
<property name="name" value="spring"></property>
</bean>

  3.2、依赖注入的方式

   3.2.1 属性注入<property>

      通过setter方法注入Bean的属性值或者依赖的对象

   3.2.2 构造器注入<constructor-arg>

      通过构造方法注入Bean的属性值或依赖的对象,保证了Bean实例在实例化后就可以使用<constructor-arg>  

 4、bean属性值详解

4.1、出现特殊字符时:  

        <constructor-arg>
<value><![CDATA[<Audi>]]></value>
</constructor-arg>

  4.2、ref 建立bean之间的引用关系 :

    <bean id="car_ref" class="com.spring.beans.Car">
<constructor-arg>
<value><![CDATA[<Audi>]]></value>
</constructor-arg>
<constructor-arg value="ShangHai"></constructor-arg>
<constructor-arg value="300000"></constructor-arg>
</bean> <bean id="person" class="com.spring.beans.Person">
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<property name="car" ref="car_ref"></property>
</bean>

   4.3、内部bean,无法被外部bean引用

    <bean id="person" class="com.spring.beans.Person">
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<property name="car">
<bean class="com.spring.beans.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="ChangAn"></constructor-arg>
<constructor-arg value="20000"></constructor-arg>
</bean>
</property>
</bean>

   4.4、null值和级联属性

 null值

    <bean id="person2" class="com.spring.beans.Person">
<constructor-arg value="jerry"></constructor-arg>
<constructor-arg value="23"></constructor-arg>
<constructor-arg><null/></constructor-arg>
</bean>

   级联属性    

<bean id="car_ref" class="com.spring.beans.Car">       
     <constructor-arg>
<value><![CDATA[<Audi>]]></value>
</constructor-arg>
<constructor-arg value="ShangHai"></constructor-arg>
<constructor-arg value="300000"></constructor-arg>
</bean> <bean id="person2" class="com.spring.beans.Person">
<constructor-arg value="jerry"></constructor-arg>
<constructor-arg value="23"></constructor-arg>
<constructor-arg ref="car_ref"></constructor-arg>
     //必须在bean文件中,定义maxSpeed的set方法【属性注入!】
    <property name="car.maxSpeed"></property>
</bean>   

 集合属性

   <bean id="car_ref" class="com.spring.beans.collections.Car">
<constructor-arg>
<value><![CDATA[<Audi>]]></value>
</constructor-arg>
<constructor-arg value="ShangHai"></constructor-arg>
<constructor-arg value="300000"></constructor-arg>
</bean> <bean id="car2_ref" class="com.spring.beans.collections.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="beijing"></constructor-arg>
<constructor-arg value="20000"></constructor-arg>
</bean> <bean id="car3_ref" class="com.spring.beans.collections.Car">
<constructor-arg value="Honda"></constructor-arg>
<constructor-arg value="beijing"></constructor-arg>
<constructor-arg value="120000"></constructor-arg>
</bean> <!-- 测试如何配置集合属性 -->
<bean id="testCollections" class="com.spring.beans.collections.Person" >
<property name="name" value="Mike"></property>
<property name="age" value="27"></property>
<property name="cars">
<list>
<ref bean="car_ref"></ref>
<ref bean="car2_ref"></ref>
<ref bean="car3_ref"></ref>
</list>
</property>
</bean>
package com.spring.beans.collections;

import java.util.List;

public class Person {
private String name; private int age; private List<Car> cars; public Person(String name, int age, List<Car> cars) {
super();
this.name = name;
this.age = age;
this.cars = cars;
} @Override
public String toString() {
return "Person [age=" + age + ", cars=" + getCarStr(cars) + ", name=" + name + "]";
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public Person() { } public List<Car> getCars() {
return cars;
} public void setCars(List<Car> cars) {
this.cars = cars;
} //获取汽车
public String getCarStr(List<Car> cars)
{
StringBuffer sb = new StringBuffer();
sb.append("[");
for(Car car :cars){
sb.append("brand: "+car.getBrand()+ "corp:"+ car.getCorp() +"maxSpeed:" +car.getMaxSpeed()+"price:"+car.getPrice()+"\n");
}
sb.append("]");
return sb.toString();
}
}
package com.spring.beans.collections;

public class Car {
private String brand;
private String corp;
private String price;
private String maxSpeed; //通过构造器注入
public Car(String brand, String corp, String price) {
super();
this.brand = brand;
this.corp = corp;
this.price = price;
} public String getBrand() {
return brand;
} public void setBrand(String brand) {
this.brand = brand;
} public String getCorp() {
return corp;
} public void setCorp(String corp) {
this.corp = corp;
} public String getPrice() {
return price;
} public void setPrice(String price) {
this.price = price;
} public String getMaxSpeed() {
return maxSpeed;
} public void setMaxSpeed(String maxSpeed) {
this.maxSpeed = maxSpeed;
} }

集合属性-2 【map】

    <bean id="testMap" class="com.spring.beans.collections.newPerson">
<property name="name" value="java"></property>
<property name="age" value="30"></property>
<property name="cars">
<map>
<entry key="A1" value-ref="car_ref"></entry>
<entry key="A2" value-ref="car2_ref"></entry>
<entry key="A3" value-ref="car3_ref"></entry>
</map>
</property>
</bean>

集合属性-3【properties】

    <bean id="dataSource" class="com.spring.beans.collections.DataSource">
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">root</prop>
<prop key="jdbcUrl">jdbc:mysql:///test</prop>
<prop key="driverClass">com.mysql.jdbc.drivce</prop>
</props>
</property>
</bean>

p标签

    <bean id="person5" class="com.spring.beans.collections.Person"
p:name="queen" p:age="30" p:cars-ref="cars"> </bean>

配置单例的集合bean

    <!-- 配置单例的集合bean,以供多个bean进行引用,需要导入util命名空间 -->
<util:list id="cars">
<ref bean="car_ref"></ref>
<ref bean="car2_ref"></ref>
<ref bean="car3_ref"></ref>
</util:list> <bean id="person4" class="com.spring.beans.collections.Person">
<property name="name" value="jack"></property>
<property name="age" value="30"></property>
<property name="cars" ref="cars"></property>
</bean>

Spring4.0学习笔记(1) —— 基础知识的更多相关文章

  1. MyBatis:学习笔记(1)——基础知识

    MyBatis:学习笔记(1)--基础知识 引入MyBatis JDBC编程的问题及解决设想 ☐ 数据库连接使用时创建,不使用时就释放,频繁开启和关闭,造成数据库资源浪费,影响数据库性能. ☐ 使用数 ...

  2. C#学习笔记(基础知识回顾)之值类型与引用类型转换(装箱和拆箱)

    一:值类型和引用类型的含义参考前一篇文章 C#学习笔记(基础知识回顾)之值类型和引用类型 1.1,C#数据类型分为在栈上分配内存的值类型和在托管堆上分配内存的引用类型.如果int只不过是栈上的一个4字 ...

  3. Quartz学习笔记:基础知识

    Quartz学习笔记:基础知识 引入Quartz 关于任务调度 关于任务调度,Java.util.Timer是最简单的一种实现任务调度的方法,简单的使用如下: import java.util.Tim ...

  4. C#学习笔记(基础知识回顾)之值传递和引用传递

    一:要了解值传递和引用传递,先要知道这两种类型含义,可以参考上一篇 C#学习笔记(基础知识回顾)之值类型和引用类型 二:给方法传递参数分为值传递和引用传递. 2.1在变量通过引用传递给方法时,被调用的 ...

  5. C#学习笔记(基础知识回顾)之值类型和引用类型

    一:C#把数据类型分为值类型和引用类型 1.1:从概念上来看,其区别是值类型直接存储值,而引用类型存储对值的引用. 1.2:这两种类型在内存的不同地方,值类型存储在堆栈中,而引用类型存储在托管对上.存 ...

  6. .net学习笔记---xml基础知识

    一.XML简介 XML是一种标记语言,用于描述数据,它提供一种标准化的方式来来表示文本数据.XML文档以.xml为后缀.需要彻底注意的是XML是区分大小写的. 先从一个简单的XML例子来了解下xml基 ...

  7. Docker的学习笔记(一)基础知识

    概述 本人最近在学习docker相关的知识,既是工作本身的需要也是自己对技术的追求的必要,以后我也会推出容器相关的随笔,既可以增长自己的知识,也可以和读者广泛交流,岂不乐乎?话不多说.第一篇先介绍do ...

  8. Scala学习笔记--正则表达式基础知识、如何在scala内使用

    正则表达式语法:https://msdn.microsoft.com/zh-cn/library/ae5bf541(VS.80).aspx 基础知识:正则表达式30分钟入门教程 http://www. ...

  9. (C/C++学习笔记) 一. 基础知识

    一. 基础知识 ● 程序和C/C++ 程序: 根据Wirth (1976), Algorithms + Data Structures = Programs. Whence C: 1972, Denn ...

随机推荐

  1. 解决mongodb ISODate相差8小时问题

    服务端使用mongoose操作mongodb,其中Schema中的日期字段定义如下: date: {type:Date, default:Date.now},//操作日期 插入到mongodb中adt ...

  2. KEIL C51中的_at_关键字

    绝对位置变量 变量可以在你的C程序中的绝对内存地址位于源模块使用_at_关键字.此功能的用法是: 类型 _ memory_space _ 变量名 _at _  常数 ; 其中:memory_space ...

  3. 双向队列 STL

    题目描述 想想双向链表……双向队列的定义差不多,也就是说一个队列的队尾同时也是队首:两头都可以做出队,入队的操作.现在给你一系列的操作,请输出最后队列的状态:命令格式:LIN X  X表示一个整数,命 ...

  4. POJ1080 Human Gene Functions(LCS)

    题目链接. 分析: 和 LCS 差不多. #include <iostream> #include <cstdio> #include <cstdlib> #inc ...

  5. UOJ #78 二分图最大匹配

    #78. 二分图最大匹配 从前一个和谐的班级,有 nl 个是男生,有 nr 个是女生.编号分别为 1,…,nl 和 1,…,nr. 有若干个这样的条件:第 v 个男生和第 u 个女生愿意结为配偶. 请 ...

  6. HDU 1254 推箱子 BFS

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1254 题目分析: 做这道题,感觉挺简单的,做着做着就错了20次, 我也是醉了, WA到吐的节奏啊! 思 ...

  7. oracle事务和锁

    数据库事务概括 1. 说明 一组SQL,一个逻辑工作单位,执行时整体修改或者整体回退. 2.事务相关概念 1)事务的提交和回滚:COMMIT/ROLLBACK 2)事务的开始和结束 开始事务:连接到数 ...

  8. HDOJ 1016 Prime Ring Problem素数环【深搜】

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, -, ...

  9. Controlling How NSThread and NSRunLoop Exit

    http://shaheengandhi.com/controlling-thread-exit/ While most concurrency can be dealt with by using ...

  10. weblogic Connection has already been closed解决方法

    今天正式环境下的有一个功能报错,看了下weblogic日志,报连接已经关闭. com.ibatis.common.jdbc.exception.NestedSQLException: --- The ...