web.xml配置如下
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <context-param> <!--配置spring的配置文件的名称和路径--> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!--启动spring自带的IOC容器的servletContextListener--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
同样配置好bean与上个模拟的实现相同
这次不再servlet中做测试,直接在.jsp中嵌入代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <% //从application域对象中获取IOC容器 ApplicationContext ctx= WebApplicationContextUtils.getWebApplicationContext(application); //从IOC容器中获取Bean对象 Person person=ctx.getBean(Person.class); //使用bean对象 person.hello(); %>
这里边的直接写application没看懂。
本文出自 “小小小小白” 博客,请务必保留此出处http://malin.blog.51cto.com/10779111/1842925
原文地址:http://malin.blog.51cto.com/10779111/1842925