Java Springbean被实例化两次
我有一个Springbean重复实例化的问题。 通常我不会介意,但问题是这是一个调度bean,所以我们将任务调度两次 我们在pom文件中使用Spring3.2.2.RELEASE。 我们有context.xml。 我们使用XML配置,而不是注释。 在我的尝试中,我将所有bean声明移动到一个上下文文件下,该文件在web.xml中声明 下面是servlet.xml。它是空的:Java Springbean被实例化两次,java,spring,spring-mvc,Java,Spring,Spring Mvc,我有一个Springbean重复实例化的问题。 通常我不会介意,但问题是这是一个调度bean,所以我们将任务调度两次 我们在pom文件中使用Spring3.2.2.RELEASE。 我们有context.xml。 我们使用XML配置,而不是注释。 在我的尝试中,我将所有bean声明移动到一个上下文文件下,该文件在web.xml中声明 下面是servlet.xml。它是空的: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=
<?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>
为什么春天创造了两个豆子?
我只有一个上下文文件。
在导入文件中,没有提到计划的bean任务
感谢您的帮助。您正在扫描所有组件两次。
include
并不意味着排除任何仍然检测到的内容。添加使用default filters=“false”
以禁用对包含模式的任何其他内容的检测。感谢您的及时响应。我在context:component scan base package=“package to scan”中添加了use default filters=“false”,但我仍然在日志中看到两次相同的初始化。您在哪里添加了它,并确保在根上下文中排除了@Controller
s。还要确保没有两次加载xml文件。我们已经设法解决了这个问题。我们看到整个上下文被初始化了两次。我们通过将HOST设置为root更改了tomcat的server.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>
<?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>
<?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>
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'