spring整合springmvc:启动tomcat服务器的时候,需要加载spring的配置文件。
监听器监听ServletContext域对象(服务器启动的时候创建,服务器关闭销毁)创建和销毁,执行一次,服务器启动执行。
监听器去加载spring的配置文件,创建web版本工厂,存储ServletContext对象。
1、在webapp下的WEB-INF中的web.xml中加入以下配置
<!--配置spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--设置配置文件的路径--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
2、修改AccountService
package com.cnstrong.controller; import com.cnstrong.service.AccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * 账户web */ @Controller @RequestMapping("/account") public class AccountController { @Autowired private AccountService accountService; @RequestMapping("/findAll") public String findAll(){ System.out.println("表现层:查询所有账户..."); //调用service的方法 accountService.findAll(); return "list"; } }
3、运行测试