GVKun编程网logo

Servlet的Spring根WebApplicationContext(servlet spring 关系)

7

对于Servlet的Spring根WebApplicationContext感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解servletspring关系,并且为您提供关于Annotatio

对于Servlet的Spring根WebApplicationContext感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解servlet spring 关系,并且为您提供关于AnnotationConfigServletWebServerApplicationContext解析、ApplicationContextException:由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext、ApplicationContextException:由于缺少ServletWebServerFactorybean而无法启动ServletWebServerApplicationContext、java web Servlet 使用 Application/ServletContext的宝贵知识。

本文目录一览:

Servlet的Spring根WebApplicationContext(servlet spring 关系)

Servlet的Spring根WebApplicationContext(servlet spring 关系)

我有一个使用SpringMVC
DispatcherServlet加载WebApplicationContext的Web应用程序。Spring参考文档说:

“每个DispatcherServlet都有自己的WebApplicationContext,它继承了已经在
根WebApplicationContext中 定义的所有bean
。这些继承的bean可以在servlet特定的作用域中被覆盖,并且您可以在给定servlet实例本地定义新的特定于作用域的bean。

但是,该根WebApplicationContext放在哪里?

答案1

小编典典

我们applicationContext.xmlWEB-INF目录中,该配置中的Bean可用于该spring-servlet.xml配置,其定义使用

<context-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/applicationContext.xml</param-value></context-param>

顺便说一下ContextLoaderListener,它负责管理根上下文

<listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

AnnotationConfigServletWebServerApplicationContext解析

AnnotationConfigServletWebServerApplicationContext解析

AnnotationConfigServletWebServerApplicationContext解析

springboot中启动的applicationContext就是这个类。类图:

getBean方法

	public <T> T getBean(Class<T> requiredType) throws BeansException {
		assertBeanFactoryActive();
		return getBeanFactory().getBean(requiredType);
	}

	public final ConfigurableListableBeanFactory getBeanFactory() {
		return this.beanFactory;
	}

	public GenericApplicationContext() {
		this.beanFactory = new DefaultListableBeanFactory();
	}

getBean方法的具体实现主要是通过beanFactory来实现的。

DefaultListableBeanFactory

类图

getBean具体的ioc流程后续分析

	public <T> T getBean(Class<T> requiredType) throws BeansException {
		return getBean(requiredType, (Object[]) null);
	}

ApplicationContextException:由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext

ApplicationContextException:由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext

如何解决ApplicationContextException:由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext?

解决方案是:

我明确下面的属性设置为noneapplication.yml文件中。

spring:
  main:
    web-application-type: none

可能你缺少@SpringBootApplicationSpring Boot入门类。

@SpringBootApplication
public class LoginSecurityAppApplication {

    public static void main(String[] args) {
        SpringApplication.run(LoginSecurityAppApplication.class, args);
    }

}

解决方法

我已经使用Spring Boot编写了Spring Batch应用程序。当我尝试在本地系统上使用命令行和类路径运行该应用程序时,它运行良好。但是,当我尝试在linux服务器上运行它时,出现以下异常

Unable to start web server; nested exception is
org.springframework.context.ApplicationContextException: 
Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

下面是我的运行方式:

java -cp jarFileName.jar; lib\* -Dlogging.level.org.springframework=DEBUG -Dspring.profiles.active=dev -Dspring.batch.job.names=abcBatchJob com.aa.bb.StartSpringBatch > somelogs.log

ApplicationContextException:由于缺少ServletWebServerFactorybean而无法启动ServletWebServerApplicationContext

ApplicationContextException:由于缺少ServletWebServerFactorybean而无法启动ServletWebServerApplicationContext

我已经使用Spring Boot编写了Spring
Batch应用程序。当我尝试在本地系统上使用命令行和类路径运行该应用程序时,它运行良好。但是,当我尝试在linux服务器上运行它时,出现以下异常

Unable to start web server; nested exception is
org.springframework.context.ApplicationContextException: 
Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

下面是我的运行方式:

java -cp jarFileName.jar; lib\* -Dlogging.level.org.springframework=DEBUG -Dspring.profiles.active=dev -Dspring.batch.job.names=abcBatchJob com.aa.bb.StartSpringBatch > somelogs.log

java web Servlet 使用 Application/ServletContext

java web Servlet 使用 Application/ServletContext

本文介绍 怎么在Servlet Web应用 中 初始化 全局变量(ServletContext)

ServletContext 也就是 java Servlet Web应用 里面的全局变量,当我们有时要做一些初始化配置是通常也是存在ServletContext里面

怎么初始化呢?有2个办法,一是监听器(listener)来初始化,二是通过设置servlet的load-on-startup=1,让servlert在web应用在启动时就加载这servlert来实现

我们这里主要讲第一个,也就是通过监听器(listener)来实现

首先我们要准备一个实现了ServletContextListener监听器接口的类

public class ContextLoaderListener implements ServletContextListener{

    //实现全局上下文初始化方法
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        //获得全局变量
        ServletContext servletContext = servletContextEvent.getServletContext();
        //设置全局变量属性
        servletContext.setAttribute("test","test1");
    }

    //实现全局上下文销毁函数
    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        //实现 
    }
}

接下来在web.xml里面配置监听器的配置,在web.xml添加以下配置

<listener>
    <listener-class>listener.ContextLoaderListener</listener-class>
</listener>
接下来你就可以在 servlet中取到 全局变量里面test变量的值,代码如下
public class HelloOut extends HttpServlet {
    public void service(HttpServletRequest request,HttpServletResponse response) throws IOException {
        PrintWriter writer = response.getWriter();
        writer.println("Hello Word For Test");
        //获取全局变量
        ServletContext servletContext = getServletContext();
        //输出test变量
        writer.println(servletContext.getAttribute("test"));
        writer.close();
    }
}

PS:扩展,你还可以在listener实现一个计数器,每次运行contextInitialized方法计数+1,然后存到全局变量里去,在servlet中查看计数的值,你就验证contextInitialized 这个方法是否只运行了一次

下一篇文章是在servlet中使用EhCache

今天关于Servlet的Spring根WebApplicationContextservlet spring 关系的介绍到此结束,谢谢您的阅读,有关AnnotationConfigServletWebServerApplicationContext解析、ApplicationContextException:由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext、ApplicationContextException:由于缺少ServletWebServerFactorybean而无法启动ServletWebServerApplicationContext、java web Servlet 使用 Application/ServletContext等更多相关知识的信息可以在本站进行查询。

本文标签: