zoukankan      html  css  js  c++  java
  • Spring基础使用(一)--------IOC、Bean的XML方式装配

    基础

    1、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">
    </beans>

    2、初始化容器方法:

    • 文件的绝对路径:
            FileSystemXmlApplicationContext context1=new FileSystemXmlApplicationContext("C:/configuration.xml")
    • Classpath加载
          ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("configuration.xml");

    IOC

    1、Bean注入的2种方式:

    • set方法注入:通过set方法进行注入。
        <bean id="helloSpring" class="service.HelloSpringImpl">
            <property name="daoImpl" ref="daoImpl"></property>
        </bean>
        <bean id="daoImpl" class="DAO.DAOImpl" ></bean>
    • 构造方法注入:通过构造方法进行注入
        <bean id="helloSpring" class="service.HelloSpringImpl">
            <constructor-arg name="daoImpl" ref="daoImpl"></constructor-arg>
        </bean>
        <bean id="daoImpl" class="DAO.DAOImpl" ></bean>

    Bean的XML装配

    1、Bean的作用域

    • singleton:单例模式
    • prototype:原型模式
    • request:当前request内有效
    • session:当前session内有效
    • global session:当前global session内有效

    2、Bean初始化和销毁方法

    初始化方法:

    • 实现InitializingBean接口,重写afterPropertiesSet()方法
    • 在xml文件中,bean定义的地方指定init-method

    销毁方法:

    • 实现DisposableBean接口,重写destroy()方法
    • 在xml文件中,bean定义的地方指定destroy-method

    全局初始化和销毁:

    通过default-init-methoddefault-destroy-method关键字进行指定。

    <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"
            default-init-method="functionInit" default-destroy-method="functionDestroy">

    3、Aware

    如果类实现了ApplicationContextAware接口,则需要重写接口中的setApplicationContext方法,在IOC容器进行初始化之后,会自动调用setApplicationContext方法。setApplicationContext方法的参数就是IOC容器对象本身。

    如果类实现了BeanNameAware接口,则需要重写接口中的setBeanName方法,在Bean实例化的时候,会自动调用setBeanName方法。

    还有其他的Aware接口:

    • BeanFactoryAware:获得当前bean Factory,从而调用容器的服务
    • MessageSourceAware:得到message source从而得到文本信息
    • ApplicationEventPublisherAware:应用时间发布器,用于发布事件
    • ResourceLoaderAware:获取资源加载器,可以获得外部资源文件

    4、Bean的自动装配(4种类型)

    在xml文件中,通过default-autowire字段进行指定装配模式。而不需要在bean中使用property或者constructor-arg

    <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"
            default-autowire="byName">
    • No:不做动作
    • byName:根据类的成员的名称,从容器中进行匹配并装配。需要set函数支持。
    • byType:根据类的成员的类型,从容器中进行匹配并装配。需要set函数支持。如果存在多个同类型对象,则抛出异常。
    • Constructor:根据类的成员的类型,从容器中进行匹配并装配。需要构造函数支持。

    5、Resources:实现对底层资源的访问

    Spring内置6种Resource类型:UrlResource,ClassPathResource,FileSystemResource,ServletContextResource,InputStreamResource,ByteArrayResource

    • UrlResource:URL对应的资源
    • ClassPathResource:类路径下的资源
    • FileSystemResource:文件系统资源
    • ServletContextResource:ServletContext对应资源
    • InputStreamResource:输入流资源
    • ByteArrayResource:字节数组资源

    8、ResourceLoader接口:资源加载器

    ResourceLoader是一个用于资源加载的接口。Spring中的ApplicationContext实现了ResourceLoader接口,所以可以通过ApplicationContext进行实际资源的加载。

    加载方式有4种:

    • classpath前缀:从classpath中获取对应的资源文件
    • file前缀:从文件系统中获取对应的资源文件,需要绝对路径
    • http前缀:从网络中获取对应的资源文件
    • 无前缀:从ApplicationContext所在目录中获取对应的资源文件


  • 相关阅读:
    Oracle优化器:星型转换
    Latches and Tuning:Buffer Cache
    [转]Oracle销售人员普遍腐败?
    Veritas ODM Async and Oracle
    How to set LOGSOURCE PATHMAP on GoldenGate
    Merry Christmas Sql Statement
    Goldengate OGG常见问题与错误列表
    Sqlserver 2005 配置 数据库镜像:1418错误:该错误很可能是安全方面的配置问题引起的
    Sqlserver 2005 配置 数据库镜像:为镜像准备镜像数据库 (TransactSQL)
    Sqlserver 2005 配置 数据库镜像:概述
  • 原文地址:https://www.cnblogs.com/kkdn/p/8985001.html
Copyright © 2011-2022 走看看