applicationContext.xml和spring-servlet.xml在Spring框架无论如何有关系吗?applicationContext.xml是否可用DispatcherServlet?*-servlet.xml?为什么applicationContext.xml单独不足?我有一个控制器,我希望每个会话都是唯一的.根据spring文档,实现有两个细节:
1.初始Web配置
为了在请求,会话和全局会话级别(Web范围的bean)支持bean的范围,在定义bean之前需要一些小的初始配置.
我web.xml在文档中显示了以下内容:
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)
2. Scoped bean作为依赖项
如果要将(例如)HTTP请求作用域bean注入另一个bean,则必须注入AOP代理来代替作用域bean.
我@Scope提供了proxyMode如下所示的bean注释:
@Controller
@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class ReportBuilder implements Serializable {
...
...
}
Run Code Online (Sandbox Code Playgroud)
问题
尽管有上述配置,但我得到以下异常:
org.springframework.beans.factory.BeanCreationException:创建名为'scopedTarget.reportBuilder'的bean时出错:当前线程的作用域'session'不活动; 考虑为这个bean定义一个范围代理,如果你想从一个单例引用它; 嵌套异常是java.lang.IllegalStateException:找不到线程绑定请求:您是指在实际Web请求之外的请求属性,还是在最初接收线程之外处理请求?如果您实际在Web请求中操作并仍然收到此消息,则您的代码可能在DispatcherServlet/DispatcherPortlet之外运行:在这种情况下,请使用RequestContextListener或RequestContextFilter来公开当前请求.
更新1
下面是我的组件扫描.我有以下内容web.xml:
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>org.example.AppConfig</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)
以下内容AppConfig.java:
@Configuration
@EnableAsync
@EnableCaching
@ComponentScan("org.example")
@ImportResource("classpath:applicationContext.xml")
public class AppConfig implements AsyncConfigurer {
...
...
}
Run Code Online (Sandbox Code Playgroud)
更新2
我创建了一个可重现的测试用例.这是一个小得多的项目,因此存在差异,但会发生同样的错误.有相当多的文件,所以我上传它作为一个tar.gz以megafileupload.
假设我有一个Spring Java项目,我正在尝试将其配置为Web服务器servlet.这是web.xml文件的精简版本:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/generalApplicationContext.xml
</param-value>
</context-param>
<servlet>
<servlet-name>my-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/specificApplicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>my-servlet</servlet-name>
<url-pattern>/foo/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
这里要注意的关键是我已经指定了两个要加载的XML文件.一个是我的整个应用程序的通用,而另一个是特定于"my-servlet"servlet.对于只有一个servlet映射的设置,这没有意义.但是,我的项目有多个servlet映射,每个都有特定的Spring设置.
我的问题: Spring将首先加载哪个contextConfigLocation?它是generalApplicationContext.xml还是specialApplicationContext.xml?更重要的是,装载的顺序是否重要?从我的调试工作来看,它似乎很明显,因为当我将一些独立的Spring配置从一个文件移动到另一个文件时,我得到了不同的错误.
注意:对于多个servlet映射是否使用多个弹簧配置是一个好的做法是值得商榷的.使用XML配置而不是新的Java配置也是如此.但这不是我在这里要问的问题.让我们试着关注我的主要问题.
我有一个带有两个上下文的Spring Web应用程序:一个(applicationContext)构建,ContextLoaderListener另一个(webContext)构建DispatcherServlet.
在applicationContext一个bean(org.springframework.security.authentication.DefaultAuthenticationEventPublisher)中,它触发spring上下文事件.
但是事件的接收器是在webContext.那个接收器没有得到这个事件.(如果将接收器用于测试目的,applicationContext那么它就会得到事件,但我不能这样做,因为我需要webContexts来实现它的功能.)
所以我的问题是,如何将事件与之结合applicationContext起来webContext?
我有一个spring mvc项目设置如下:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-contexts/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-contexts/configuration-context.xml</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-contexts/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-contexts/configuration-context.xml</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)
如果我在configuration-context.xml中创建一个bean并在servlet-context.xml中引用一个bean,它就无法找到它.这些是作为两个不同的背景创建的吗?为什么这种情况会发生/一般这样?
我试图理解在Spring-MVC应用程序中定义Spring Security的推荐方法,其中bean定义分为多个父/子上下文.
例如,我当前的应用程序web.xml看起来如下,(我理解为相当标准)
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
/WEB-INF/securityContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
所以,我有一个标准ContextLoaderListener定义/,加载我的全局配置 - applicationContext.xml和securityContext.xml.我还定义了spring mvc DispatcherServletat /app/,它从中加载了自己的bean spring-mvc-servlet.xml.
据我了解,定义的spring-mvc-servlet.xml配置对于在任一顶级上下文文件中定义的配置是不可见的.
哪里是定义应用级安全概念的最佳位置?例如,我想添加以下过滤器.
<security:http pattern="/oauth/token" create-session="stateless" entry-point-ref="oauthAuthenticationEntryPoint">
<security:custom-filter ref="clientCredentialsTokenEndpointFilter" before="BASIC_AUTH_FILTER" />
</security:http>
Run Code Online (Sandbox Code Playgroud)
这样,请求/app/oauth/token通过此过滤器,并获得处理的基本身份验证.
因为这与Spring-MVC app的关注直接相关,所以我最初定义它spring-mvc-context.xml(这就是为什么它app被排除在url之外).
但是,这意味着它对于定义的安全配置不可见securityContext.xml,因此它被忽略.
所以,我把它移动到securityContext.xml,但这样做,也必须移动所有依赖项.我很快就把所有东西都搬到了applicationContext.xml,这spring-mvc-context.xml …
如果我通过调度程序servlet创建spring上下文,我在DelegatingFilterProxyfilter中遇到错误:
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:251)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.apache.logging.log4j.core.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:66)
Run Code Online (Sandbox Code Playgroud)如果我创建Spring上下文由ContextLoaderListener我只是有404错误怎么一回事,因为中没有的servlet
如果我通过servlet和listener创建spring上下文,我有重复的上下文,所以所有bean都是重复的,包括带有请求映射的控制器,双执行@Scheduled方法等.
如何在不重复上下文的情况下创建高级spring应用程序(包括大量过滤器等)?
我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>MyWebApplication</display-name>
<servlet>
<servlet-name>springDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring.xml
</param-value>
</context-param>
<!-- UTF-8 -->
<filter>
<filter-name>encoding-filter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param> …Run Code Online (Sandbox Code Playgroud) 我一直在努力理解这个错误.我已经完成了与此相关的大部分问题,我不清楚,因为大多数问题都是如何解决的.我想了解错误是什么以及它为什么会出现.请帮助我理解如下:
2.我不一致地接受这个错误.如果这是一个问题,它应该一直都会出现.以下是我的代码段.
- 申请背景
<bean id="location" class="com.mindtree.ruc.cmn.utils.LoginLocation" scope="session">
<aop:scoped-proxy/>
</bean>
<bean id="homePageRH" class="com.rsaame.pas.home.ui.HomePageRH">
<property name="location" ref="location" />
</bean>
Run Code Online (Sandbox Code Playgroud)
- web.xml
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)
- 日志:
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.rsaame.pas.dao.cmn.PASStoredProcedure]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.location': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is …Run Code Online (Sandbox Code Playgroud) 我正在开发一个Spring mvc应用程序,并且最近注意到所有servlet在启动时都注册了两次.
你们有谁知道为什么?如果是这样,我如何在保持所有功能的同时避免这种情况?
Spring配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<!-- SPRING MVC -->
<mvc:annotation-driven />
<context:annotation-config/>
<!-- Activates @Scheduled and @Async annotations for scheduling -->
<task:annotation-driven />
<import resource="spring-security.xml"/>
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/robots.txt" location="robots.txt" />
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"/>
</bean>
<mvc:interceptors>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean> …Run Code Online (Sandbox Code Playgroud) spring ×9
java ×7
spring-mvc ×5
servlets ×2
web.xml ×2
events ×1
java-ee ×1
spring-bean ×1
tomcat ×1
wicket ×1