溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用spring的自身的listener進(jìn)行web的配置

發(fā)布時(shí)間:2020-07-05 16:01:59 來源:網(wǎng)絡(luò) 閱讀:682 作者:一笑江湖了 欄目:開發(fā)技術(shù)

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>
    
    <!--啟動(dòng)spring自帶的IOC容器的servletContextListener-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

同樣配置好bean與上個(gè)模擬的實(shí)現(xiàn)相同

這次不再servlet中做測(cè)試,直接在.jsp中嵌入代碼

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
 <%
//從application域?qū)ο笾蝎@取IOC容器
   ApplicationContext ctx= WebApplicationContextUtils.getWebApplicationContext(application);
   //從IOC容器中獲取Bean對(duì)象
     Person person=ctx.getBean(Person.class);
   //使用bean對(duì)象
     person.hello();

 %>

這里邊的直接寫application沒看懂。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI