无法将@Service bean注入QuartzJobBean

时间:2013-08-22 16:20:52

标签: spring spring-mvc quartz-scheduler

我在将一个@Service bean注入QuartzJobBean时遇到了麻烦。使用this question中的提示,我能够注入一个@Repository bean但不能注入@Service bean。这就是我所拥有的:

用于访问MongoDB文档的Repository bean:

public interface MyRepository extends MongoRepository<> {}

用于执行业务逻辑的服务bean:

@Service
public class MyService {
    @Autowired
    private MyRepository myRepository;

    public void method1() { ... }
}

我能够从Spring MVC控制器使用MyService,所以我知道Spring正确地实例化了它们。

接下来,我创建了一个从QuartzJobBean扩展的Job,并尝试将MyService注入其中:

    <bean name="myJob" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass" value="com.MyJob" />  
    </bean>
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
       [jobDetails and triggers omitted]
       <property name="schedulerContextAsMap">
        <map>
           <entry key="myService">
                <ref bean="myService"/>
           </entry>

        </map>
    </property>
    </bean>

使用此配置,我得到了'BeanCreationException: Cannot resolve reference to bean 'myService' while setting bean property 'schedulerContextAsMap'

如果我将schedulerContextAsMap中的'myService'更改为'myRepository',它就可以了。但后来我不想在QuartzJobBean中重新实现业务逻辑。

为什么调度器FactoryBean看不到myService bean?顺便说一句,我已经启用了注释配置和组件扫描标签。

[更新 - 上下文初始化] 这是我的web.xml,你可以看到spring-quartz.xml最后被引用,所以一切都应该在它之前被初始化,不是吗?

  <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>
        /WEB-INF/spring/root-context.xml,
        /WEB-INF/spring/spring-security.xml,
        /WEB-INF/spring/spring-quartz.xml
      </param-value>
   </context-param>
  <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>appServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
      </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>appServlet</servlet-name>
     <url-pattern>/</url-pattern>
   </servlet-mapping>

根context.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:mongo="http://www.springframework.org/schema/data/mongo" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">

<!-- Root Context: defines shared resources visible to all other web components -->
<!-- Factory bean that creates the Mongo instance -->
<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
    <property name="host" value="localhost" />
</bean>

<!-- MongoTemplate for connecting and quering the documents in the database -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongo" ref="mongo" />
    <constructor-arg name="databaseName" value="test" />
</bean>

<!-- Use this post processor to translate any MongoExceptions thrown in @Repository annotated classes -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

<mongo:repositories base-package="com.repositories" />

</beans>

servlet的context.xml中

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

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven conversion-service="conversionService">
    <argument-resolvers>
        <beans:bean class="org.springframework.mvc.data.CustomArgumentResolver"/>           
        <beans:bean class="org.springframework.data.web.PageableArgumentResolver" />
    </argument-resolvers>
</annotation-driven>

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<!-- Only needed because we install custom converters to support the examples in the org.springframewok.samples.mvc.convert package -->
<beans:bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <beans:property name="formatters">
        <beans:bean class="org.springframework.mvc.convert.MaskFormatAnnotationFormatterFactory" />
    </beans:property>
</beans:bean>

<!-- Only needed because we require fileupload in the org.springframework.samples.mvc.fileupload package -->
<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

<context:annotation-config />
<context:component-scan base-package="com" />
</beans:beans>

1 个答案:

答案 0 :(得分:0)

似乎您已根据自己对问题的评论解决了问题。但是,只是在案例中:

底层原始问题是您只在servlet-context中配置了组件扫描。因此,仅在初始化servlet上下文时才会处理MyService批注。当ContextLoaderListener为根上下文处理spring-quartz.xml时,它还不可用。

如果您正在使用服务的注释等,可能希望在root-context.xml中启用组件扫描,但过滤掉任何可能由servlet上下文中的组件扫描拾取的内容,例如控制器。这样你就不会得到重复的初始化。

如果您的服务和Web代码共享通用软件包名称,那么常见的模式是使用过滤器,组件将除控制器之外的所有内容扫描到根上下文中,然后使用某些内容仅扫描servet上下文中的控制器(和servlet)像这样:

根context.xml中

<context:component-scan base-package="package">
    <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

servlet的context.xml中

<context:component-scan base-package="package" use-default-filters="false">
    <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" />
</context:component-scan>

当然,还有很多其他选择。欲了解更多信息:

相关问题