Spring用一种非入侵的方式来管理程序,模块结构图如下:

 

.Core层

IOC(控制反转):应用本身程序不负责依赖对象的创建及维护,依赖对象的创建及维护有外设容器负责,即:IOC;

DI(依赖注入):程序运行期间,外部容器动态的将依赖对象注入到另外的对象中,DI中强调的是注入方式;

与Core应用相关的jar包:commons-logging.jar、spring-beans.jar 、 spring-context.jar 、 spring-core.jar,其中commons-logging.jar是为了打印日志;

 

.启动IOC容器

1、AppliactionContext ac = new ClassPathXmlApplicationContext(new String[]{………..});

    类路径下加指定的Xml文件,其中参数为字符串数组,说明可同时加载多个路径下的XML文件;

2、ApplicationCOntext ac = new FileSystemXmlApplicationContext(new String[]{……..});

    文件路径下加载指定的Xml文件,其中参数为字符串数组,说明可同时加载多个路径下的XML文件

AppliactionContext被定义为接口,可视为制造对象的统一接口,XML文件加载后实例化的对象在内存中Key-Value的结构存储(Map),其中的Key为Xml文件中配置的对象ID(ID定义必须唯一);

<?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-2.5.xsd">

    <bean id="testID" class="cn.chenx.Test"/>

</beans>

 

public class TestSpring {
    public static void main(String[] args) {
        //启动springIOC容器
        ApplicationContext ac =
            new ClassPathXmlApplicationContext(new String[]{".../spring.xml"});
        Test test = (Test)ac.getBean("testID");
    }
}

 

.XML文件解析过程

加载指定路径下的 XML文件;

创建XML文件中配置的所有<bean />标签中指定类的对象,并加载进内存形成id与对象的键值对;

当<bean />标签下存在<property />标签时,接下该标签中的配置数据,并赋值给创建对象的对应属性字段;

如果<bean />标签之存在关系,设置对应的关系(这里的关系指对象之间引用);

 

.Bean的创建方式

1、无参构造创建对象(默认):

<bean id="UnitID" class="cn.chenx.spring.pojo.TestUnit"/>

class的值表示需要实例化类的类路径去掉.java后缀;

2、有参构造,通过辅助类静态方法创建对象:

<bean id="UnitID" class="cn.chenx.spring.pojo.HelpUnit" factory_method="createTestUnit"/>

class的值表示TestUnit的辅助类HelpUnit,createTestUnit为辅助类的静态方法,并返回TestUnit对象;

3、有参构造,通过辅助类非静态方法创建对象:

<bean id="HelpID" class="cn.chenx.spring.pojo.HelpFactory" />

<bean id="UnitID" factory_bean="HelpID" factory_method="createTestUnit"/>

HelpID表示辅助类ID,UnitID表示需创建对象的ID,createTestUnit为辅助的成员方法(非静态);

.Bean的生命周期

Bean的初始化和销毁的监听使用AbstractApplicationContext工厂类;

<bean id="UnitID" class="cn.chenx.spring.pojo.TestUnit"
          init-method="init"
          destroy-method="destroy"/>

init-method指定的方法在Bean对象初始化时调用,destroy-method指定的方法在Bean对象销毁是调用;

相关函数调用:

AbstractApplicationContext.close()立即销毁;

AbstractApplicationContext.registerShutDownHook延迟销毁,Java虚拟机停止时调用;

.Spring中的单例模式与原型模式

1、单例模式

<bean id="UnitID" class="cn.chenx.spring.pojo.TestUnit" scope="singleton" />

2、原型模式(默认)

<bean id="UnitID" class="cn.chenx.spring.pojo.TestUnit" scope="prototype" />

.Bean懒值实例化配置

<bean id="UnitID" class="cn.chenx.spring.pojo.TestUnit" lazy-init="true" />

初始化将被推迟,直到首次对它取值才实例化 , 默认情况下lazy-init="false",表示springIOC容器启动时,<bean />标签将被实例化;

.Bean继承关系配置

<bean id="parentID" class="cn.chenx.spring.pojo.ParentUnit">
    <property name="name" value="张三"/>
    <property name="sex"  value="男"/>
</bean>

<bean id="sonID" class="cn.chenx.spring.pojo.TestUnit" parent="parentID">
    <property name="name"     value="张五"/>
</bean>

父子关系通过 parent="parentID" 字段配置指定;

 

.Bean属性配置

1、构造器方式

Unit.java

