1. Define interfaces and implementation classes
According to the test steps in http://sb33060418.iteye.com/admin/blogs/2372850 , create interface classes TestService, TestDao and implementation classes TestServiceImpl, TestDaoImpl in the same package.
[ 2. Spring configuration
root-context.xml adds the following content:
<import resource="services.xml"/> <import resource="daos.xml"/>
Which introduces the service.xml configuration:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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.xsd" default-autowire="byName" default-lazy-init="true"> <!-- services --> <bean id="testService" class="com.sunbin.test.testSpring.service.impl.TestServiceImpl"> </bean> </beans>
dao.xml placement
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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.xsd" default-autowire="byName" default-lazy-init="true"> <!-- daos --> <bean id="testDao" class="com.sunbin.test.testSpring.dao.impl.TestDaoImpl"> </bean> </beans>
3. Added Controller
to create BeanController.java class
package com.sunbin.test.testSpring.web.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import com.sunbin.test.testSpring.service.TestService; public class BeanController implements Controller { private TestService testService; public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub ModelAndView modelAndView = new ModelAndView(); String param = request.getParameter("param"); String messsage = testService.test(param); System.out.println(messsage); modelAndView.addObject("message", messsage); modelAndView.setViewName("helloWorld"); return modelAndView; } public TestService getTestService() { return testService; } public void setTestService (TestService testService) { this.testService = testService; } }
The testService is referenced in the class, and the address parameter named param is read.
4. Springmvc configure
resolvers-context.xml to add the following content:
<bean name="/bean" class="com.sunbin.test.testSpring.web.controller.BeanController"></bean>
A controller with an address of /bean is configured, and testService is automatically assembled through spring's autowire.
7. After testing the BeanController deployment project and starting tomcat, you can see the output by visiting http://localhost:8080/testSpringWeb/bean?param=sunny
through the browser : testServiceImpl.test:testDaoImpl.test:sunny indicates that the Controller injects and calls the Service , Dao succeeded.