引言 工作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 ? <?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> <?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 ? <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> <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 ? ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {“services.xml”, “daos.xml”}); ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"}); 我们写这一句简单的代码,其实Springdydyx做了很多功夫。 首先Spring会从classpath路径下找到services.xml和daos.xml文件, 然后将文件内的配置信息读取出来, 然后做了N多内部的初始化工作(关于容器初始化的详细细节,后面我会补充一篇文章从源码角度详细说明)。
我们还可以用FileSystemApplicationContext来初始化Spring容器, [java] view plain copy print ? ApplicationContext context = new FileSystemXmlApplicationContext(“D:/Test/services.xml”); ApplicationContext context = new FileSystemXmlApplicationContext("D:/Test/services.xml"); 这2中方式效果是一样的,只是通过不同的方式读取配置文件。 容器初始完后,我们就可以用这个容器来获取我们之前配置了个bean,简单示例如下: [java] view plain copy print ? // 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> // create and configure beansApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});// retrieve configured instancePetStoreServiceImpl service = context.getBean("petStore", PetStoreServiceImpl.class);// use configured instanceList 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 ? <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml classpath:resources/services.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml classpath:resources/services.xml</param-value></context-param><listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
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就悄悄开始工作了,至于怎么工作的还需要点篇幅去描述,这篇文件就先不细说。