phoenix
phoenix

Reputation: 995

How to change the following bean properties at runtime?

I have the following Spring xml file for generating different views for different file formats. I have two properties one url and datasource. I want the url to be changed at run time i.e. I want to use different jrxml files rather just one static one.

    <bean id="pdfReport"
        class="org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView"
        p:url="classpath:tree-template.jrxml" p:reportDataKey="datasource" />

    <bean id="xlsReport"
        class="org.springframework.web.servlet.view.jasperreports.JasperReportsXlsView"
        p:url="classpath:tree-template.jrxml" p:reportDataKey="datasource" />

    <bean id="htmlReport"
        class="org.springframework.web.servlet.view.jasperreports.JasperReportsHtmlView"
        p:url="classpath:tree-template.jrxml" p:reportDataKey="datasource" />

    <bean id="csvReport"
        class="org.springframework.web.servlet.view.jasperreports.JasperReportsCsvView"
        p:url="classpath:tree-template.jrxml" p:reportDataKey="datasource" />

Upvotes: 0

Views: 785

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280171

Retrieve the bean from your context

ApplicationContext context = ...;
JasperReportsCsvView view = (JasperReportsCsvView) context.getBean("csvReport");

and use its setter to change the property

view.setUrl(someNewValue);

Do the same thing for each bean. If your beans share a common super type, you can use ApplicationContext#getBeansOfType(Class) to retrieve all of them at once as a Map. You then iterate over the entries and change the view's property.

Upvotes: 3

Related Questions