一、SSH配置文件的写法(XML版本)
<util:properties id="jdbc" location="classpath:db.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="#{jdbc.driverName}"></property> <property name="url" value="#{jdbc.url}"></property> <property name="username" value="#{jdbc.userName}"></property> <property name="password" value="#{jdbc.password}"></property> <!-- 设置连接最大数 --> <property name="maxActive" value="20"></property> <!-- 设置连接池实例化时初始创建的连接数 --> <property name="initialSize" value="2"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 指定db连接参数 --> <property name="dataSource" ref="dataSource"></property> <!-- 指定hibernate框架参数 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.OracleDialect </prop> <prop key="hibernate.show_sql"> true </prop> <prop key="hibernate.format_sql"> true </prop> </props> </property> <!-- 指定hbm映射描述信息 --> <property name="mappingLocations"> <list> <value>classpath:org/tarena/entity/Cost.hbm.xml</value> </list> </property> </bean> <bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 事务控制 --> <!-- 定义事务管理bean --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"> </property> </bean> <!-- 定义方面和通知,默认环绕通知 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <!-- 可以指定目标目标对象中不同方法采用不同的事务机制 --> <tx:attributes> <tx:method name="save" propagation="REQUIRED"/> <tx:method name="update" propagation="REQUIRED"/> <tx:method name="delete" propagation="REQUIRED"/> <tx:method name="find*" read-only="true" propagation="REQUIRED"/> <tx:method name="get*" read-only="true" propagation="REQUIRED"/> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 定义切入点,AOP切入 --> <aop:config> <aop:pointcut id="daoPoint" expression="within(org.tarena.dao..*)"/> <!-- 将切入点和通知结合 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="daoPoint"/> </aop:config>
二、SSH配置文件的写法(注解版本)
<util:properties id="jdbc" location="classpath:db.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="#{jdbc.driverName}"></property> <property name="url" value="#{jdbc.url}"></property> <property name="username" value="#{jdbc.userName}"></property> <property name="password" value="#{jdbc.password}"></property> <!-- 设置连接最大数 --> <property name="maxActive" value="20"></property> <!-- 设置连接池实例化时初始创建的连接数 --> <property name="initialSize" value="2"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 指定db连接参数 --> <property name="dataSource" ref="DataSource"></property> <!-- 指定hibernate框架参数 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.OracleDialect </prop> <prop key="hibernate.show_sql"> true </prop> <prop key="hibernate.format_sql"> true </prop> </props> </property> <!-- 指定hbm映射描述信息 --> <property name="mappingLocations"> <list> <value>classpath:org/tarena/entity/Cost.hbm.xml</value> </list> </property> </bean> <!-- 开启组件扫描,扫描Action,Service,Dao --> <context:component-scan base-package="org.tarena"/> <!-- 事务控制 --> <!-- 定义事务管理bean --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"> </property> </bean> <!-- 开启事务注解@Transactional --> <tx:annotation-driven transaction-manager="txManager"/>
package org.tarena.dao; import java.sql.SQLException; import java.util.List; import javax.annotation.Resource; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.context.annotation.Scope; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.stereotype.Repository; import org.tarena.entity.Cost; @Repository @Scope("prototype") public class CostDaoImpl extends HibernateDaoSupport implements CostDao { //用注解才要这么写,否则不用 @Resource//注入sf public void setMySessionFactory(SessionFactory sf){ super.setSessionFactory(sf); }
三、SSM配置文件的写法(XML版本)
<util:properties id="jdbc" location="classpath:db.properties"/> <bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="#{jdbc.driverName}"></property> <property name="url" value="#{jdbc.url}"></property> <property name="username" value="#{jdbc.userName}"></property> <property name="password" value="#{jdbc.password}"></property> <!-- 设置连接最大数 --> <property name="maxActive" value="20"></property> <!-- 设置连接池实例化时初始创建的连接数 --> <property name="initialSize" value="2"></property> </bean> <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dbcp"></property> <property name="mapperLocations" value="classpath:org/tarena/note/sql/*.xml"></property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactory" ref="ssf"></property> <property name="basePackage" value="org.tarena.note.dao"></property> </bean>
<!-- 开启组件扫描,service,controller -->
<context:component-scan base-package="org.tarena.note"/> <!-- SpringMVC配置 --> <mvc:annotation-driven/> <!-- spring事务管理(xml版) --> <!-- 封装事务的提交回滚 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dbcp"></property> </bean> <!-- 指定txManager管理哪些方法 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <!-- 可以指定目标对象中不同方法采用不同的事务机制 --> <tx:attributes> <tx:method name="checkLogin" read-only="true"/> <tx:method name="load*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 指定txManager作用在哪些组件上 --> <aop:config> <aop:pointcut id="servicePoint" expression="within(org.tarena.note.service.*)"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePoint"/> </aop:config> <!-- AOP实例(xml版) --> <!-- AOP记录操作日志案例 --> <bean id="loggerBean" class="com.tarena.aop.LoggerBean"> </bean> <!-- AOP记录Action抛出的异常信息 --> <bean id="exceptionBean" class="com.tarena.aop.ExceptionBean"> </bean> <aop:config> <!-- 公用同一个pointcut --> <aop:pointcut id="actionPointcut" expression="within(com.tarena.action..*)"/> <aop:aspect id="loggerAspect" ref="loggerBean"> <aop:around pointcut-ref="actionPointcut" method="logger"/> </aop:aspect> <aop:aspect id="exceptionAspect" ref="exceptionBean"> <aop:after-throwing pointcut-ref="actionPointcut" method="exec" throwing="ex"/> </aop:aspect> </aop:config>
四、SSM配置文件的写法(注解版本)
<util:properties id="jdbc" location="classpath:db.properties"/> <bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="#{jdbc.driverName}"></property> <property name="url" value="#{jdbc.url}"></property> <property name="username" value="#{jdbc.userName}"></property> <property name="password" value="#{jdbc.password}"></property> <!-- 设置连接最大数 --> <property name="maxActive" value="20"></property> <!-- 设置连接池实例化时初始创建的连接数 --> <property name="initialSize" value="2"></property> </bean> <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dbcp"></property> <property name="mapperLocations" value="classpath:org/tarena/note/sql/*.xml"></property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactory" ref="ssf"></property> <property name="basePackage" value="org.tarena.note.dao"></property> </bean>
<!-- 开启组件扫描,service,controller -->
<context:component-scan base-package="org.tarena.note"/> <!-- SpringMVC配置 --> <mvc:annotation-driven/> <!-- spring事务管理(注解版) --> <!-- 封装事务的提交回滚 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dbcp"></property> </bean> <!-- 带有@Transactional标记的方法会调用txManager组件追加的事务控制 --> <tx:annotation-driven transaction-manager="txManager"/> <!-- AOP实例(注解版) --> <!-- 开启AOP注解@Aspect,@Before,@After --> <aop:aspectj-autoproxy />
@Component @Aspect public class ExceptionBean { Logger logger = Logger.getLogger(ExceptionBean.class); //ex代表目标方法抛出的异常 @AfterThrowing(pointcut="within(com.tarena.action..*)",throwing="ex") public void exec(Exception ex){ ......
@Component//将该组件扫描到Spring容器 @Aspect//将该组件定义为方面组件 public class LoggerBean { Logger logger = Logger.getLogger(LoggerBean.class); //采用环绕通知 @Around("within(com.tarena.action..*)") public Object logger(ProceedingJoinPoint jp) throws Throwable{ ......