搭建spring的开发环境

在eclipse中新建web项目,集成spring开发环境,把集成spring的过程描述如下,

  1. 从spring官网下载spring的jar包,我这里是spring4.1, 下载的文件中包含了源码及文档,我们挑选出需要的jar包, 一共20个,为了方便我们可以把20个jar全部放进lib目录下
  2. 在web.xml文件中配置spring,这里有两种方式:

直接配置一个listener

如下:

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

这时必须把spring的配置文件加到WEB-INF下, 且名字必须为application-context.xml; 这里是spring默认的路径及默认的配置文件名。

配置context-param属性

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-application.xml</param-value>
</context-param>

此属性指定spring的配置文件的路径, 这里指定为classpath下的spring-application.xml, 这里为src下的spring-application.xml。 contextLoaderListener实现了servletContextListener接口。

如上便可以把spring4.1集成到现有的web项目中。

具体配置文件如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    
    <bean id="address" class="com.cn.test.spring.Address"></bean>
        
</beans>

从上边的配置文件中可以看到,配置了一个bean,其ID为address, 类的全限类名为com.cn.test.spring.Address。 由于使用了spring那么类的创建都由spring来管理, 我们在java代码中不需要使用new关键字创建一个对象, 那么要如何获得一个由spring创建的类的对象呢,下面是本文的重点。

如何从spring环境中获得一个实例对象

要从spring中获得一个类的实例,可以通过spring的上下文ApplicationContext对象, ApplicationtContext是一个接口,也称为IOC容器,另外还有BeanFactory容器。 它有几个比较重要的实现类, ClassPathXmlApplicationContext、FileSystemXmlApplicationContext、XmlWebApplicationContext

ClassPathXmlApplicationContext

从类路径下读取配置文件,即从src下读取spring的配置文件,如下,

ClassPathXmlApplicationContext appContext = new 
    ClassPathXmlApplicationContext("spring-application.xml");
Address address = (Address) appContext.getBean("address");

从类路径下读取了spring-application.xml,然后获得Address的实例对象 这种方式使用于在java项目下获得IOC容器,并取得类的实例。

FileSystemXmlApplicationContext

从文件系统中读取配置文件,这个类用的不多

XmlWebApplicationContext

在jsp或servlet中获得applicationContext容器, 如下是在servlet中获得applicationContext.

@Override
public void init(ServletConfig config) throws ServletException {
    // TODO Auto-generated method stub
    WebApplicationContext wac = WebApplicationContextUtils.
        getRequiredWebApplicationContext(config.getServletContext());
    //XmlWebApplicationContext wac = (XmlWebApplicationContext)
    //    WebApplicationContextUtils.getRequiredWebApplicationContext(
    //    config.getServletContext());
    Address address = (Address)wac.getBean("address");
    address.printInfo();
}

上面的代码是servlet中的init方法,在此方法中可以获得在javaWeb环境下获得IOC容器 (前提是在web.xml中已经配置了spring)。 上面两种方式都可以使用。如果外部要使用IOC容器,则可以从这里入手。

在java Web环境下配置spring的环境做如下补充

在Java项目中通过ClassPathXmlApplicationContext类手动实例化ApplicationContext容器是最常用的。 但对于Web项目就不行了,Web项目的启动是由相应的Web服务器负责的; 所以,在Web项目中ApplicationContext容器的实例化工作最好交给Web服务器来完成。 这里以tomcat服务器为例。

在java Web环境下配置spring的环境有两种方式, 第一种是使用listener,另一种是使用servlet; 这两种方式都要使用配置文件,默认是类路径下的application-context.xml, 如果没有,则必须使用标签指定,具体的方式可本篇文章前面讲解。

使用listener

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

使用servlet

这种方式是为了兼顾Servlet2.3及以下规范的Servlet容器,在tomcat5中已经支持servlet2.4了,所以第一种方式是主流的,

<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

web.xml

<!-- springmvc配置开始 -->
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 
        可以自定义servlet.xml配置文件的位置和名称,
        默认为WEB-INF目录下,
        名称为[<servlet-name>]-servlet.xml,
        如spring-servlet.xml
    -->
    <!-- 
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </init-param>
    -->
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

<!-- springmvc配置结束 -->

<!-- Spring配置开始 --> 
<listener> 
   <listenerclass>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<!-- 指定Spring Bean的配置文件所在目录。
    默认配置在WEB-INF目录下 --> 
<context-param> 
   <param-name>contextConfigLocation</param-name> 
   <param-value>classpath:config/applicationContext.xml</param-value> 
</context-param>

<!-- Spring配置结束 -->
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值