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沒看懂。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。