gpt4 book ai didi

java - 我可以在 dispatcher-servlet 中定义 beans 吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:51:55 27 4
gpt4 key购买 nike

在我的 web.xml 中,我删除了 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>


最佳答案

是的,但它们仅在网络上下文中可见。看这个回答Spring can't see beans between servlet-context and contextConfigLocation beans

Spring Framework API(在编写 3.2.2 时)对 WebApplicationContext 的引用:

Like generic application contexts, web application contexts are hierarchical. There is a single root context per application, while each servlet in the application (including a dispatcher servlet in the MVC framework) has its own child context.

也在这里:Context hierarchies :

For example, if you are developing a Spring MVC web application you will typically have a root WebApplicationContext loaded via Spring's ContextLoaderListener and a child WebApplicationContext loaded via Spring's DispatcherServlet. This results in a parent-child context hierarchy where shared components and infrastructure configuration are declared in the root context and consumed in the child context by web-specific components.

这里:17.2 The DispatcherServlet :

ApplicationContext instances in Spring can be scoped. In the Web MVC framework, each DispatcherServlet has its own WebApplicationContext, which inherits all the beans already defined in the root WebApplicationContext. These inherited beans can be overridden in the servlet-specific scope, and you can define new scope-specific beans local to a given Servlet instance.

不是最后一句:

you can define new scope-specific beans local to a given Servlet instance

关于java - 我可以在 dispatcher-servlet 中定义 beans 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17293104/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com