[英]Spring singleton being called twice
在我的 spring 应用程序中遇到一些问题。
我有非常简单的春豆,它们被注入到其他各种春豆中。 在调试时我发现,它们被调用了两次,Constructor 和 @PostConstruct 都被调用了两次。
我的应用程序没有前端技术。 它只是与后端任务相关。
弹簧配置
<?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 调度程序初始化。
我现在将配置溢出到 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>
实际上,您的问题是您可能在调度程序 servlet 和 spring 上下文中定义了 bean,调度程序提供了一个不同的上下文,但它(我认为是一个子上下文)是主上下文,因此正确的做事方法是让您的主上下文扫描您的“模型类”,调度程序只扫描控制器。
我希望这可以帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.