在test包下新增一个测试类PorscheDaoTest,对selectAll方法进行测试
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:application.xml") public class PorscheDaoTest { @Resource private PorscheDao porscheDao; @Test public void selectAll() { List<Porsche> porscheList = porscheDao.selectAll(); for (Porsche porsche : porscheList) { System.out.println(porsche); } } } 复制代码
执行测试
输出porsche表中所有的数据,与预期目标一致。
新增service包,增加PorsheService
@Service public class PorscheService { @Autowired private PorscheDao porscheDao; public List<Porsche> getPorscheList(){ return porscheDao.selectAll(); } } 复制代码
新增controller包,增加PorscheController,该类调用PorscheService的getPorscheList方法,获取全部数据,并在页面上展示
public class PorscheController extends HttpServlet { } 复制代码
对于Web项目来说当启动成功后第一次访问时Tomcat容器会创建PorscheController对象,IOC容器创建的对象和Tomcat创建的对象两者无任何关联,Controller对象要交给Tomact来创建,不能增加在类上@Controller注解
PorscheController对象想要获取PorscheService对象就不能通过@Autowired方式来获取,可以通过IOC容器工具来获取,新增util包,实现IOC容器工具ContextUtil
public class ContextUtil { public static Object getBean(String beanName){ ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml"); Object bean = context.getBean(beanName); return bean; } } 复制代码
测试这个工具类
public class ContextUtilTest { @Test public void getBean() { Object porscheService = ContextUtil.getBean("porscheService"); System.out.println(porscheService); } } 复制代码
执行测试
通过工具类可以获取到PorscheService,从而也就可以调用getPorscheList获得所有数据
public class PorscheController extends HttpServlet { private PorscheService porscheService = (PorscheService) ContextUtil.getBean("porscheService"); @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 查询所有数据 List<Porsche> porscheList = porscheService.getPorscheList(); request.setAttribute("porscheList",porscheList); request.getRequestDispatcher("index.jsp").forward(request,response); for (Porsche porsche : porscheList) { System.out.println(porsche); } System.out.println("doGet被调用"); } } 复制代码
在index.jsp中获取并展示数据
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Index</title> </head> <body> ${requestScope.porscheList} </body> </html> 复制代码
在web.xml中配置PorscheController的访问路径
<servlet> <description></description> <display-name>PorscheController</display-name> <servlet-name>PorscheController</servlet-name> <servlet-class>com.citi.controller.PorscheController</servlet-class> </servlet> <servlet-mapping> <servlet-name>PorscheController</servlet-name> <url-pattern>/porsches</url-pattern> </servlet-mapping> 复制代码
打开Project Structure,选中Artifacts,将右侧的jar包全部导入左侧WEB-INF目录下的新建的lib文件夹
配置Tomcat
启动Tomcat,浏览器输入 http://localhost:8080/porsches
Spring整合Java Web成功
1.4 监听器的作用
在ProscheController中通过IoC容器工具ContextUtil来获取PorscheService,而ContextUtil中是通过下面这行代码来获取一个新的IoC容器。
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml"); 复制代码
但是这里只有创建容器的代码没有销毁容器的代码,会造成内存被占满的情况。因此希望这个IoC容器能够在合适的时机来创建和销毁,即项目启动时IoC容器创建完成,项目销毁时IoC容器也随之销毁。
Spring Web中的ContextLoaderListener可以监听Tomcat容器,并提供了创建和销毁IoC容器的方法,查看org.springframework.web.context.ContextLoaderListener的源码
该类提供了容器初始化和销毁的方法
1.4.1 使用监听器
首先需要在pom.xml中增加Spring Web的依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring-version}</version> </dependency> 复制代码
web.xml中增监听器的配置,同时也需要配置Spring配置文件的路径,监听器通过Spring配置文件中的配置来创建Spring容器
<!--监听器依赖的配置--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application.xml</param-value> </context-param> <!--监听器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 复制代码
ContextUtil中就不需要通过new ClassPathXmlApplicationContext("classpath:application.xml")的方式来获取IoC容器了。可以通过ContextLoader.getCurrentWebApplicationContext()来获取;修改ContextUtil的getBean方法的代码
public static Object getBean(String beanName){ WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext(); Object bean = context.getBean(beanName); return bean; } 复制代码
重新启动Tomcat,浏览器再次输入 http://localhost:8080/porsches
成功输出所有数据
Spring 与 Java Web的集成正式完成,并且得到成功的验证!