来自XmlWebApplicationContext javadoc:
默认情况下,配置将从"/WEB-INF/applicationContext.xml"获取根上下文,而"/WEB-INF/test-servlet.xml"获取具有命名空间"test-servlet"的上下文(如对于具有servlet-name"test"的DispatcherServlet实例.
Spring语境是什么意思?
什么是根背景?还有哪些其他类型的Spring上下文?
什么是命名空间?
更新:
一些后续问题:
什么是Spring ApplicationContext - 它是否包含在配置XML文件中定义的bean?
查看ContextLoaderListener的代码,看起来它加载了配置XML文件中定义的数据.但我的Spring Web应用程序无需定义此侦听器或任何其他侦听器.怎么会这样?
在什么情况下有多个Spring的DispatcherServlet实例是有意义的?
根上下文(来自applicationContext.xml的数据)是否适用于DispatcherServlet的每个实例,而其他上下文(例如来自test-servlet.xml的数据)仅适用于相关的DispatcherServlet(即测试)?
当我第一次开始学习Spring时,在applicationContext.xml文件中配置了一些东西.然后当我开始专门阅读更新版本的spring时,他们都在单独的XML文件中完成了配置,例如myapp-servlet-xml,myapp-security.xml,myapp-service.xml等.在web.xml文件中配置contextConfigLocation.因此,例如,我一直关注的代码就是它的contextConfigLocation:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/myapp-servlet.xml
/WEB-INF/myapp-data.xml
</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)
无论如何,最近我遇到了一个配置问题(StackOverflow的帮助人员帮助我弄清楚),这是由于这种分离.这些书中的示例没有applicationContext.xml文件,稍后当我尝试向应用程序添加自动扫描和注释时,这会导致问题.我尝试将所有内容移动到applicationContext.xml中并取消其他文件并解决了问题.没有其他改变,我只是将所有内容都放在applicationContext.xml中.
因此,这与其他人的评论一起使我有理解,即使您没有创建applicationContext.xml,它仍然被使用,并且它是某种配置层次结构的顶级.我希望其他人可以向我解释这一切是如何运作的,因为我没有在任何地方遇到任何解释.
因此,例如,如果我将某些上下文:组件扫描标记放入applicationContext.xml下面的配置文件中,则可能导致某些类无法扫描.那种性质的东西.我不明白优先级和必须去哪里确定应用程序的广泛性等等.如果有人能够清楚地解释它或指向我解释它的资源我会非常感激,谢谢.希望我所要求的是有道理的.
我有一个spring bean(dao)对象,我通过以下xml在我的ServletContext中实例化:
<bean id="userDao" class="com.company.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
Run Code Online (Sandbox Code Playgroud)
这个bean在我的webapp-servlet.xml文件中声明,并由我的应用程序在ServletContext中使用.
我也在使用SpringSecurity.我的理解是,它在不同的上下文中运行(SecurityContext).
我的应用程序有一个webapp-security.xml,我在其中实例化一个自定义身份验证提供程序.我想使用我的应用程序中使用的我的dao也在我的安全上下文中进行用户查找,但是当我运行时:
<bean id="userAuthenticationProvider" class="com.company.security.UserAuthenticationProvider">
<property name="userDao" ref="userDao" />
</bean>
Run Code Online (Sandbox Code Playgroud)
我得到错误,说没有这样的bean"userDao".bean在我的其他上下文中声明的bean中自动装配好,但不在我的安全上下文中.根据Spring Docs,我相信web.xml中需要两个单独的上下文
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.security.web.session.HttpSessionEventPublisher
</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,如何才能访问我的SecurityContext中存在于我的ServletContext中的DAO?我的dao是否有范围修饰符,或者我可以在运行时在我的身份验证提供程序中以某种方式获取ServletContext?作为参考,这是我想在我的身份验证提供程序中使用它的方式:
public class UserAuthenticationProvider extends
AbstractUserDetailsAuthenticationProvider {
@Override
protected UserDetails retrieveUser(String userName,
UsernamePasswordAuthenticationToken authenticationToken)
throws AuthenticationException {
// use dao here
Run Code Online (Sandbox Code Playgroud)
谢谢你向我解释这个
更新:
继续我的调查,似乎我使用我的daos的DispatcherServlet是一个子上下文,安全上下文在某个更高的位置.因此,父上下文无法看到我的DispatcherServlet中的bean.我认为答案是以某种方式将我的bean声明移动到父应用程序上下文中,但我不知道如何做到这一点.这是我的web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring-*.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.security.web.session.HttpSessionEventPublisher
</listener-class>
</listener>
<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> …Run Code Online (Sandbox Code Playgroud) 嗨我想从属性文件autowire boolean值已经引用以下链接与地图url Spring属性(property-placeholder)自动装配
但我想自动连接一个布尔属性,也提到了问题Spring Autowire原始布尔 Spring Autowire原始布尔值, 但那是bean值,在我的情况下我想使用点分隔的属性值做同样的事情.
${does.it.allow}
//它失败,并给出字符串不能转换为boolean
#{does.it.allow}
//这给用名称来定义无豆/属性确实,但我有正确的属性文件,它证明了容器能够加载它,因为第一个错误.