1

I am having trouble injecting a @Service bean into a QuartzJobBean. Using the tips from this question, I was able to inject a @Repository bean but not a @Service bean. Here's what I have:

A Repository bean to access MongoDB document:

public interface MyRepository extends MongoRepository<> {}

A Service bean to perform business logic:

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

    public void method1() { ... }
}

I was able to use MyService from a Spring MVC controller, so I know Spring instantiated them properly.

Next I created a Job that extends from QuartzJobBean and try to inject MyService into it:

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

With this config, I got 'BeanCreationException: Cannot resolve reference to bean 'myService' while setting bean property 'schedulerContextAsMap'

If I change 'myService' in the schedulerContextAsMap to 'myRepository' it worked. But then I don't want to re-implement the business logic in the QuartzJobBean.

Why is it that myService bean is not visible to the SchedulerFactoryBean? BTW, I already have annotation-config and component-scan tags enabled.

[Update - Context Initialization] Here's my web.xml, as you can see spring-quartz.xml is referenced last so everything should have been initialized before it, no?

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

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

1 回答 1

0

似乎您已根据您对问题的评论解决了该问题。但是,以防万一:

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

如果您正在为您的服务使用注释等,您可能希望在 root-context.xml 中启用组件扫描,但过滤掉任何会被您的 servlet 上下文中的组件扫描拾取的东西,例如控制器,以便您不要重复初始化。

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

根上下文.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>

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

于 2013-08-22T20:01:12.177 回答