Accessing @Autowired beans in Java event handlers in spring boot
<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</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>
<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</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>
0
Comments
-
<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<br>
@Scope("singleton")<br>
public class ReportEngineFactory implements ApplicationContextAware {<br>
private IReportEngineFactory factory;<br>
private ApplicationContext context;<br>
@PostConstruct<br>
public void start() throws BirtException {<br>
Platform.startup();<br>
factory = (IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);<br>
}<br>
@SuppressWarnings("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<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<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<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<br>
private SampleData sampleData;<br><br><br>
@Override<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<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>
0
Categories
- All Categories
- 108 Developer Announcements
- 53 Articles
- 106 General Questions
- 144 IM Services
- 43 OpenText Hackathon
- 32 Developer Tools
- 20.6K Analytics
- 4.1K AppWorks
- 8.9K Extended ECM
- 899 Cloud Fax and Notifications
- 77 Digital Asset Management
- 9.3K Documentum
- 29 eDOCS
- 120 Exstream
- 39.8K TeamSite
- 1.7K Web Experience Management