zoukankan      html  css  js  c++  java
  • Spring3.0官网文档学习笔记(四)--3.1~3.2.3

    3.1 Spring IoC容器与Beans简单介绍
        BeanFactory接口提供对随意对象的配置;
        ApplicationContext是BeanFactory的子接口。整合了Spring Aop功能,消息资源控制。事件公布,应用层特殊的上下文(在web应用中)
        由IoC容器实例化、组装、管理的对象都是Bean
    3.2 容器概述
        org.springframework.context.ApplicationContext代表Spring IoC容器,而且负责通过读取配置元数据来实例化、配置、组装Bean。配置元数据能够通过三种形式表示:xml,Java凝视,Java code(这个不懂)
        在独立的环境中。实例化ClassPathXmlApplicationContext或FileSystemXmlApplicationContext对象。
    3.2.1 配置元数据
        除了配置在xml文件里,还有两种方法:
        Annotation-based configuration: ?
        Java-based configuration: ?

        你能够使用Spring's integration with AspectJ配置不在IoC容器控制下的对象?

        基于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-3.0.xsd">
    
      <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
      </bean>
    
      <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
      </bean>
    
      <!-- more bean definitions go here -->
    
    </beans>
    3.2.2 实例化一个容器

    ApplicationContext context =
        new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});

       Note:第4章有更方便的机制读取XML文件?

    3.2.2.1 基于XML配置的元数据的组成

    <beans>
    
        <import resource="services.xml"/>
        <import resource="resources/messageSource.xml"/>
        <import resource="/resources/themeSource.xml"/>
    
        <bean id="bean1" class="..."/>
        <bean id="bean2" class="..."/>
    
    </beans>
    services.xml必须位于包括该文件的xml文件同个文件夹或者是在classPath文件夹下。

    能够通过相对路径“../path”这样的方式来配置文件路径,可是这方法并不推荐(用这样的方式创建出来的是依赖文件是在当前应用的外部?)。尤其不推荐用于"classpath:"这样的URL中(执行时解决程序会选择“近期的”classpth,并查看它的父文件夹,导致找不到相应的xml文件)。

    能够使用:"file:C:/config/services.xml"或"classpath:/config/services.xml"。可是要记住假设使用绝对路径的话,就存在耦合问题了。

    3.2.3 使用容器

    // create and configure beans
    ApplicationContext context =
        new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
    
    // retrieve configured instance
    PetStoreServiceImpl service = context.getBean("petStore", PetStoreServiceImpl.class);
    
    // use configured instance
    List userList service.getUsernameList();

  • 相关阅读:
    Nginx和PHP-FPM的启动、重启、停止脚本分享
    [Linux]Fedora19修复被Windows干掉的引导
    [Linux]RHEL/CentOS6配置tomcat使用80端口(与httpd整合)
    [Linux]SAMBA共享打印机
    [Linux]配置Logwatch使用第三方smtp发送电子邮件
    [oVirt]在双网卡网络环境下使用oVirt LiveCD
    走进Linux世界主题讲座纪录
    mysql用户及权限复制
    记一次失败的K8S安装部署
    HTTP状态码与爬虫
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/7295739.html
Copyright © 2011-2022 走看看