Note that there are some explanatory texts on larger screens.

plurals
  1. POFailed to inject @Service bean into QuartzJobBean
    primarykey
    data
    text
    <p>I am having trouble injecting a @Service bean into a QuartzJobBean. Using the tips from <a href="https://stackoverflow.com/questions/6990767/inject-bean-reference-into-a-quartz-job-in-spring">this question</a>, I was able to inject a @Repository bean but not a @Service bean. Here's what I have:</p> <p>A Repository bean to access MongoDB document:</p> <pre><code>public interface MyRepository extends MongoRepository&lt;&gt; {} </code></pre> <p>A Service bean to perform business logic:</p> <pre><code>@Service public class MyService { @Autowired private MyRepository myRepository; public void method1() { ... } } </code></pre> <p>I was able to use MyService from a Spring MVC controller, so I know Spring instantiated them properly.</p> <p>Next I created a Job that extends from QuartzJobBean and try to inject MyService into it:</p> <pre><code> &lt;bean name="myJob" class="org.springframework.scheduling.quartz.JobDetailBean"&gt; &lt;property name="jobClass" value="com.MyJob" /&gt; &lt;/bean&gt; &lt;bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt; [jobDetails and triggers omitted] &lt;property name="schedulerContextAsMap"&gt; &lt;map&gt; &lt;entry key="myService"&gt; &lt;ref bean="myService"/&gt; &lt;/entry&gt; &lt;/map&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p>With this config, I got <code>'BeanCreationException: Cannot resolve reference to bean 'myService' while setting bean property 'schedulerContextAsMap'</code></p> <p>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.</p> <p>Why is it that myService bean is not visible to the SchedulerFactoryBean? BTW, I already have annotation-config and component-scan tags enabled.</p> <p>[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?</p> <pre><code> &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt; /WEB-INF/spring/root-context.xml, /WEB-INF/spring/spring-security.xml, /WEB-INF/spring/spring-quartz.xml &lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;servlet&gt; &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt; &lt;init-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/spring/appServlet/servlet-context.xml&lt;/param-value&gt; &lt;/init-param&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt; &lt;url-pattern&gt;/&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; </code></pre> <p>root-context.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;!-- Root Context: defines shared resources visible to all other web components --&gt; &lt;!-- Factory bean that creates the Mongo instance --&gt; &lt;bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean"&gt; &lt;property name="host" value="localhost" /&gt; &lt;/bean&gt; &lt;!-- MongoTemplate for connecting and quering the documents in the database --&gt; &lt;bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"&gt; &lt;constructor-arg name="mongo" ref="mongo" /&gt; &lt;constructor-arg name="databaseName" value="test" /&gt; &lt;/bean&gt; &lt;!-- Use this post processor to translate any MongoExceptions thrown in @Repository annotated classes --&gt; &lt;bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /&gt; &lt;mongo:repositories base-package="com.repositories" /&gt; &lt;/beans&gt; </code></pre> <p>servlet-context.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --&gt; &lt;!-- Enables the Spring MVC @Controller programming model --&gt; &lt;annotation-driven conversion-service="conversionService"&gt; &lt;argument-resolvers&gt; &lt;beans:bean class="org.springframework.mvc.data.CustomArgumentResolver"/&gt; &lt;beans:bean class="org.springframework.data.web.PageableArgumentResolver" /&gt; &lt;/argument-resolvers&gt; &lt;/annotation-driven&gt; &lt;!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory --&gt; &lt;resources mapping="/resources/**" location="/resources/" /&gt; &lt;!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --&gt; &lt;beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;beans:property name="prefix" value="/WEB-INF/views/" /&gt; &lt;beans:property name="suffix" value=".jsp" /&gt; &lt;/beans:bean&gt; &lt;!-- Only needed because we install custom converters to support the examples in the org.springframewok.samples.mvc.convert package --&gt; &lt;beans:bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"&gt; &lt;beans:property name="formatters"&gt; &lt;beans:bean class="org.springframework.mvc.convert.MaskFormatAnnotationFormatterFactory" /&gt; &lt;/beans:property&gt; &lt;/beans:bean&gt; &lt;!-- Only needed because we require fileupload in the org.springframework.samples.mvc.fileupload package --&gt; &lt;beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /&gt; &lt;context:annotation-config /&gt; &lt;context:component-scan base-package="com" /&gt; &lt;/beans:beans&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload