SSSP项目-初始化

  • A+
所属分类:JAVA

一、web.xml的配置

1.配置启动IOC容器的Listener:ContextLoaderListener

路径设置为:applicationContext.xml

具体示例:

  1. <!-- 配置启动IOC容器的Listener:ContextLoaderListener -->
  2.   <!-- needed for ContextLoaderListener -->
  3.     <context-param>
  4.         <param-name>contextConfigLocation</param-name>
  5.         <param-value>classpath:applicationContext.xml</param-value>
  6.     </context-param>
  7.     <!-- Bootstraps the root web application context before servlet initialization -->
  8.     <listener>
  9.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  10.     </listener>

2.配置SpringMVC的DispatcherServlet

配置SpringMVC的DispatcherServlet,删除iniparam的默认配置,并在web.xml旁边创建文件springDispatcherServlet-servlet.xml,并配置路径/

具体示例:

  1. <!-- 配置SpringMVC的DispatcherServlet -->
  2.     <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
  3.     <servlet>
  4.         <servlet-name>springDispatcherServlet</servlet-name>
  5.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  6.         <load-on-startup>1</load-on-startup>
  7.     </servlet>
  8.     <!-- Map all requests to the DispatcherServlet for handling -->
  9.     <servlet-mapping>
  10.         <servlet-name>springDispatcherServlet</servlet-name>
  11.         <url-pattern>/</url-pattern>
  12.     </servlet-mapping>

3.配置字符编码过滤器CharacterEncodingFilter

配置字符编码过滤器CharacterEncodingFilter(位置需在listener之后,其他过滤器之前),init-param的encoding值UTF-8

  1. <!-- 配置字符编码过滤器 -->
  2.     <!-- 字符编码过滤器必须配置在所有过滤器的最前面! -->
  3.     <filter>
  4.         <filter-name>CharacterEncodingFilter</filter-name>
  5.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  6.         <init-param>
  7.             <param-name>encoding</param-name>
  8.             <param-value>UTF-8</param-value>
  9.         </init-param>
  10.     </filter>
  11.     <filter-mapping>
  12.         <filter-name>CharacterEncodingFilter</filter-name>
  13.         <url-pattern>/*</url-pattern>
  14.     </filter-mapping>

4.配置转换POST请求过滤器HiddenHttpMethodFilter

具体示例:

  1. <!-- 配置转换POST请求过滤器HiddenHttpMethodFilter -->
  2.     <filter>
  3.         <filter-name>HiddenHttpMethodFilter</filter-name>
  4.         <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  5.     </filter>
  6.     <filter-mapping>
  7.         <filter-name>HiddenHttpMethodFilter</filter-name>
  8.         <url-pattern>/*</url-pattern>
  9.     </filter-mapping>

5.配置OpenEntityManagerInViewFilter解决懒加载异常问题

配置OpenEntityManagerInViewFilter解决懒加载异常问题,但是会导致多个seesion同时访问变慢

具体示例:

  1. <!-- 配置OpenEntityManagerInViewFilter解决懒加载异常问题 -->
  2.     <filter>
  3.         <filter-name>OpenEntityManagerInViewFilter</filter-name>
  4.         <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
  5.     </filter>
  6.     <filter-mapping>
  7.         <filter-name>OpenEntityManagerInViewFilter</filter-name>
  8.         <url-pattern>/*</url-pattern>
  9.     </filter-mapping>

以上,web.xml配置完毕

二、配置springDispatcherServlet-servlet.xml

1.配置context自动扫描的包

配置context自动扫描的包,并不用默认配置,include进Controller,ControllerAdvice

使用spring config editor来打开xml文件,在namespace中加入context和mvc

具体例子:

  1. <!-- 配置扫描包 -->
  2.     <context:component-scan base-package="com.lancelotblog.sssp" use-default-filters="false">
  3.         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  4.         <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
  5.     </context:component-scan>

2.配置bean视图解析器InternalResourceViewResolver

具体例子:

  1. <!-- 配置视图解析器InternalResourceViewResolver -->
  2.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  3.         <property name="prefix" value="/WEB-INF/views/"></property>
  4.         <property name="suffix" value=".jsp"></property>
  5.     </bean>

3.配置mvc

配置mvc:default-servlet-handler,mvc:annotation-driven

具体例子:

  1. <!-- 配置mvc -->
  2.     <mvc:default-servlet-handler/>
  3.     <mvc:annotation-driven></mvc:annotation-driven>

以上springDispatcherServlet-servlet.xml便配置完毕

三、配置applicationContext.xml

1.配置自动扫描的包,并排除 Controller,ControllerAdvice

在classpath中新建文件applicationContext.xml,配置自动扫描包,并不使用默认配置,实例如下:

  1. <!-- 配置扫描包 -->
  2.     <context:component-scan base-package="com.lancelotblog.sssp" use-default-filters="true">
  3.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  4.         <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
  5.     </context:component-scan>

2.建立db.properties,并用context:property-placeholder引入

在机器中安装mysql数据库服务端,并建立好用户和数据库。然后建立db.properties,并用context:property-placeholder引入。

在建立db.properties时使用jdbc.user和jdbc.password,jdbc.driverClass,jdbc.jdbcUrl分别对应数据库的内容。

db.properties内容如下:

  1. jdbc.user=lancelotblog
  2. jdbc.password=********
  3. jdbc.driverClass=com.mysql.jdbc.Driver
  4. jdbc.jdbcUrl=jdbc\:mysql\:///sssp

在context:property-placeholder内容如下:

  1. <!-- db.propertites -->
  2.     <context:property-placeholder location="classpath:db.propertites"/>

3.配置数据源ComboPooledDataSource

配置数据源ComboPooledDataSource,并测试数据源

例子如下:

  1. <!-- 配置数据源 -->
  2.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  3.         <property name="user" value="${jdbc.user}"></property>
  4.         <property name="password" value="${jdbc.password}"></property>
  5.         <property name="driverClass" value="${jdbc.driverClass}"></property>
  6.         <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
  7.     </bean>

测试数据源,使用juit4:

  1. import javax.sql.DataSource;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class testDataSource {
  6.     private ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  7.     @Test
  8.     public void testDataSource() {
  9.         DataSource dataSource = context.getBean(DataSource.class);
  10.         System.out.println(dataSource.toString());
  11.     }
  12. }

4.配置EntityManagerFactory

(1).使用LocalContainerEntityManagerFactoryBean,引用数据源dataSource

(2).用HibernateJpaVendorAdapter,配置属性jpaVendorAdapter:

(3).配置entityManagerFactory的packagesToScan

(4).配置 entityManagerFactory的jpaProperties,配置如下:

(5).配置 entityManagerFactory的二级缓存:配置sharedCacheMode值ENABLE_SELECTIVE

以上总体例子如下:

  1. <!-- 配置EntityManagerFactory -->
  2.     <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  3.         <property name="dataSource" ref="dataSource"></property>
  4.         <property name="jpaVendorAdapter">
  5.             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
  6.         </property>
  7.         <property name="packagesToScan" value="com.lancelotblog.sssp"></property>
  8.         <property name="jpaProperties">
  9.             <props>
  10.                 <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
  11.                 <prop key="hibernate.hbm2ddl.auto">update</prop>
  12.                 <prop key="hibernate.show_sql">true</prop>
  13.                 <prop key="hibernate.format_sql">true</prop>
  14.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
  15.                 <prop key="hibernate.cache.use_second_level_cache">true</prop>
  16.                 <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
  17.                 <prop key="hibernate.cache.use_query_cache">true</prop>
  18.             </props>
  19.         </property>
  20.         <property name="sharedCacheMode" value="ENABLE_SELECTIVE"></property>
  21.     </bean>

5.配置transactionManager

配置transactionManager事务JpaTransactionManager,以及其属性entityManageFactory

如下:

  1. <!-- 配置事务 -->
  2.     <bean id="transctionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  3.         <property name="entityManagerFactory" ref="entityManagerFactory"></property>
  4.     </bean>

6.配置支持注解的事务

配置tx:annotation-driven的transaction-manager,如下:

  1. <!-- 配置支持注解的事务 -->
  2.     <tx:annotation-driven transaction-manager="transactionManager" />

7.配置SpringData

配置jpa:repositories的base-package和entity-manager-factory-ref

  1. <!-- 配置SpringData -->
  2.     <jpa:repositories base-package="com.lancelotblog.sssp" entity-manager-factory-ref="entityManagerFactory"></jpa:repositories>

至此,applicationContext.xml配置完毕!!!

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: