ssm註解式開發
阿新 • • 發佈:2018-12-17
- 將service改為註解,完成Dao的注入
- 修改spring-service.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- <bean id="studentService" class="com.enpong.service.impl.StudentServiceImpl"> <property name="studentDao" ref="IStudentDao"></property> </bean> --> <context:component-scan base-package="com.enpong.service.impl"></context:component-scan> </beans>
- 修改Service實現類,對dao注入時可以用ByType方式或者ByName方式注入
package com.enpong.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.enpong.beans.Student; import com.enpong.dao.IStudentDao; import com.enpong.service.IStudentService; @Service public class StudentServiceImpl implements IStudentService { @Autowired //ByType方式注入 private IStudentDao studentDao; @Override @Transactional public void addStudent(Student student) { studentDao.insertStudent(student); } }
- 修改spring-service.xml
- 將事務以註解方式織入到Service層
- 修改spring-tx.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 註冊事務通知 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
- 在service實現類中加入@Transactional
- 修改spring-tx.xml