Announcement Announcement Module
Collapse
No announcement yet.
@Resource not being injected Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Resource not being injected

    I'm sorry for posting this if it sound stupid, but I am just getting started with spring batch and this issue is driving me nuts.

    Let's say my app as 3 jars.
    jar1 defines a marshaller interface + impl and has the following context definition:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    	<bean id="marshaller" class="com.accovia.totem.tjc.MarshallerImpl" />
    	<bean id="flags" class="com.accovia.totem.tjc.Flags" />
    </beans>
    jar2 injects the marshaller as a resource:
    Code:
    public class ToiLineMapper implements LineMapper<InputRecord> {
        @Resource
        Marshaller marshaller;
    ...
    and imports in its context like this
    Code:
    <import resource="classpath*:/applicationContext-totem-infra.xml" />
    Finally my cli-app (using commandlinejobrunner) ties it all up with a app context and a job context
    which does the same import as above and does the standard definitions and has also this to register automatically my jobs:
    Code:
    	<!-- Job Registrar -->
    	<bean
    		class="org.springframework.batch.core.configuration.support.AutomaticJobRegistrar">
    		<property name="applicationContextFactories">
    			<bean
    				class="org.springframework.batch.core.configuration.support.ClasspathXmlApplicationContextsFactoryBean">
    				<property name="resources" value="classpath*:/jobs/job*.xml" />
    			</bean>
    		</property>
    		<property name="jobLoader">
    			<bean
    				class="org.springframework.batch.core.configuration.support.DefaultJobLoader">
    				<property name="jobRegistry" ref="jobRegistry" />
    			</bean>
    		</property>
    	</bean>
    For reference my reader def (which fails on a NPE on the @resource injection):
    Code:
    <bean id="toiFileMultiLineItemReader" class="com.accovia.totem.batch.io.MultiLineToiItemReader">
    		<property name="delegate">
    			<bean class="org.springframework.batch.item.file.FlatFileItemReader">
    				<property name="resource" ref="toiFile" />
    				<property name="lineMapper">
    					<bean class="com.accovia.totem.batch.io.ToiLineMapper">
    					</bean>
    				</property>
    			</bean>
    		</property>
    	</bean>
    For reference my job def:
    Code:
    	<batch:job id="toiJob">
    		<batch:step id="step">
    			<batch:tasklet>
    				<batch:chunk reader="toiFileMultiLineItemReader" writer="bookingItemWriter" commit-interval="5" />
    			</batch:tasklet>
    		</batch:step>
    	</batch:job>
    Why is my @Resource always null ?!? Im sure it is something simple and obvious, but I just can't see it
    If I Marshaller marshaller = new MarshallerImpl();, the jobs runs perfectly, but it defies the spring use ...

    Please help ...
    Thanks in advance

  • #2
    did you configure your spring context as shown in annotation-based Container Configuration ?

    Code:
    <?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:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    <context:annotation-config/>
    
    </beans>

    Comment


    • #3
      Many thanks, it works !
      I knew I was missing something obvious, but just did not know what to look for ...
      Thank you !

      Comment

      Working...
      X