gpt4 book ai didi

java - Spring单例被调用两次

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:30:34 25 4
gpt4 key购买 nike

我的 spring 应用程序出现了一些问题。

我有非常简单的 spring bean,它们被注入(inject)到其他各种 spring bean 中。在调试时我发现,它们被调用了两次,Constructor 和 @PostConstruct 都被调用了两次。

我的应用程序没有前端技术。它只是与后端任务相关。

Spring 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd">


<context:component-scan base-package="com.green.integration" />

<!-- ######################################################## -->
<!-- EXPOSING SPRING BEAN VIA HTTPINVOKER SPRING REMOTING -->
<!-- ######################################################## -->

<bean name="/switch"
class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="SwitchController" />
<property name="serviceInterface"
value="com.green.ISwitchController" />
</bean>

<!-- Load in application properties reference -->
<bean id="applicationProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:application.properties" />
</bean>


<bean id="mongo" class="com.mongodb.Mongo">
<constructor-arg value="${mongo.server}" />
<constructor-arg value="${mongo.port}" />
</bean>

<bean id="morphia" class="com.google.code.morphia.Morphia">
</bean>


</beans>

Spring Bean 类

@Repository
public class TransactionDAO extends BasicDAO<Transaction, ObjectId> {
private Datastore datastore;

@Autowired
public TransactionDAO(Mongo mongo, Morphia morphia) {
super(mongo, morphia, "itransact");
morphia.map(Transaction.class);
// TO USE MONGO WITHOUT SECURITY
this.datastore = morphia.createDatastore(mongo, "itransact");
logger.debug("***** CONNECTED TO MONGODB SUCCESSFULLY *****");
this.datastore.ensureIndexes();
// this.datastore.ensureCaps();
}
}

构造函数“TransactionDAO”被调用了两次。

我试图通过以下方式查看调用堆栈跟踪

Throwable t = new Throwable();
System.out.println(t.getStackTrace()[1].toString());

每次都显示以下内容

sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

最佳答案

我刚刚弄清楚了这个问题,特别感谢@Juan Alberto 给我提示了这个问题。

描述:实际上我为 contextListner 和调度程序 servlet 提供了一个 applicationContext.xml 文件。所以第一个 bean 是为 spring 核心初始化的,第二次是为 spring dispatcher 初始化的。

我现在将配置溢出到 applicationContext.xml 和 applicationContext-dispatcher.xml 中,它们只有它们的相关配置,并且我的 bean 正在正确初始化一次。

有问题的配置

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


<servlet>
<servlet-name>remoting</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>

已解决的配置

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


<servlet>
<servlet-name>remoting</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-dispatcher.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>

关于java - Spring单例被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10790120/

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