gpt4 book ai didi

java - Spring bean 在 Spring Web 应用程序中初始化两次

转载 作者:行者123 更新时间:2023-11-30 06:35:04 25 4
gpt4 key购买 nike

Project Directory我们正在开发 Spring MVC 项目。我们希望在部署 EAR 时初始化所有 Bean。以下是我们的 web.xml 文件:

    <servlet>
<servlet-name>Spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Spring</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>


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

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

下面是我们的 Spring 配置文件:

<context:component-scan base-package="com"></context:component-scan>
<context:annotation-config></context:annotation-config>
<mvc:annotation-driven></mvc:annotation-driven>

<!-- Switch on the Caching -->
<cache:annotation-driven />

<!-- Do the component scan path -->
<!-- <context:component-scan base-package="caching" /> -->
<bean id="ehcache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="WEB-INF/ehcache.xml" p:shared="true" /> <!-- Why changed to true? https://stackoverflow.com/a/16370326 -->

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref="ehcache" />

<!-- creating datasource -->
<bean id="dataSourceForFilters"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.mariadb.jdbc.Driver" />
<property name="url" value="jdbc:mariadb://ip:3306/demo" />
<property name="username" value="remote" />
<property name="password" value="password" />
</bean>
<bean id="dataSourceForData" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:/misoracle"></property>
</bean>

<!-- creating jdbctemplate and injecting datasource into it -->
<bean id="jdbcTemplateForFilters" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSourceForFilters"></property>
</bean>
<bean id="jdbcTemplateForData" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSourceForData"></property>
</bean>
<!-- end -->

<bean id="applicationContextProvider" class="com.providers.ApplicationContextProvider"></bean>

我们使用组件注释(@Controller、@Service、@Repository)来定义 bean。

将 EAR 部署到服务器时,Beans 可以按照我们的要求正常初始化。

当我们使用“IP:port/context-root/rest”等 URL 模式访问应用程序时,Spring bean 会再次初始化。

为什么 beans 再次初始化,有人可以帮忙吗?

最佳答案

您的 web.xml 映射应如下所示:

<servlet>
<servlet-name>Spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/Spring-servlet.xml</param-value>
</context-param>

关于java - Spring bean 在 Spring Web 应用程序中初始化两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45344942/

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