public class Unit {
    private String name;
    private String value;
  

    public Unit(){

    }

    public Unit(String name,String value){
        this.name = name;
        this.value = value;
    }

}

spring.xml

<bean id="unitID" class="pojo.Unit">
    <constructor-arg index="0">
        <value>张三</value>
    </constructor-arg>
    <constructor-arg index="1">
        <value>5000</value>
    </constructor-arg>
</bean>

注:Unit的两个构造函数都必须存在,XML文件中 index的值,对应的是Unit类中有参构造函数中参数前后顺序类似数组的下标访问,如:name的下标为0,value下标为1;

 

2、set方式注入

Unit.java

public class Bean {
    private String name;
    private String info;

    //对应字段的get和set方法

}

public class Unit {
    private String uName;
    private Date uDate;
    private Set<String>  uSet;
    private List<String> uList;
    private Map<String, String> uMap;
    private Set<Bean>  beanSet;
    private List<Bean> beanList;
    private Map<Bean, Bean> beanMap;

    // 对应各个字段的get和set方法

}

spring.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-2.5.xsd">

        <!-- Bean类中基本数据类型或包装数据类型字段name与info的设置 -->
        <bean id="beanId1" class="ioc.Bean">
            <property name="name" value="李一"></property>
            <property name="info" value="5000"></property>
        </bean>

        <bean id="udateId" class="java.util.Date" ></bean>

        <bean id="unitId" class="ioc.Unit" >
            <!-- Unit类中基本数据类型或包装数据类型字段uName值的设置 -->
            <property name="uName" value="张三" />

            <!-- Unit类中引用数据类型字段uDate值的设置 -->
            <property name="uDate" ref="udateId" />

            <!-- Unit类中Set集合数据类型字段uSet值的设置,且集合成员数据类型为基本数据类型 -->
            <property name="uSet" >
                <set>
                    <value>小学</value>
                    <value>中学</value>
                    <value>高中</value>
                    <value>大学</value>
                </set>
            </property>

            <!-- Unit类中List集合数据类型字段uList值的设置,且集合成员数据类型为基本数据类型 -->
            <property name="uList">
                <list>
                    <value>少先队员</value>
                    <value>共青团员</value>
                    <value>中共党员</value>
                    <value>毕业入职</value>
                </list>
            </property>

            <!-- Unit类中Map集合数据类型字段uMap值的设置,且集合成员数据类型为基本数据类型 -->
            <property name="uMap">
                <map>
                    <entry key="小学" value="少先队员"></entry>
                    <entry key="中学" value="共青团员"></entry>
                    <entry key="高中" value="中共党员"></entry>
                    <entry key="大学" value="毕业入职"></entry>
                </map>
            </property>

            <!-- Unit类中Set集合数据类型字段beanSet值的设置,且集合成员数据类型为引用数据类型 -->
            <property name="beanSet">
                <set>
                    <ref bean="beanId1" />
                </set>
            </property>

            <!-- Unit类中List集合数据类型字段beanList值的设置,且集合成员数据类型为引用数据类型 -->
            <property name="beanList">
                <list>
                    <ref bean="beanId1" />
                </list>
            </property>

            <!-- Unit类中Map集合数据类型字段beanMap值的设置,且集合成员数据类型为引用数据类型 -->
            <property name="beanMap">
                <map>
                    <entry key-ref="beanId1">
                        <ref bean="beanId1" />
                    </entry>
                </map>
            </property>
        </bean>

</beans>

Core层开发所需Jar包(Spring3.2):

asm-3.2.0.RELEASE.jar

com.springsource.org.apache.commons.logging-1.1.1.jar

spring-beans-3.2.0.RELEASE.jar

spring-context-3.2.0.RELEASE.jar

spring-core-3.2.0.RELEASE.jar

spring-expression-3.2.0.RELEASE.jar

spring-test-3.2.0.RELEASE.jar

