如何通过Java Spring框架实现WEB应用的实例化?

2026-04-30 09:082阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计327个文字,预计阅读时间需要2分钟。

如何通过Java Spring框架实现WEB应用的实例化?

1. 前面讲解的都是通过直接读取配置文件的方式来初始化,例如实例化ApplicationContext和AbstractApplicationContext,使用代码`app=new ClassPathXmlApplicationContext(beans.xml);`。下面讲解的是通过web.xml文件进行初始化。

1.前面讲解的都是通过直接读取配置文件,进行的实例化ApplicationContext

AbstractApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");

下面讲解直接通过配置文件进行初始化。

2.web.xml

如何通过Java Spring框架实现WEB应用的实例化?

<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代码,不过实现的效果都是一样的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计327个文字,预计阅读时间需要2分钟。

如何通过Java Spring框架实现WEB应用的实例化?

1. 前面讲解的都是通过直接读取配置文件的方式来初始化,例如实例化ApplicationContext和AbstractApplicationContext,使用代码`app=new ClassPathXmlApplicationContext(beans.xml);`。下面讲解的是通过web.xml文件进行初始化。

1.前面讲解的都是通过直接读取配置文件,进行的实例化ApplicationContext

AbstractApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");

下面讲解直接通过配置文件进行初始化。

2.web.xml

如何通过Java Spring框架实现WEB应用的实例化?

<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代码,不过实现的效果都是一样的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。