SpringBoot和SpringBatch在尝试注册Bean时遇到问题,例如注入时出错

时间:2015-07-17 11:33:13

标签: spring spring-mvc dependency-injection spring-boot spring-batch

我的方案是:某些旧应用程序高度依赖于org.w3c.dom.Elementorg.apache.xerces.dom.DocumentImpl

此应用程序从客户读取电子表格,使用参数读取许多本地xml文件,并根据每个节点值创建新文件。换句话说,根据每个节点值应用许多规则,我必须将它们保存到新的应用程序中。

我的要求是:我必须创建一个休息网络服务,它将接收电子表格并使用相同的规则处理它。我已经决定使用Spring Batch和Spring Boot,甚至认为这是我第一次使用它们。

在我尝试注射org.w3c.dom.Element之前,一切都很顺利。 我仔细阅读了手册,但我必须遗漏一些基本概念:

http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#using-boot-importing-xml-configuration
http://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-boot-application.html

错误:

2015-07-17 05:34:58.405  INFO 15348 --- [           main] hello.BatchConfiguration                 : 
2015-07-17 05:34:58.425  WARN 15348 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'batchController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.batch.core.Job hello.BatchController.job; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'importUserJob' defined in class path resource [hello/BatchConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 1 of type [org.springframework.batch.core.Step]: : Error creating bean with name 'step1' defined in class path resource [hello/BatchConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 3 of type [org.springframework.batch.item.ItemProcessor]: : Error creating bean with name 'processor': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.w3c.dom.Element hello.MessageItemProcessor.element; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.w3c.dom.Element] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: candidate for this dependency. Dependency annotations: 

我的代码:

import org.springframework.beans.factory.annotation.Autowired;
import org.w3c.dom.*;
public class MessageItemProcessor implements ItemProcessor<Message, Message> {
       private static final Logger log = LoggerFactory.getLogger(MessageItemProcessor.class);
       @Autowired
       private Element element;
    @Override
    public Message process(final Message message) throws Exception {
       log.info("processing");
       return message;
    }
}

application.java

@Configuration
@ComponentScan
@EnableAutoConfiguration
@ImportResource({"classpath*:applicationContext.xml"}) //I just tried it to see what happens
public class Application {
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName); 
        }
    }
}

从现在开始,事实上,有我的试探性。我不希望有必要添加applicationContext.xml,也不要dispatcher-servelet web.xml,但我试着看看它是否给了我一个亮点。 applicationContext.xml因为我知道启动会处理上下文初始化。顺便说一句,我试过了。

首先,我尝试在classpath和applicationContext.xml

中添加@ImportResource

applicationContext.xml中 ...

       <bean id="element" class="org.w3c.dom.Element" />
</beans>

其次我尝试在config.xml中使用dispatcher-servlet mvc-dispatcher-servlet.xml ...

<bean id="element" class="org.w3c.dom.Element" />
</beans>

的web.xml

       <display-name>test</display-name>
       <servlet>
              <servlet-name>mvc-dispatcher</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
       </servlet>
       <servlet-mapping>
              <servlet-name>mvc-dispatcher</servlet-name>
              <url-pattern>/</url-pattern>
       </servlet-mapping>
       <context-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>/applicationContext.xml</param-value>
       </context-param>
       <listener>
              <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
       </listener>
</web-app>

0 个答案:

没有答案