2012-05-28 3 views
8

내 스프링 응용 프로그램에 문제가 발생했습니다.스프링 싱글 톤이 두 번 호출 됨

저는 매우 단순한 봄용 콩을 가지고 있으며, 다른 여러 가지 스프링 콩에 주입됩니다. 내가 찾은 디버깅하는 동안 그들은 두 번 호출되고 있습니다. 생성자 & @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> 

봄 콩 클래스

@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) 
+1

'@ PostConstruct'도 두 번 호출됩니까? 두 번 호출되는 생성자는 쉽게 설명 할 수 있지만 @ PostConstruct는 설명 할 수 없습니다. –

+0

디스패처 서블릿 구성이 있습니까? –

+0

을 통해 다른 컨텍스트의 xml 구성 파일에서 응용 프로그램 컨텍스트 xml을 가져 왔습니까? –

답변

16

다음 난 그냥 내가 힌트를 줄 @Juan 알베르토에 문제 특별 감사를 파악했다 문제에.

설명 사실 나는 contextListner와 dispatcher 서블릿 모두에 대해 하나의 applicationContext.xml 파일을 제공하고있었습니다. 그래서 첫 번째 bean은 spring core에 대해 초기화하고 spring dispatcher에 대해 두 번째 초기화합니다.

구성을 applicationContext.xml 및 applicationContext-dispatcher.xml에 쏟았습니다.이 구성에는 관련 구성 만 있고, Bean은 제대로 한 번 초기화됩니다.

문제있는 CONFIGS

<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> 

해결 CONFIGS

<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> 
+4

'applicationContext-dispatcher.xml'에 넣어야 할 내용과'applicationContext.xml'에 넣어야 할 내용을 이해하지 못합니다. –

5

사실 문제는 디스패처 서블릿의 콩을 정의 할 수 있으며, 또한 스프링 컨텍스트, 발송자가 다른를 제공하는 것입니다 컨텍스트가 아니라 메인 컨텍스트의 하위 컨텍스트이므로 작업을 수행하는 올바른 방법은 주 컨텍스트에서 "모델 클래스"를 검색하고 디스패처는 컨트롤러 만 검색하는 것입니다.

이 정보가 도움이되기를 바랍니다.

관련 문제