spring的使用《一》

在前边的文章中说明了,如何搭建一个spring的开发环境,简单回顾下就是把spring的jar包导入工程中,如果是在javaWeb项目中是放在lib目录下,然后在web.xml文件中进行配置,配置spring的配置文件的路径,上篇文章中忘记贴spring的配置文件了,具体的配置文件入下,

<?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容器,则可以从这里入手。

下面对在javaWeb环境下配置spring的环境做如下补充,

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

在javaWeb环境下配置spring的环境有两种方式,第一种是使用listener,另一种是使用servlet;这两种方式都要使用配置文件,默认是类路径下的application-context.xml,如果没有,则必须使用<context-param>标签指定,具体的方式可查看上篇文章。

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


综上是spring在java项目中的使用

有不正之处欢迎指出

谢谢

原文地址:https://www.cnblogs.com/teach/p/5725877.html