Spring笔记(二)Core层

时间:2023-03-08 21:49:00

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

Spring笔记(二)Core层

 

.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