Spring容器是什么

引言


工作4年多,做了3年的java,每个项目都用Spring,但对Spring一直都是知其然而不知其所以然。鄙人深知Spring是一个高深的框架,正好近期脱离加班的苦逼状态,遂决定从Spring的官方文档入手,结合Spring代码和实际项目,全面的学习Spring,并将学习内容记录在博客上,以便以后查阅并可以和众猿讨论分享。PS:文章中会有部分是官方原句翻译,例子也会有官方例子。

概述


Spring容器是什么?

Spring容器是Spring的核心,一切Spring bean都存储在Spring容器内,并由其通过IoC技术管理。Spring容器也就是一个bean工厂(BeanFactory)。应用中bean的实例化,获取,销毁等都是由这个bean工厂管理的。

Spring容器究竟是什么。。。

org.springframework.context.ApplicationContext接口用于完成容器的配置,初始化,管理bean。一个Spring容器就是某个实现了ApplicationContext接口的类的实例。也就是说,从代码层面,Spring容器其实就是一个ApplicationContext。

在普通的JAVA工程中,我们可以通过代码显式new一个ClassPathXmlApplicationContext或者FileSystemXmlApplicationContext来初始化一个Spring容器。

在Web工程中,我们一般是通过配置web.xml的方式来初始化Spring容器。

Spring配置文件


Spring配置文件以XML格式呈现,如下:

[html] view plain copy print ?

  1. <xmlversion=“1.0”encoding=“UTF-8”><beansxmlns=“http://www.springframework.org/schema/beans”

  2. xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

  3. xsi:schemaLocation=”http://www.springframework.org/schema/beans

  4. http://www.springframework.org/schema/beans/spring-beans.xsd”>

  5. <beanid=“…”class=“…”>

  6. <!–collaboratorsandconfigurationforthisbeangohere–>

  7. </bean>

  8. <beanid=“…”class=“…”>

  9. <!–collaboratorsandconfigurationforthisbeangohere–>

  10. </bean>

  11. <!–morebeandefinitionsgohere–>

  12. </beans>

<?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">

  <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>

在eclipse中,我们可以使用SpringSource Tool Suite工具来协助我们编写Spring的配置文件。

工程中允许有多个Spring配置文件,我们可以按照自己的需求定义文件的分类,例如可以从处理逻辑层级来区分:services.xml,daos.xml等。

另外,一个配置文件可以引入多个其他的配置文件,写法如下:

[html] view plain copy print ?

  1. <beans>

  2. <importresource=“services.xml”/>

  3. <importresource=“resources/messageSource.xml”/>

  4. <importresource=“/resources/themeSource.xml”/>

  5. <beanid=“bean1”class=“…”/>

  6. <beanid=“bean2”class=“…”/>

  7. </beans>

  8. <strong>

  9. <spanstyle=“font-family:MicrosoftYaHei;”></span></strong>

<beans>

    <import resource="services.xml"/>
    <import resource="resources/messageSource.xml"/>
    <import resource="/resources/themeSource.xml"/>

    <bean id="bean1" class="..."/>
    <bean id="bean2" class="..."/>

</beans>
<strong>
<span style="font-family:Microsoft YaHei;"></span></strong>

通过ClassPathApplicationContext初始化Spring容器


[java] view plain copy print ?

  1. ApplicationContextcontext=

  2. newClassPathXmlApplicationContext(newString[]{“services.xml”,“daos.xml”});

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

我们写这一句简单的代码,其实Spring在里面做了很多功夫。

首先Spring会从classpath路径下找到services.xml和daos.xml文件,

然后将文件内的配置信息读取出来,

然后做了N多内部的初始化工作(关于容器初始化的详细细节,后面我会补充一篇文章从源码角度详细说明)。

我们还可以用FileSystemApplicationContext来初始化Spring容器,

[java] view plain copy print ?

  1. ApplicationContextcontext=

  2. newFileSystemXmlApplicationContext(“D:/Test/services.xml”);

    ApplicationContext context =
    new FileSystemXmlApplicationContext(“D:/Test/services.xml”);

这2中方式效果是一样的,只是通过不同的方式读取配置文件。

容器初始完后,我们就可以用这个容器来获取我们之前配置了个bean,简单示例如下:

[java] view plain copy print ?

  1. //createandconfigurebeans

  2. ApplicationContextcontext=

  3. newClassPathXmlApplicationContext(newString[]{“services.xml”,“daos.xml”});

  4. //retrieveconfiguredinstance

  5. PetStoreServiceImplservice=context.getBean(”petStore”,PetStoreServiceImpl.class);

  6. //useconfiguredinstance

  7. ListuserList=service.getUsernameList();

  8. <spanstyle=”font-family:MicrosoftYaHei;”>

// 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();
<strong>
<span style="font-family:Microsoft YaHei;"></span></strong>

Web工程的Spring配置


假设我们的web工程my-app的基础目录结构如下:

my-app

–src

—-resources

——services.xml

–WebContent

—-META-INF

—-WEB-INFI

——lib

——applicationContext.xml

——web.xml

那么我们的web.xml需要这么,配置方式如下**:**

[html] view plain copy print ?

  1. <context-param>

  2. <param-name>contextConfigLocation</param-name>

  3. <param-value>/WEB-INF/applicationContext.xmlclasspath:resources/services.xml</param-value>

  4. </context-param>

  5. <listener>

  6. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  7. </listener>

    contextConfigLocation /WEB-INF/applicationContext.xml classpath:resources/services.xml org.springframework.web.context.ContextLoaderListener

contextConfigLocation:

contextConfigLocation指的是Spring该去哪里读取配置文件,ContextLoaderListener用于启动的web容器(如tomcat)时,去读取配置文件并完成Spring容器的初始化(包括加载bean等)。

关于contextConfigLocation的配置方式也是可以非常丰富的,还可以使用通配符 * ,这里简单举几个例子说明:

1. classpath:resources/services.xml

表示到web工程的classes/resources文件夹中查找配置文件。

2. classpath*:resources/services.xml

这种方式除了会到classes/resources文件夹查找,还会到lib下面的jar包中查找,查找路径是jar包内/resources/services.xml。

3. classpath:resouces/**/*services.xml

这种方式表示到classpath的resources文件夹下所有文件夹(不限层级,可以在第N层子文件夹中)中查找文件名以services.xml结尾的文件。

4. 多个路径配置可以用空格分开

classpath知识补充:

web工程部署后,对应war包下的WEB-INF下会有一个classes文件夹和一个lib文件,当然还有其他的。

其中classes文件夹中的内容是从工程中的源码文件夹(对应右键工程,Properties - Java Build Path - Source页签中看到的文件夹)中编译过来,lib文件夹即工程中引用的jar包。

这个classes文件夹和lib中的jar都属于classpath。

ContextLoaderListener:

这个Listener就是在标准Spring Web工程中Spring开始干活的切入点,为什么要说标准?因为我们可以写一个自己的Listener去启动Spring容器。扯远了~

因为ContextLoaderListener实现了ServletContextListener,所以在web容器启动时,ContextLoaderListener就悄悄开始工作了,至于怎么工作的还需要点篇幅去描述,这篇文件就先不细说。

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值