applicationContext.xml文件的部署
1.声明命名空间
tx+beans+context+aop
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
2.引入其他的xml
dao service action
<!-- 映入其他xml -->
<import resource="classpath:dao.xml"/>
<import resource="classpath:service.xml"/>
<import resource="classpath:action.xml"/>
3.声明使用注解
1.往Spring注入bean的注解
2.往bean注入属性的主键
3.AOP的注解
component-scan包含annotation-config
<!-- 使用注解 -->
<context:component-scan base-package="pack.pack"/>
<context:annotation-config/>
<aop:aspectj-autoproxy/>
4.连接池
1.声明连接池(DruidDataSource,连接池配置信息看其源代码)
2.引入db.properties
<!-- 连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="password" value="${db.password}"/>
<property name="username" value="${db.username}"/>
</bean>
<context:property-placeholder location="classpath:db.properties"/>
db.properties
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql:///jdbc
db.password=admin
db.username=root
5.Hibernate的session工厂
在spring文件声明hibernate的配置:LocalSessionFactoryBean(注意版本)
1.引入连接池
2.配置信息
3.映射目录地址声明
<!-- sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<value>
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.cache.use_second_level_cache=true
</value>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:domain</value>
</list>
</property>
</bean>
6.在service层开启hibernate的事务
1.事务管理员.(HibernateTransactionManager注入session工厂)
2.事务的方法属性.(声明管理员+通知)
3.织入事务切面.(切点+切面织入)
<!-- Transaction -->
<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="list*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* *..*Service.*(..))" id="pc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>
web.xml部署
1.声明命名空间
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
2.声明监听器
1.声明监听器ContextLoaderListener
2.配置全局参数contextConfigLocation
3.sturts的过滤器StrutsPrepareAndExecuteFilter
<!-- Spring的监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- sturts的过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.在请求时候打开session
Spring集成Hiberna会出现延迟加载问题
解决:在请求时候打开session,响应时候关session
防止proxy对象没有实例化就关session而导致no session问题
<filter>
<filter-name>opensessioninview</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>opensessioninview</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>