Java 我可以在DispatcherServlet中定义bean吗
在我的web.xml中,我删除了Java 我可以在DispatcherServlet中定义bean吗,java,spring,jakarta-ee,spring-mvc,Java,Spring,Jakarta Ee,Spring Mvc,在我的web.xml中,我删除了contextConfigLocation,而不是指向application context,我在dispatcher servlet中定义了bean。这是允许的还是Spring确定要查找contextConfigLocation <!--context-param> //This part is commented. Is this allowed? <param-name>contextConfigLocation
contextConfigLocation
,而不是指向application context
,我在dispatcher servlet中定义了bean。这是允许的还是Spring确定要查找contextConfigLocation
<!--context-param> //This part is commented. Is this allowed?
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener-->
通过调度程序servlet中的bean
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
p:driverClassName="oracle.jdbc.driver.OracleDriver"
p:url="jdbc:oracle:thin:@localhost:1521:Xe"
p:username="hibernate"
p:password="hibernate"></bean>
是,但它们仅在web上下文中可见。看看这个答案 引用Spring框架API(在编写3.2.2时)中的以下内容: 与一般应用程序上下文一样,web应用程序上下文也是 等级制度每个应用程序都有一个根上下文,而 应用程序中的每个servlet(包括 MVC框架)有自己的子上下文 也在这里:: 例如,如果您正在开发一个SpringMVCWeb应用程序,您可以 通常会通过Spring的 ContextLoaderListener和通过加载的子Web应用程序上下文 Spring的DispatcherServlet。这将导致父子上下文 共享组件和基础架构配置所在的层次结构 在根上下文中声明并在子上下文中使用 特定于web的组件 这里:: Spring中的ApplicationContext实例可以被限定范围。在Web MVC中 框架,每个DispatcherServlet都有自己的WebApplicationContext, 它继承根目录中已定义的所有bean WebApplicationContext。这些继承的bean可以在 servlet特定的作用域,并且您可以定义新的特定于作用域的bean 给定Servlet实例的本地 不是最后一句话: 您可以在给定Servlet实例的本地定义新的特定于作用域的bean
简短的回答-是的,你可以。