spring是目前java 开发领域非常优秀的开源框架,使用spring可以帮助我们构建松耦合的java项目(无论是web项目还是java桌面项目),spring都表现出了非常强悍的
一 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- spring 配置文件的路径信息可以使用(以classpath:开头则spring使用ClassPathXMLApplicationContext加载配置文件)antpath
最好将spring的基础配置文件和应用程序不同层次的配置文件存放在不同的目录下,便于维护!
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml,classpath*:spring/applicationContext*.xml</param-value>
</context-param>
<!-- 配置log4j -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath*:log4j.properties</param-value>
</context-param>
<!-- 配置spring 的web层次的applicationContext 加载器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- 如果使用springmvc框架,配置DispatcherServlet,将spring 容器随着服务器的启动而启动,同时将http请求纳入到spring的管辖范围 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.mvc.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:springmvc/springmvc*.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<!-- 配置默认的页面编码类型使用UTF-8 -->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>false</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*.htm</url-pattern>
</filter-mapping>
</web-app>
二 applicationContext.xml
本文件要建立在src目录下(其实是被ide编译到应用的classes目录下)
这个文件是spring 的主配置文件完成项目的数据源、连接池、AspectJ的开启,基础框架的类的初始化、全局声明事务、缓存声明、连接池属性文件的导入等其他系统基础设施的初始化
<?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-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
"
default-autowire="byName" default-lazy-init="false">
<!-- 属性文件读入 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:init.properties</value>
</list>
</property>
</bean>
<!-- 支持 @Transactional 标记 -->
<tx:annotation-driven />
<!-- 支持 @AspectJ 标记-->
<aop:aspectj-autoproxy proxy-target-class="true" />
<!-- 以AspectJ方式 定义 AOP -->
<aop:config proxy-target-class="true">
<aop:advisor
pointcut="execution(* com.core.service..*Manager.*(..))"
advice-ref="txAdvice" />
<aop:advisor
pointcut="execution(* net.schoolblog..service..*Manager.*(..))"
advice-ref="txAdvice" />
<aop:aspect id="cacheAspect" ref="cacheIntercepter" >
<aop:pointcut id="cachePointcut" expression="execution(* net.schoolblog..service..*Manager.*(..))" />
<aop:around pointcut-ref="cachePointcut" method="cacheExecute" />
</aop:aspect>
<aop:aspect id="cacheAspectTest" ref="cacheIntercepter" >
<aop:pointcut id="cachePointcut2" expression="execution(* com.cool.core.service..test..*Manager.*(..))" />
<aop:around pointcut-ref="cachePointcut2" method="cacheExecute" />
</aop:aspect>
</aop:config>
<bean id="cacheIntercepter" class="com.cool.core.cache.ehcache.intercepter.EhCacheIntercepter">
<property name="ehCache" ref="cacheFactory"></property>
</bean>
<!-- 基本事务定义,使用transactionManager作事务管理,默认get*方法的事务为readonly,其余方法按默认设置.
默认的设置请参考Spring文档事务一章. -->
<tx:advice id="txAdvice" transaction-manager="jdbcTransactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="select*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="pageQuery*" read-only="true" />
<tx:method name="mapQuery*" read-only="true"/>
<tx:method name="pageMapQuery*" read-only="true"/>
<tx:method name="propertyQuery*" read-only="true"/>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass">
<value>${jdbc.driverClassName}</value>
</property>
<property name="jdbcUrl">
<value>${jdbc.url}</value>
</property>
<property name="user">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
<property name="initialPoolSize">
<value>${pool.initialPoolSize}</value>
</property>
<property name="maxPoolSize">
<value>${pool.maxPoolSize}</value>
</property>
<property name="idleConnectionTestPeriod">
<value>${pool.testPeriod}</value>
</property>
<property name="automaticTestTable">
<value>${pool.testTable}</value>
</property>
<property name="maxIdleTime">
<value>${pool.maxIdleTime}</value>
</property>
<property name="acquireIncrement">
<value>${pool.acquireIncrement}</value>
</property>
</bean>
<!-- ============================================= -->
<!-- jdbcTemplate 是很好用的JDBC工具 -->
<!-- ============================================= -->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dao" class="com.cool.core.dao.jdbc.BaseJdbcDao">
<property name="jdbcExecutor" ref="jdbcFactory"></property>
</bean>
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation">
<value>classpath:ehcache.xml</value>
</property>
</bean>
<bean id="cacheFactory" class="org.springframework.cache.ehcache.EhCacheFactoryBean" >
<property name="cacheManager">
<ref local="cacheManager"/>
</property>
</bean>
<!-- ============================= -->
<!-- TransactionManager -->
<!-- ============================= -->
<!-- Only use for ibatic or jdbc transaction-->
<bean id="jdbcTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--sendEmail -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="username" value="rentianlikelai"></property>
<property name="password" value="040907"></property>
<property name="defaultEncoding" value="GB2312"></property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
</props>
</property>
<property name="host" value="SMTP.163.com"/>
</bean>
</beans>
三 springmvc的具体配置
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="2"></property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
<property name="order" value="2"></property>
</bean>
<bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="1"></property>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1000485760"></property>
</bean>
<bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="prefixJson" value="false"></property>
</bean>
<bean id="requestCacheIntercepter"
class="com.cool.core.interceptor.RequestCacheInterceptor">
</bean>
<!-- 以AspectJ方式 定义 AOP -->
<aop:config proxy-target-class="true">
<aop:advisor
pointcut="execution(* com.cool.core.service..*Manager.*(..))"
advice-ref="txAdvice" />
<aop:advisor
pointcut="execution(* net.schoolblog..service..*Manager.*(..))"
advice-ref="txAdvice" />
<aop:aspect id="cacheAspect" ref="cacheIntercepter" >
<aop:pointcut id="cachePointcut" expression="execution(* net.schoolblog..service..*Manager.*(..))" />
<aop:around pointcut-ref="cachePointcut" method="cacheExecute" />
</aop:aspect>
<aop:aspect id="cacheAspectTest" ref="cacheIntercepter" >
<aop:pointcut id="cachePointcut2" expression="execution(* com.cool.core.service..test..*Manager.*(..))" />
<aop:around pointcut-ref="cachePointcut2" method="cacheExecute" />
</aop:aspect>
</aop:config>
<bean id="cacheIntercepter" class="com.cool.core.cache.ehcache.intercepter.EhCacheIntercepter">
<property name="ehCache" ref="cacheFactory"></property>
</bean>
<!-- 基本事务定义,使用transactionManager作事务管理,默认get*方法的事务为readonly,其余方法按默认设置.
默认的设置请参考Spring文档事务一章. -->
<tx:advice id="txAdvice" transaction-manager="jdbcTransactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="select*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="pageQuery*" read-only="true" />
<tx:method name="mapQuery*" read-only="true"/>
<tx:method name="pageMapQuery*" read-only="true"/>
<tx:method name="propertyQuery*" read-only="true"/>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
其实如果要使用更多的特性比如WebService JMS JMX ,JPA,EJB,JTA,Struts Hibernate RMI 以及spring对动态语言的支持,spring都提供了相应的抽象模型进行扩展,今天就先总结到这里,其他内容会在后续的章节进行总结,谢谢!