Accessing @Autowired beans in Java event handlers in spring boot

BIRT User
edited February 11, 2022 in Analytics #1
<p><span style="font-family:verdana, geneva, sans-serif;"><span style="font-size:14px;">Hi Team,</span><br><span style="font-size:14px;">I am using spring boot application to run the report and to create Java event handlers. I am able to set the spring context using the below code. </span></span><br>
 <br><strong>ApplicationContext sprCtx = ContextAccess.getApplicationContext();<br>
config.getAppContext().put("spring",sprCtx);<br>
Platform.startup( config );<br>
 </strong><br><span style="font-size:14px;"><span style="font-family:verdana, geneva, sans-serif;">In BIRT event handler class, i have initialized the object using @Autowired annotation. However the bean is not instantiated and it throws NullPointerException when i try to access its methods.</span></span><br>
 </p>
<p><strong>@Autowired&lt;/strong></p>
<p><strong>MyObject obj;</strong><br>
 </p>
<p><span style="font-size:14px;"><span style="font-family:verdana, geneva, sans-serif;">Any idea how to get the beans autowired in java event handler implicitely?</span></span></p>
<p> </p>
<p> </p>
<p><span style="font-size:14px;"><span style="font-family:verdana, geneva, sans-serif;">I am using ReportDesignHandler which extends <strong>ReportEventAdapter</strong>. I am overwritting the properties in <strong>onPrepare</strong>() and <strong>beforefactory</strong>() methods.</span></span></p>
<p> </p>
<p> </p>

Comments

  • kclark
    kclark E
    edited September 27, 2017 #2
    <p>Can you post your code?  I'm running into a similar error to you when trying to recreate your issue but I think my error is likely caused by another issue.</p>
    Warning No formatter is installed for the format ipb
  • <p>Hi Clarke,</p>
    <p> </p>
    <p>I couldn't attach the files as Zip due to firewall issues.</p>
    <p> </p>
    <p>I am using a plain empty report design and with the help of event handler i am adding the elements in it.<br>
     </p>
    <p>
    </p>
    <p>@Component&lt;br>
    @Scope(&quot;singleton")<br>
    public class ReportEngineFactory implements ApplicationContextAware {<br>
        private IReportEngineFactory factory;<br>
        private ApplicationContext context;<br>
        @PostConstruct&lt;br>
        public void start() throws BirtException {<br>
            Platform.startup();<br>
            factory = (IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);<br>
        }<br>
        @SuppressWarnings(&quot;unchecked")<br>
        public IReportEngine createEngine() {<br>
            EngineConfig config = new EngineConfig();<br>
            config.getAppContext().put("spring", this.context);<br>
            return factory.createReportEngine(config);<br>
        }<br>
        @PreDestroy&lt;br>
        public void shutdown() {<br>
            Platform.shutdown();<br>
        }<br>
        public void setApplicationContext(ApplicationContext arg0) {<br>
            this.context = arg0;<br>
        }<br>
    }</p>
    <p>
    </p>
    <p>@Service&lt;br>
    public class BirtEngine {<br>
        private IReportEngine renderEngine = null;<br>
        private static final String REPORT_DESIGN_NAME = "src/main/resources/TestReport.rptdesign";<br><br>
        @Autowired&lt;br>
        public BirtEngine(ReportEngineFactory engineFactory) {<br>
            renderEngine = engineFactory.createEngine();<br>
        }<br>
        public  void runAndRenderTask() throws EngineException {<br>
            IRunAndRenderTask runAndRenderTask = null;<br>
            IReportRunnable reportRunnable = renderEngine.openReportDesign(REPORT_DESIGN_NAME);<br>
            runAndRenderTask = renderEngine.createRunAndRenderTask(reportRunnable);<br>
            runAndRenderTask.setRenderOption(getRenderOption());<br>
            runAndRenderTask.run();<br>
            runAndRenderTask.close();<br>
        }<br>
        private IRenderOption getRenderOption() {<br>
            IRenderOption renderOption = new PDFRenderOption();<br>
            renderOption.setOutputFormat("pdf");<br>
            renderOption.setOutputFileName("TestReport.pdf");<br>
            return renderOption;<br>
        }<br>
    }</p>
    <p> </p>
    <p>
    </p>
    <p>public class ReportDesignHandler extends ReportEventAdapter {<br><br>
        @Autowired&lt;br>
        private SampleData sampleData;<br><br><br>
        @Override&lt;br>
        public void onPrepare(IReportContext reportContext) throws ScriptException {<br><br>
            try {<br>
                <br>
                <br>
                GridHandle newGrid = reportContext.getDesignHandle().getElementFactory().newGridItem("", 2, 1);<br><br>
                LabelHandle label1 = reportContext.getDesignHandle().getElementFactory().newLabel("country");<br>
                label1.setText(sampleData.getCountryName());<br>
                LabelHandle label2 = reportContext.getDesignHandle().getElementFactory().newLabel("city");<br>
                label2.setText(sampleData.getCityName());<br>
                newGrid.getCell(0, 1).getContent().add(label1);<br>
                newGrid.getCell(0, 2).getContent().add(label2);<br>
                <br>
                <br>
                reportContext.getDesignHandle().getBody().add(newGrid);<br>
                reportContext.getDesignHandle().saveAs("TestSaveAs.rptdesign");<br>
                <br>
            }<br>
            catch (SemanticException | IOException e) {<br>
                e.printStackTrace();<br>
            }<br><br>
        }<br><br>
    }</p>
    <p> </p>
    <p>
    </p>
    <p>@Service&lt;br>
    public class SampleData {<br><br>
        private String countryName;<br>
        private String cityName;<br>
        public SampleData(){<br>
            setCountryName("United States");<br>
            setCityName("Chicago");<br>
        }<br>
        public String getCountryName() {<br>
            return countryName;<br>
        }<br>
        public void setCountryName(String countryName) {<br>
            this.countryName = countryName;<br>
        }<br>
        public String getCityName() {<br>
            return cityName;<br>
        }<br>
        public void setCityName(String cityName) {<br>
            this.cityName = cityName;<br>
        }<br>
    }</p>
    <p>
    </p>