在前边的文章中说明了,如何搭建一个spring的开发环境,简单回顾下就是把spring的jar包导入工程中,若是是在javaWeb项目中是放在lib目录下,而后在web.xml文件中进行配置,配置spring的配置文件的路径,上篇文章中忘记贴spring的配置文件了,具体的配置文件入下,java
<?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建立的类的对象呢,下面是本文的重点。web
如何从spring环境中得到一个实例对象spring
要从spring中得到一个类的实例,能够经过spring的上下文ApplicationContext对象,ApplicationtContext是一个接口,也称为IOC容器,另外还有BeanFactory容器。它有几个比较重要的实现类,ClassPathXmlApplicationContext、FileSystemXmlApplicationContext、XmlWebApplicationContexttomcat
ClassPathXmlApplicationContext服务器
从类路径下读取配置文件,即从src下读取spring的配置文件,以下,app
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext( "spring-application.xml"); Address address = (Address) appContext.getBean("address");
从类路径下读取了spring-application.xml,而后得到Address的实例对象
这种方式使用于在java项目下得到IOC容器,并取得类的实例。jsp
FileSystemXmlApplicationContextide
从文件系统中读取配置文件,这个类用的很少ui
XmlWebApplicationContextxml
在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项目中的使用
有不正之处欢迎指出
谢谢