Spring笔记(二)Core层的更多相关文章

  1. spring笔记(二)

    共性问题: 1. 服务器启动报错,什么原因? * jar包缺少.jar包冲突 1) 先检查项目中是否缺少jar包引用 2) 服务器: 检查jar包有没有发布到服务器下: 用户库jar包,需要手动发布到 ...

  2. spring笔记二

    DI—Dependency Injection,即“依赖注入”:组件之间依赖关系由容器在运行期决定,形象的说,即由容器动态的将某个依赖关系注入到组件之中.依赖注入的目的并非为软件系统带来更多功能,而是 ...

  3. spring cloud+dotnet core搭建微服务架构:服务发现(二)

    前言 上篇文章实际上只讲了服务治理中的服务注册,服务与服务之间如何调用呢?传统的方式,服务A调用服务B,那么服务A访问的是服务B的负载均衡地址,通过负载均衡来指向到服务B的真实地址,上篇文章已经说了这 ...

  4. spring cloud+.net core搭建微服务架构:服务发现(二)

    前言 上篇文章实际上只讲了服务治理中的服务注册,服务与服务之间如何调用呢?传统的方式,服务A调用服务B,那么服务A访问的是服务B的负载均衡地址,通过负载均衡来指向到服务B的真实地址,上篇文章已经说了这 ...

  5. Spring笔记01_下载_概述_监听器

    目录 Spring笔记01 1.Spring介绍 1.1 Spring概述 1.2 Spring好处 1.3 Spring结构体系 1.4 在项目中的架构 1.5 程序的耦合和解耦 2. Spring ...

  6. Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven)

    Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven) 本篇和 Spring 没有什么关系,只是学习 Spring,必备一些知识,所以放在这里了. 本篇内容: (1)M ...

  7. Spring笔记(6) - Spring的BeanFactoryPostProcessor探究

    一.背景 在说BeanFactoryPostProcessor之前,先来说下BeanPostProcessor,在前文Spring笔记(2) - 生命周期/属性赋值/自动装配及部分源码解析中讲解了Be ...

  8. jQuery源码笔记(二):定义了一些变量和函数 jQuery = function(){}

    笔记(二)也分为三部分: 一. 介绍: 注释说明:v2.0.3版本.Sizzle选择器.MIT软件许可注释中的#的信息索引.查询地址(英文版)匿名函数自执行:window参数及undefined参数意 ...

  9. Git 笔记二-Git安装与初始配置

    git 笔记二-Git安装与初始配置 Git的安装 由于我日常生活和工作基本上都是在Windows上,因此此处只说windows上的安装.Windows上的安装和其他程序一样,只需要到http://g ...

随机推荐

  1. 整理sed实战修改多行配置技巧

    老男孩老师有关sed实战技巧分享,来自课堂教学内容实战1.在指定行前插入两行内容,分别为oldboy和oldgirl.提示:被修改的文件内容必须要大于等于2行 1 sed -i '2 ioldboy\ ...

  2. bzoj 4127: Abs 树链剖分

    4127: Abs Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 11  Solved: 5[Submit][Status][Discuss] Des ...

  3. PHP漏洞全解(五)-SQL注入攻击

    本文主要介绍针对PHP网站的SQL注入攻击.所谓的SQL注入攻击,即一部分程序员在编写代码的时候,没有对用户输入数据的合法性进行判断,使应用程序存在安全隐患.用户可以提交一段数据库查询代码,根据程序返 ...

  4. Javascript编程模式(JavaScript Programming Patterns)Part 2.(高级篇)

    模块编程模式的启示(Revealing Module Pattern) 客户端对象(Custom Objects) 懒函数定义(Lazy Function Definition) Christian  ...

  5. 一个用于清除loadrunner产生log文件的批处理

    @echo off set work_path="%~dp0" for /R %%s in (*.txt,*.log) do ( del /f "%%s" ) ...

  6. 去除Coding4Fun中MessagePrompt的边框(Border)

    在App.xaml文件中添加 xmlns:c4f="clr-namespace:Coding4Fun.Toolkit.Controls;assembly=Coding4Fun.Toolkit ...

  7. Android 图片从网页中获取并动态加载到listview中

    实现功能: 效果图: 代码:这里

  8. Android ExpandableListView使用+获取SIM卡状态信息

    ExpandableListView 是一个可以实现下拉列表的控件,大家可能都用过QQ,QQ中的好友列表就是用ExpandableListView实现的,不过它是自定义的适配器.本篇 博客除了要介绍E ...

  9. 打死也不换系统?笑谈过气的Windows XP

    http://tech.qq.com/a/20131012/007336.htm 按照IT领域的“安迪-比尔定律”:软件和游戏不断生成过户需求,硬件则通过技术创新来消化这些需求,这个过程会刺激用户在电 ...

  10. Java 输入流读取文本文件换行符问题

    一问题 在学习流编程的过程中,我遇到了一下问题.首先来看一下我写的java源程序: package StreamLearn; import java.io.*; public class TestFi ...