Java Spring WEB应用实例化如何实现
作者:手撕高达的村长 时间:2022-05-20 04:26:35
1.前面讲解的都是通过直接读取配置文件,进行的实例化ApplicationContext
AbstractApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");
下面讲解直接通过配置文件进行初始化。
2.web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
这样,ApplicationContext便已经实例化了,默认就直接加载了beans.xml里面的内容。
来看看底层的代码,类ContextLoaderListener中有个初始化方法
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
if (this.contextLoader == null) {
this.contextLoader = this;
}
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
进入initWebApplicationContext方法 :
ApplicationContext parent = loadParentContext(servletContext);
// Store context in local instance variable, to guarantee that
// it is available on ServletContext shutdown.
this.context = createWebApplicationContext(servletContext, parent);
这句也就是容器加载的结果。
1和2一个是java代码一个是xml代码,不过实现的效果都是一样的。
来源:https://www.cnblogs.com/sunxun/p/5408273.html
标签:Java,Spring,WEB,应用,实例
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java isInterrupted()判断线程的实例讲解
2023-07-21 01:45:53
新手初学Java流程控制
2023-08-23 08:12:34
![](https://img.aspxhome.com/file/2023/8/77108_0s.png)
java集合继承关系图分享
2023-04-25 17:17:23
![](https://img.aspxhome.com/file/2023/0/67480_0s.png)
springmvc与mybatis集成配置实例详解
2021-06-16 22:10:27
CentOS 7下JDK8的详细安装步骤
2022-02-18 12:06:28
![](https://img.aspxhome.com/file/2023/2/76302_0s.png)
Spring Boot如何优化内嵌的Tomcat示例详解
2023-11-13 17:52:53
JavaWeb使用POI操作Excel文件实例
2022-06-12 18:19:30
![](https://img.aspxhome.com/file/2023/5/72385_0s.png)
Java 添加、读取和删除 Excel 批注的操作代码
2023-10-28 21:55:36
![](https://img.aspxhome.com/file/2023/5/81885_0s.png)
Java ArrayList深入源码层分析
2021-10-18 04:12:31
![](https://img.aspxhome.com/file/2023/9/60919_0s.png)
java使用dom4j操作xml示例代码
2022-03-21 18:28:38
快速搭建Spring Boot+MyBatis的项目IDEA(附源码下载)
2023-10-24 18:29:53
![](https://img.aspxhome.com/file/2023/6/84306_0s.png)
SpringCloud:feign对象传参和普通传参及遇到的坑解决
2023-02-17 11:18:58
RocketMQ实现随缘分BUG小功能示例详解
2023-01-20 04:21:05
![](https://img.aspxhome.com/file/2023/8/64038_0s.jpg)
java编程创建型设计模式单例模式的七种示例
2023-06-21 08:47:39
![](https://img.aspxhome.com/file/2023/7/77387_0s.png)
JVM内存增强之逃逸分析
2022-01-23 00:22:08
Java安全-ClassLoader
2023-08-18 02:12:21
![](https://img.aspxhome.com/file/2023/9/80329_0s.png)
图文详解Java中的序列化机制
2021-06-15 12:11:49
![](https://img.aspxhome.com/file/2023/9/77059_0s.png)
新的Java访问mysql数据库工具类的操作代码
2023-04-05 16:54:58
基于Java swing组件实现简易计算器
2023-03-29 02:10:07
![](https://img.aspxhome.com/file/2023/7/80897_0s.png)
Springboot文件上传功能的实现
2021-09-20 12:48:47
![](https://img.aspxhome.com/file/2023/0/64400_0s.png)