1、Spring框架的搭建

1 导入Jar包

什么时候spring容器启动?其实就是程序中执行加载 web.xml配置文件的时候。

这个很简单,只需要web容器中注册org.springframework.web.context.ContextLoaderListener,并指定spring加载配置文件,那么spring容器搭建完成。(当然org.springframework的核心jar包需要引入)

2 web.xml

Spring可以通过(Servlet)org.springframework.web.context.ContextLoaderServlet和Listener(org.springframework.web.context.ContextLoaderListener)两个类作为Spring启动的入口。

当然为了更加易用支持J2EE应用,一般我们还会加上如下:

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns:xsi=””
xmlns=””
xsi:schemaLocation=”

id=”WebApp_ID”
version=”2.5″>
<display-name>teacher</display-name>

  • 1.应用程序下加载

Spring监听HTTP请求事件:org.springframework.web.context.request.RequestContextListener

<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

ApplicationContext context = new
FileSystemXmlApplicationContext(“WebRoot/WEB-INF/applicationContext.xml”);

<!– spring配置文件开始 –>

<welcome-file-list>
<welcome-file>/</welcome-file>
</welcome-file-list>

applicationContext.xml内容如下:

<context-param>

<!– spring MVC配置 –>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<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>/</url-pattern>
</servlet-mapping>

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN” “;
 
<beans>
    <!– dataSource config –>
    <bean id =”dataSource” class =”org.apache.commons.dbcp.BasicDataSource” destroy-method=”close”> 
        <property name=”driverClassName” value=”com.mysql.jdbc.Driver” /> 
        <property name=”url” value=”jdbc:mysql://localhost:3306/game” /> 
        <property name=”username” value=”root” /> 
        <property name=”password” value=”root”/> 
    </bean> 
    
    <!– SessionFactory –>
    <bean id=”sessionFactory”
        class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>

<param-name>contextConfigLocation</param-name><!–
spring配置文件,请根据需要选取 –>

<!– contextConfigLocation配置
–>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
   
/WEB-INF/applicationContext-bean.xml,
   
/WEB-INF/applicationContext-trans.xml,
   
/WEB-INF/applicationContext-hibernateTemplate.xml
   </param-value>
</context-param>

        <property name=”dataSource”>
            <ref bean=”dataSource”/>
        </property>
        <property name=”configLocation”>
            <value>classpath:comsterningbeanhibernatehibernate.cfg.xml</value>
        </property>        
    </bean>
    
    <!– TransactionManager  不过这里暂时没注入–>
    <bean id=”transactionManager”
        class=”org.springframework.orm.hibernate3.HibernateTransactionManager”>
        <property name=”sessionFactory”>
            <ref local=”sessionFactory”/>
        </property>
    </bean>
    
    <!– DAO –>
    <bean id=”booksDao” class=”com.sterning.books.dao.hibernate.BooksMapDao”>
        <property name=”sessionFactory”>
            <ref bean=”sessionFactory”/>
        </property>
    </bean>
    
    <!– Services –>
    <bean id=”booksService” class=”com.sterning.books.services.BooksService”>
        <property name=”booksDao”>
            <ref bean=”booksDao”/>
        </property>
    </bean>
    
    <bean id=”pagerService” class=”com.sterning.commons.PagerService”/>
    
    <!– view –>
    <bean id=”bookAction” class=”com.sterning.books.web.actions.BooksAction” singleton=”false”>
        <property name=”booksService”>
            <ref bean=”booksService”/>
        </property>
        <property name=”pagerService”>
            <ref bean=”pagerService”/>
        </property>
    </bean>  
    
</beans>

<param-value>classpath*:webconfig/service-all.xml</param-value>

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

 

</context-param>

</web-app>

  • 2.web模式下加载

<listener><!– Spring负责监听web容器启动和关闭的事件
–><!– Spring ApplicationContext载入 –>

3 按照web.xml的配置,分别配置spring-servlet.xml, /WEB-INF/applicationContext-bean.xml,
    
/WEB-INF/applicationContext-trans.xml,
    
/WEB-INF/applicationContext-hibernateTemplate.xml

web.xml:
   <context-param>
           <param-name>log4jConfigLocation</param-name>
        
 <param-value>/WEB-INF/classes/log4j.properties</param-value>
  </context-param>
 <!– ContextConfigLocation –>
 <context-param>
     <param-name>contextConfigLocation</param-name>
   
 <param-value>/WEB-INF/spring-context/applicationContext.xml</param-value>
   </context-param>
   
 <!– Listener contextConfigLocation
–>
   <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
 <!– Listener log4jConfigLocation
–>
   <listener>
     <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
   </listener>

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

spring-servlet.xml:

 

</listener>

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=””
xmlns:xsi=””
xmlns:mvc=””
xmlns:aop=””
xmlns:context=””
xmlns:tx=””
xsi:schemaLocation=”
       

       

       

       
 
         
 
 
          “
        >
         
<aop:aspectj-autoproxy
proxy-target-class=”true” />
<context:annotation-config
/>
<context:component-scan
base-package=”*”>
   
<context:include-filter
type=”annotation”
expression=”org.springframework.stereotype.Controller”
/>
<context:exclude-filter
type=”annotation”
expression=”org.springframework.stereotype.Service”
/>
<context:exclude-filter
type=”annotation”
expression=”org.springframework.stereotype.Repository”
/>

