Bean Creation or defining is failing in springboot

57 Views Asked by At

Main Class

      package com.prac.sdp;
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.context.ApplicationContext;
      import com.prac.sdp.pdf.PdfGenerator;

     @SpringBootApplication
      public class SdpApplication {


       public static void main(String[] args) {
          ApplicationContext ctx=SpringApplication.run(SdpApplication.class, args);
          PdfGenerator pdg=ctx.getBean(PdfGenerator.class);
          pdg.pdfgenerate();
        }
    }

       

PdfGenerator.java

              package com.prac.sdp.pdf;
              import java.io.FileNotFoundException;
              import org.springframework.beans.factory.annotation.Autowired;
              import org.springframework.stereotype.Component;
              import com.itextpdf.kernel.pdf.PdfDocument;
              import com.itextpdf.kernel.pdf.PdfPage;
              import com.itextpdf.kernel.pdf.PdfWriter;
              import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
              import com.itextpdf.layout.Document;

           @Component
            public class PdfGenerator {

          @Autowired 
          PdfWriter writer;  <------ Autowiring is not working here I don't know why.
        }

 

Issue -- Consider defining a bean of type 'com.itextpdf.kernel.pdf.PdfWriter' in your configuration.

Resolution tried:
Used @ComponentScan("com.itextpdf") --> Started getting Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.prac.sdp.pdf.PdfGenerator' available.

Let me know how this issue can be fixed I am stuck at this. Thanks in advance.

1

There are 1 best solutions below

1
On

create bean with @Bean annotation tag , as this is from third party library , you need to define by Method.

@Bean
public PdfWriter writer(){
  return new PdfWriter();
}

Add above method to your SdpApplication class.