web.xml---->
<!-- 从类路径下加载spring配置文件,classpath关键字特指类路径下加载 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 负责启动spring容器的监听器,它将引用上面的上下文参数获得spring配置文件地址 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 负责在View层打开session(lazy load) -->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring的核心是DispatcherServlet,它负责控制整个页面的请求路劲 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<!--拦截所有以html结尾的请求-->
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<!--处理从页面传递中文到后台而出现的中文乱码问题-->
<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>
<!-- 错误页面设置 -->
<error-page>
<error-code>404</error-code>
<location>/404.htm</location>
</error-page>
applicationContext.xml---->
<!-- 扫描com下的所有类包,应用spring的注解配置 -->
<context:component-scan base-package="com">
<context:exclude-filter type="regex" expression=".*Service"/>
</context:component-scan>
<!-- 加载JDBC资源文件信息 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 获取数据源 -->
<bean id="c3p0DataSource" destroy-method="close" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 指定连接数据库的驱动 -->
<property name="driverClass" value="${driver}"/>
<!-- 指定连接数据库的URL -->
<property name="jdbcUrl" value="${url}"/>
<!-- 指定连接数据库的用户名 -->
<property name="user" value="${user}"/>
<!-- 指定连接数据库的密码 -->
<property name="password" value="${password}"/>
<property name="maxPoolSize" value="${maxPoolSize}"/>
<property name="minPoolSize" value="${minPoolSize}"/>
<property name="initialPoolSize" value="${initialPoolSize}"/>
<property name="maxIdleTime" value="${maxIdleTime}"/>
</bean>
<!-- 定义Hibernate的SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="c3p0DataSource"/>
<!-- 基于注解类的配置 -->
<property name="packagesToScan">
<list>
<value>com.domain</value>
</list>
</property>
<property name="hibernateProperties">
<!-- 设置Hibernate属性 -->
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_commants">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="connection.autocommit">true</prop>
</props>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 通过AOP配置提供事务增强,让Service包下所有bean的所有方法拥有事务 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<aop:config>
<aop:pointcut id="servicePointcut" expression="execution(* com.service..*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut"/>
</aop:config>
<bean id="employeeService" class="com.service.EmployeeService"/>
<bean id="studentService" class="com.service.StudentService"/>
java类结构