温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用spring的自身的listener进行web的配置

发布时间:2020-07-05 16:01:59 来源:网络 阅读:593 作者:一笑江湖了 栏目:开发技术

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没看懂。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI