加入jar包
在web.xml中
添加spring监听器
2 | <listener- class >org.springframework.web.context.ContextLoaderListener</listener- class > |
添加spring容器(父容器)配置文件:
2 | <param-name>contextConfigLocation</param-name> |
4 | /WEB-INF/config/application-context.xml <!--声明数据库连接参数和事务管理--> |
5 | /WEB-INF/config/customer-admin-manage.xml <!--dao和service--> |
spring配置文件application-context.xml
数据库配置文件 jdbc.properties:
1 | jdbc.driverClassName=com.mysql.jdbc.Driver |
添加到spring父容器的配置文件application-context.xml中
1 | <bean id= "propertyConfigurer" class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > |
2 | <property name= "locations" > |
4 | <value>/WEB-INF/config/jdbc.properties</value> |
配置dataSource:
01 | <bean id= "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource" > |
02 | <property name= "driverClass" value= "${jdbc.driverClassName}" /> |
03 | <property name= "jdbcUrl" value= "${jdbc.url}" /> |
04 | <property name= "user" value= "${jdbc.username}" /> |
05 | <property name= "password" value= "${jdbc.password}" /> |
06 | <property name= "autoCommitOnClose" value= "true" /> |
07 | <property name= "checkoutTimeout" value= "${cpool.checkoutTimeout}" /> |
08 | <property name= "initialPoolSize" value= "${cpool.minPoolSize}" /> |
09 | <property name= "minPoolSize" value= "${cpool.minPoolSize}" /> |
10 | <property name= "maxPoolSize" value= "${cpool.maxPoolSize}" /> |
11 | <property name= "maxIdleTime" value= "${cpool.maxIdleTime}" /> |
12 | <property name= "acquireIncrement" value= "${cpool.acquireIncrement}" /> |
13 | <property name= "maxIdleTimeExcessConnections" value= "${cpool.maxIdleTimeExcessConnections}" /> |
配置sessionFactory:
01 | <bean id= "sessionFactory" class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" > |
02 | <property name= "dataSource" ref= "dataSource" /> |
03 | <property name= "mappingLocations" > |
05 | <value>classpath*:/com/cms/customer/entity/hbm/*.hbm.xml</value> |
08 | <property name= "hibernateProperties" > |
10 | hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect |
11 | hibernate.show_sql= false |
12 | hibernate.format_sql= false |
13 | hibernate.query.substitutions= true 1 , false 0 |
14 | hibernate.jdbc.batch_size= 20 |
15 | hibernate.cache.use_query_cache= true |
18 | <property name= "entityInterceptor" >< !-- 配置Hibernate拦截器,自动填充数据的插入、更新时间(不知道什么意思)--> |
19 | <ref local= "treeInterceptor" /> |
21 | <property name= "cacheProvider" >< !-- 为WEB应用提供缓存。 --> |
22 | <ref local= "cacheProvider" /> |
24 | <property name= "lobHandler" >< !-- spring提供的操作lob字段。<bean id= "lobHandler" class = "org.springframework.jdbc.support.lob.DefaultLobHandler" lazy-init= "true" /> --> |
25 | <ref bean= "lobHandler" /> |
基于全注解的事务声明管理
1 | <bean id= "transactionManager" class = "org.springframework.orm.hibernate3.HibernateTransactionManager" > |
2 | <property name= "sessionFactory" ref= "sessionFactory" /> |
4 | <context:annotation-config/> |
5 | <tx:annotation-driven transaction-manager= "transactionManager" /> |
对于context:annotation-config
2 | AutowiredAnnotationBeanPostProcessor(想使用Autowired注解)、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor这 4 个BeanPostProcessor。 |
3 | 另外,<context:component-scan base- package =”XX.XX”/>(用于自动扫描需要注入的bean) 包含了<context:annotation-config/>的功能,在这里没有使用<context:component-scan base- package =”XX.XX”/>,则必须将bean添加到父容器的(dao,service,manager之类的)xml中 |
对于tx:annotation-driven ,则表示所有Transactional注解了的manager都使用这个事务管理
配置springmvc(子容器)
添加springmvc的Servlet
2 | <servlet-name>admin</servlet-name> |
3 | <servlet- class >org.springframework.web.servlet.DispatcherServlet</servlet- class > |
5 | <param-name>contextConfigLocation</param-name><!--指定注入action的配置文件,如果没有指定,则默认在web-inf下查找admin-servlet.xml --> |
6 | <param-value>/WEB-INF/config/admin.xml</param-value> |
8 | <load-on-startup> 1 </load-on-startup> |
每一个servlet对应一个配置文件,用于映射不同的请求路径集合
2 | <servlet-name>front</servlet-name> |
3 | <servlet- class >org.springframework.web.servlet.DispatcherServlet</servlet- class > |
5 | <param-name>contextConfigLocation</param-name> |
6 | <param-value>/WEB-INF/config/front.xml</param-value> |
8 | <load-on-startup> 2 </load-on-startup> |
对于admin.xml
001 | <value>/WEB-INF/languages/core_admin/messages</value> |
002 | <bean id= "messageSource" class = "org.springframework.context.support.ReloadableResourceBundleMessageSource" > |
003 | <property name= "cacheSeconds" value= "-1" /> |
004 | <property name= "basenames" > |
006 | <value>/WEB-INF/languages/core_admin/messages</value> |
012 | <bean id= "multipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver" /> |
014 | <!--通过注解,把一个URL映射到Controller类的方法上--> |
015 | <bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > |
016 | <property name= "webBindingInitializer" ><!--重写WebBindingInitializer--> |
017 | <bean class = " com.cms.common.web.springmvc.BindingInitializer" /> |
021 | <!--用于Spring 从外部属性文件中载入属性,并使用这些属性值替换Spring 配置文件中的占位符变量(${varible})。 --> |
022 | <bean id= "propertyConfigurer" class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > |
023 | <property name= "locations" > |
025 | <value>/WEB-INF/config/firewall.properties</value> |
030 | <!-- DefaultAnnotationHandlerMapping-映射url到被RequestMapping注解的controller或者下面的方法--> |
031 | <bean class = "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" > |
032 | <property name= "interceptors" > |
034 | <ref bean= "adminContextInterceptor" /> |
035 | <ref bean= "adminLocaleIntercept" /> |
036 | <ref bean= "fireWallInterceptor" /> |
041 | <bean id= "adminContextInterceptor" class = "com.cms.cms.web.AdminContextInterceptor" > |
042 | <property name= "auth" value= "true" /> |
043 | <property name= "loginUrl" value= "/admin/cms/login.do" /> |
044 | <property name= "returnUrl" value= "/admin/cms/index.do" /> |
045 | <property name= "excludeUrls" > |
047 | <value>/login. do </value> |
048 | <value>/logout. do </value> |
052 | <bean id= "adminLocaleIntercept" class = "com.cms.cms.web.AdminLocaleInterceptor" /> |
053 | <bean id= "fireWallInterceptor" class = "com.cms.cms.web.FireWallInterceptor" ></bean> |
056 | <bean id= "localeResolver" class = "org.springframework.web.servlet.i18n.CookieLocaleResolver" > |
057 | <property name= "cookieName" value= "clientlanguage" /> |
058 | <property name= "cookieMaxAge" value= "-1" /> |
062 | <bean class = "org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" > |
063 | <property name= "exceptionMappings" > |
065 | <prop key= "org.springframework.web.bind.MissingServletRequestParameterException" >/error/requiredParameter</prop> |
066 | <prop key= "org.springframework.beans.TypeMismatchException" >/error/mismatchParameter</prop> |
067 | <prop key= "org.springframework.web.bind.ServletRequestBindingException" >/error/bindException</prop> |
068 | <prop key= "org.springframework.dao.DataIntegrityViolationException" >/error/integrityViolation</prop> |
073 | <bean id= "freemarkerViewResolver" class = "com.cms.common.web.springmvc.RichFreeMarkerViewResolver" > |
074 | <property name= "prefix" value= "/cms_sys/" /> |
075 | <property name= "suffix" value= ".html" /> |
076 | <property name= "contentType" value= "text/html; charset=UTF-8" /> |
077 | <property name= "exposeRequestAttributes" value= "false" /> |
078 | <property name= "exposeSessionAttributes" value= "false" /> |
079 | <property name= "exposeSpringMacroHelpers" value= "true" /> |
081 | <bean id= "freemarkerConfig" class = "org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" > |
082 | <property name= "templateLoaderPath" value= "/WEB-INF" /> |
083 | <property name= "freemarkerVariables" > |
085 | <!--在FCK编辑器中需要用到appBase,以确定connector路径。--> |
086 | <entry key= "appBase" value= "/admin/cms" /> |
088 | <entry key= "cms_perm" value-ref= "cms_perm" /> |
089 | <entry key= "text_cut" value-ref= "text_cut" /> |
090 | <entry key= "html_cut" value-ref= "html_cut" /> |
093 | <property name= "freemarkerSettings" > |
095 | <prop key= "template_update_delay" > 0 </prop> |
096 | <prop key= "defaultEncoding" >UTF- 8 </prop> |
097 | <prop key= "url_escaping_charset" >UTF- 8 </prop> |
098 | <prop key= "locale" >zh_CN</prop> |
099 | <prop key= "boolean_format" > true , false </prop> |
100 | <prop key= "datetime_format" >yyyy-MM-dd HH:mm:ss</prop> |
101 | <prop key= "date_format" >yyyy-MM-dd</prop> |
102 | <prop key= "time_format" >HH:mm:ss</prop> |
103 | <prop key= "number_format" > 0 .######</prop> |
104 | <prop key= "whitespace_stripping" > true </prop> |
105 | <prop key= "auto_import" >/ftl/cms/index.ftl as p,/ftl/spring.ftl as s</prop> |
111 | <context:annotation-config/> |
114 | < import resource= "admin-action.xml" /> |
对于:admin-action.xml
1 | <bean id= "customerAct" class = "com.customer.action.CustomerAct" /> |
2 | <bean id= "basedataAct" class = "com.customer.action.BasedataAct" /> |
3 | <bean id= "employeeAct" class = "com.customer.action.EmployeeAct" /> |
4 | <bean id= "projectAct" class = "com.customer.action.ProjectAct" /> |
5 | <bean id= "productAct" class = "com.customer.action.ProductAct" /> |
每一个action都要用Controller注解,其中使用的manager属性用Autowired注解,每一个方法都要用RequestMapping注解
每一个manager都要用Service,Transactional注解