- 将spring-mvc改为注解
- 修改spring-mvc.xml,将处理器的注册方式由原来的<bean/>注册方式改为组件扫描器
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- <bean id="/test/register.do" class="com.enpong.web.StudentController"> <property name="studentService" ref="studentService"></property> </bean> --> <context:component-scan base-package="com.enpong.web"></context:component-scan> </beans>
- 修改处理器类,修改配置文件后,无法再通过配置文件完成service的注入,需要在处理器中以注解方式完成Service的注入
package com.enpong.web; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.enpong.beans.Student; import com.enpong.service.IStudentService; @Controller @RequestMapping("/test") public class StudentController{ @Autowired @Qualifier("studentService") //ByName方式注入 private IStudentService studentService; @RequestMapping("/regiter.do") public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { //接收请求参数 String name = request.getParameter("name"); String ageStr = request.getParameter("age"); Integer age = Integer.valueOf(ageStr); Student student = new Student(name, age); studentService.addStudent(student); return new ModelAndView("/WEB-INF/jsp/welcome.jsp"); } }
- 修改spring-mvc.xml,将处理器的注册方式由原来的<bean/>注册方式改为组件扫描器
- 将spring改为注解
- 将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
- 将service改为注解,完成Dao的注入
- 一般情况下,考虑性能问题,对于mybatis不建议使用注解式开发
ssm注解式开发
最新推荐文章于 2024-06-14 11:45:00 发布