spring環境配置(2.5.6)

1,在工程中導入spring支持的jar包:spring.jar、commons-logging.jar;java

官網下載地址:http://www.springsource.org/downloadweb

2,配置bean的xml文件,模板能夠下載的例子裏找到;spring

<?xml version="1.0" encoding="UTF-8"?>
<!--
  - Middle tier application context definition for the p_w_picpath database.
  -->
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">app

<bean id="..." class="...">ide

...spa

</beans>xml

3,實例化spring容器環境(xml文件是基於路徑的):對象

方法一:接口

    public static ApplicationContext getApplicationContextByClassPath()
    {
//        eg1.
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
       
//        eg2.
//      String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};
//      ApplicationContext ctx = new ClassPathXmlApplication(locations);
        return ctx;
    }get

 

方法二:

    public static ApplicationContext getApplicationContextByFileSystem()
    {
//        eg1.   
        ApplicationContext ctx =new FileSystemXmlApplicationContext("D:/workspace2/springtimer/src/beans.xml");
       
//        eg2.
//      String[] locations = {"beans.xml", "bean2.xml", "bean3.xml"};
//      ApplicationContext ctx = new FileSystemXmlApplicationContext(locations );
       
        return ctx;
    }

 

4,在web環境中配置spring環境:

(1)在web.xml裏配置ApplicationContext(beans.xml文件的路徑是相對於web應用根目錄的)

     <context-param>
         <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/classes/beans.xml</param-value>
     </context-param>

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

若是有多個配置文件,

A.能夠採用加載多個xml的方式:

  a.能夠這樣用逗號分開:

   <context-param> 
       <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/applicationContext-dao.xml,/WEB-INF/applicationContext-service.xml</param-value> 
    </context-param> 

  b.也能夠這樣使用通配符:

 <context-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/applicationContext-*.xml</param-value> 
 </context-param> 

B.能夠採用import標籤倒入多個文件的方式:

<beans> 

 

    <import resource="applicationContext-service.xml"/> 

 

    <import resource="applicationContext-dao.xml"/>    

 

</beans>

 

 

(2)在Servlet中獲取spring容器環境,獲取bean

    ServletContext servletContext=request.getSession().getServletContext();
        ApplicationContext atx=WebApplicationContextUtils.getWebApplicationContext(servletContext);
        IUserService userService=(IUserService)atx.getBean("userService");

 注:也可使用3步中的方式,不過每次建立bean,都須要加載xml、和bean,效率不高。

(3)在普通java類中獲取ServletContext或ApplicationContext

   1)實現接口org.springframework.web.context.ServletContextAware。如:

public class SpringContextUtil implements ServletContextAware
{
    private static ServletContext servletContext;

    @Override
    public void setServletContext(ServletContext servletContext)
    {
        SpringContextUtil.servletContext = servletContext;
    }

    public static Object getBean(String name)
    {
        ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        return applicationContext.getBean(name);
    }
}

  2)實現接口org.springframework.context.ApplicationContextAware。如:

public class SpringContextUtil implements ApplicationContextAware
{
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
    {
        SpringContextUtil.applicationContext=applicationContext;
       
    }

    public static Object getBean(String name)
    {
        return applicationContext.getBean(name);
    }

}

注意xml配置文件,bean必須當即加載,才能注入ServletContext或ApplicationContext對象:

<bean id="SpringContextUtil" class="com.company.util.SpringContextUtil" lazy-init="false"></bean>

相關文章
相關標籤/搜索