我有一个重复实例化Spring Bean的问题。 通常情况下我不介意,但问题是这是一个预定的bean,所以我们的任务计划安排两次。
我们在pom文件中使用Spring 3.2.2.RELEASE。 我们有context.xml。 我们使用XML配置而不是注释。 在我尝试期间,我将所有bean声明移动到一个上下文文件中,该文件在web.xml中声明。
这是servlet.xml。它是空的:
<?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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd ">
</beans>
这是web.xml部分:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>The App</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
META-INF/application-ctx.xml,WEB-INF/security-context.xml
</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
... Other filters and servlets
</web-app>
以下是上下文: 它的下半部分曾在servlet.context
中<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<import resource="classpath:META-INF/management-application-ctx.xml"/>
<import resource="classpath:META-INF/lucene-application-ctx.xml"/>
<import resource="classpath:META-INF/serving-application-ctx.xml" />
<import resource="classpath:META-INF/tracking-application-ctx.xml" />
<import resource="scheduling-ctx.xml"/>
<!-- configuration -->
<bean id="propertyConfigurer" class="some class">
<property name="location">
<value>config.xml</value>
</property>
<property name="propertiesPersister" ref="persister" />
</bean>
<bean id="persister" class="some other class">
<property name="configSchemaLocation">
<value>config.xsd</value>
</property>
</bean>
... other beans
<!-- This was in the servlet.xml -->
<mvc:annotation-driven />
<context:component-scan base-package="package to scan">
<context:include-filter expression="org.springframework.stereotype.Controller"
type="annotation" />
</context:component-scan>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="detectHandlersInAncestorContexts" value="true" />
</bean>
<mvc:resources mapping="/styles/**" location="/styles/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/static/**" location="/static/" />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/akamai/**" location="/static/akamai/" />
<mvc:interceptors>
<bean
class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />
</mvc:interceptors>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>messages</value>
</property>
</bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="jsonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="exposeContextBeansAsAttributes" value="true" />
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
计划bean(通知我们使用任务和旧的计划方法):
<?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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<task:scheduled-tasks scheduler="aScheduler" >
<task:scheduled ref="someBean" method="runIndexTask"
fixed-delay="300000" initial-delay="300000" />
</task:scheduled-tasks>
<task:scheduler id="aScheduler" pool-size="10"/>
<bean class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref local="credentialScheduledTask" />
</list>
</property>
</bean>
<bean id="credentialScheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="delay" value="10000" />
<property name="period"
value="${core:configuration:credentialScheduledTaskPeriod}" />
<property name="timerTask" ref="cachedCredentialDao" />
</bean>
</beans>
为了验证,我添加了Spring初始化的debig日志。即使在对一个配置位置进行了所有更改之后,我确实看到了bean的双重实例。
是什么导致这种情况?
26/10/2014 08:01:15,491 [localhost-startStop-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'aScheduler'
26/10/2014 08:01:15,491 [localhost-startStop-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'aScheduler'
26/10/2014 08:01:15,494 [localhost-startStop-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'aScheduler' to allow for resolving potential circular references
26/10/2014 08:01:15,501 [localhost-startStop-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'aScheduler'
26/10/2014 08:01:15,501 [localhost-startStop-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'aScheduler'
26/10/2014 08:01:15,502 [localhost-startStop-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'aScheduler'
26/10/2014 08:01:16,485 [localhost-startStop-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'aScheduler'
26/10/2014 08:02:16,593 [localhost-startStop-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'aScheduler'
26/10/2014 08:02:16,593 [localhost-startStop-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'aScheduler'
26/10/2014 08:02:16,597 [localhost-startStop-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'aScheduler' to allow for resolving potential circular references
26/10/2014 08:02:16,605 [localhost-startStop-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'aScheduler'
26/10/2014 08:02:16,605 [localhost-startStop-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'aScheduler'
为什么Spring会创建两个bean? 我只有一个上下文文件。 在导入文件中,未提及计划的bean任务。
感谢任何帮助。