另外参考:

<listener><!– Spring监听HTTP请求事件 –>

</context:component-scan>

<!– 使spring支持request与session的scope,如: –>

<mvc:annotation-driven />

<!– <bean scope=”request”/> –>

<bean id=”viewResolver”
class=”org.springframework.web.servlet.view.InternalResourceViewResolver”>
<property name=”prefix”
value=”/”></property>
<property name=”suffix”
value=”.jsp”></property>
</bean>

<!– 使用: –>

</beans>  

<!– 1、注解获取:@AutowiredHttpServletRequest request; –>

applicationContext-bean.xml:

<!– 2、java代码:HttpServletRequest request =

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=””
xmlns:xsi=””
xmlns:mvc=””
xmlns:aop=””
xmlns:context=””
xmlns:tx=””
xsi:schemaLocation=”
       

       

       

       
 
         
 
 
           
        “>

((ServletRequestAttributes)RequestContextHolder.getRequestAttributes.getRequest();
–>

<aop:aspectj-autoproxy
proxy-target-class=”true” />
<context:annotation-config
/>
<context:component-scan
base-package=”*”>
<context:exclude-filter
type=”annotation”
expression=”org.springframework.stereotype.Controller”
/>
</context:component-scan>

<!– 3、直接在参数中传递:publicString sayHi(HttpServletRequest
request) –>

<mvc:annotation-driven />

<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>

</beans>

</listener>

applicationContext-hibernateTemplate.xml:

<listener><!– Spring 刷新Introspector防止内存泄露 –>

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=””  
        xmlns:xsi=””  
        xmlns:context=””
 
        xmlns:mvc=””  
        xmlns:cache=””
         xmlns:tx=””
        xsi:schemaLocation=”  
           
         
 
           
         
 
               
         
         
 
 
          “>
    

<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>

<bean id=”dataSourceC3p_zh”
class=”com.mchange.v2.c3p0.ComboPooledDataSource”
destroy-method=”close”>
<property
name=”driverClass”>
<value>com.mysql.jdbc.Driver</value>
</property>
<property name=”jdbcUrl”>
<value>jdbc:mysql://localhost:3306/teacher?characterEncoding=utf-8</value>
</property>
<property name=”user”>
<value>root</value>
</property>
<property name=”password”>
<value>0000</value>
</property>
<property
name=”initialPoolSize” value=”3″ />
<property name=”maxPoolSize”
value=”10″ />
<property name=”minPoolSize”
value=”2″ />
<property
name=”acquireIncrement” value=”3″ />
<property name=”maxIdleTime”
value=”600″ />
<property name=”maxStatements”
value=”0″ />
<property
name=”maxStatementsPerConnection” value=”80″ />
</bean>

</listener>

<bean id=”sessionFactory”
class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>
<property
name=”dataSource”>
<ref local=”dataSourceC3p_zh”
/>
</property>
<property
name=”hibernateProperties”>
<props>
<prop
key=”hibernate.show_sql”>true</prop>
<prop
key=”hibernate.format_sql”>true</prop>
<prop
key=”hibernate.dialect”>org.hibernate.dialect.MySQLDialect</prop>
<prop
key=”hibernate.current_session_context_class”>thread</prop>
<prop
key=”hibernate.generate_statistics”>true</prop> 
</props>
</property>

<filter>

<property
name=”mappingResources”>
<list>
<value>com/cptl/user/model/teacher.hbm.xml</value>

<filter-name>encodingFilter</filter-name>

</list>
</property>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

</bean>
<bean id=”hibernateTemplate”
class=”org.springframework.orm.hibernate3.HibernateTemplate”>
<property
name=”sessionFactory”>
<ref local=”sessionFactory”
/>
</property>
</bean>

<init-param>

<!–
配置事务管理器bean,使用HibernateTransactionManager事务管理器 –>
<!– 
<tx:annotation-driven
transaction-manager=”transactionManager”/>
<bean id=”transactionManager”
 
 class=”org.springframework.orm.hibernate3.HibernateTransactionManager”>
       <property
name=”sessionFactory” ref=”sessionFactory_zh”/>
</bean>
–>
</beans>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

<init-param>

<param-name>forceEncoding</param-name>

<param-value>false</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>encodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<!– spring配置文件结束 –>

2、Spring MVC的搭建

首先我们知道Spring
MVC的核心是org.springframework.web.servlet.DispatcherServlet,所以web容器中少不了它的注册。(当然org.springframework的web、mvc包及其依赖jar包需要引入)

<!– spring mvc配置开始 –>

<servlet>

<servlet-name>Spring-MVC</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath*:spring/spring-mvc.xml</param-value><!–
spring mvc配置文件 –>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>Spring-MVC</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<!– spring mvc配置结束 –>

同时为了更好使用MVC,spring-mvc.xml需要配置以下:

1)多部分请求解析器(MultipartResolver)配置,与上传文件有关
需要类库commons-io、commons-fileupload

<bean >

<property
name=”defaultEncoding”value=”utf-8″></property><!–
默认编码–>

<property
name=”maxUploadSize”value=”104857600″></property><!–
文件大小最大值–>

<property
name=”maxInMemorySize”value=”40960″></property><!–
内存中的最大值–>

</bean>

发表评论

电子邮件地址不会被公开。 必填项已用*标注