使用AbstractITextPdfView,自动服务为空

时间:2017-03-04 00:28:42

标签: spring

我正在使用AbstractITextPdfView来显示PDF文档,而自动装配的operatorReportService始终是null

我检查过组件扫描是否正确:

@ComponentScan("com.amci.wireless1440;com.amci.wireless1440.view")

我已尝试使用@Configurable@Component,但未解决此问题。

package com.amci.wireless1440.view;

@Component
public class OperatorReportPdfView extends AbstractITextPdfView  {

    @Autowired
    OperatorReportService operatorReportService; //dependency inject

    @Override
    protected void buildPdfDocument(Map<String, Object> model,
                                    Document document, PdfWriter writer, HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        // get data model which is passed by the Spring container
        @SuppressWarnings("unchecked")
        List<Integer> assetIds = (List<Integer>) model.get("assetIds");
        String reportDate = (String)model.get("reportDate");

        //OperatorReportServiceImpl operatorReportService = new OperatorReportServiceImpl();
        operatorReportService.build(assetIds, reportDate);

    }

}

@Bean
public ViewResolver contentNegotiatingViewResolver(
                ContentNegotiationManager manager) {

        List< ViewResolver > resolvers = new ArrayList< ViewResolver >();

        InternalResourceViewResolver r1 = new InternalResourceViewResolver();
        r1.setPrefix("/WEB-INF/jsp/");
        r1.setSuffix(".jsp");
        r1.setViewClass(JstlView.class);
        resolvers.add(r1);

        //add the operator report PDF view 
        resolvers.add(pdfViewResolver());

        resolvers.add(jsonViewResolver());
        ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
        resolver.setViewResolvers(resolvers);
        resolver.setContentNegotiationManager(manager);
        return resolver;

}

/*
 * Configure View resolver to provide IText PDF output.
*/
@Bean
public ViewResolver pdfViewResolver() {
    return new PdfViewResolver();
}

//来自03-07-2017 ///////

我已将@Configurable添加到我的PdfViewResolver课程中:

@Configurable
public class PdfViewResolver implements ViewResolver

我的配置类中有这个:

@Bean
public OperatorReportPdfView operatorReportPdfView(){
   OperatorReportPdfView operatorReportPdfView = new OperatorReportPdfView();
   return operatorReportPdfView;
}

//Configure View resolver to provide IText PDF output.
@Bean
public ViewResolver pdfViewResolver() {
        return new PdfViewResolver();
} 

...我的OperatorReportPdfView类配置如下:

@Configurable
public class OperatorReportPdfView extends AbstractITextPdfView  {
    @Autowired
    OperatorReportService operatorReportService; //dependency inject
...
}    

回应2017年3月9日评论: 非常感谢你的帮助。 在我的@Configuration课程中,如果有:

  1. PdfViewResolver定义为@Bean

    @Bean
    public ViewResolver pdfViewResolver() {
        return new PdfViewResolver();
    

    }

  2. OperatorReportPdfView定义为@Bean

    @Bean
    public OperatorReportPdfView operatorReportPdfView(){
        OperatorReportPdfView operatorReportPdfView = new OperatorReportPdfView();
       return operatorReportPdfView;
    }
    
  3. OperatorReportPdfView课程中,我正在注入OperatorReportService。我有信心可以找到这项服务,因为我正在注射 它在控制器中。 但是,当我在这堂课中遇到断点时,我发现OperatorReportServicenull

    @Component
    public class OperatorReportPdfView extends AbstractITextPdfView  {
    
        @Autowired
        OperatorReportService operatorReportService; //dependency inject    
    

    2017年3月9日 在控制台启动日志中找到:

    INFO: Overriding bean definition for bean 'pdfViewResolver' with a different definition: replacing [Generic bean: class [com.amci.wireless1440.spring.configuration.PdfViewResolver]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [C:\Users\mspeicher\workspace-stsNEW\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\Wireless1440Branch\WEB-INF\classes\com\amci\wireless1440\spring\configuration\PdfViewResolver.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=webConfig; factoryMethodName=pdfViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in com.amci.wireless1440.spring.configuration.WebConfig]
    Mar 09, 2017 1:29:52 PM org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
    INFO: Overriding bean definition for bean 'operatorReportPdfView' with a different definition: replacing [Generic bean: class [com.amci.wireless1440.view.OperatorReportPdfView]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [C:\Users\mspeicher\workspace-stsNEW\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\Wireless1440Branch\WEB-INF\classes\com\amci\wireless1440\view\OperatorReportPdfView.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=webConfig; factoryMethodName=operatorReportPdfView; initMethodName=null; destroyMethodName=(inferred); defined in com.amci.wireless1440.spring.configuration.WebConfig] 
    

1 个答案:

答案 0 :(得分:-1)

如果您还没有@Configuration类,则可能需要创建一个@Configuration类。这是一个例子:

@Configuration
public class MyConfig {

    @Bean
    public OperatorReportService operatorReportService() {
        return new OperatorReportService();
    }
}

确保此配置类是组件扫描的一部分。