Announcement Announcement Module
No announcement yet.
get configured bean from applicationContext Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • get configured bean from applicationContext

    Hi i have configured bean:
    <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
            <property name="driverClassName" value="${database.driverClassName}"/>
            <property name="url" value="${database.url}"/>
            <property name="username" value="${database.username}"/>
            <property name="password" value="${database.password}"/>
            <property name="validationQuery" value="SELECT 1 FROM DUAL"/>
            <property name="testOnBorrow" value="true"/>
    and I want to get it in my java code. I could create a new jdbc connection but that wouldnt be efficient so I want to use my bean but I just cant get it...

    I am using this in my controller:

    String AppContextPath=projectDeployPath+"/WEB-INF/classes/META-INF/spring/applicationContext.xml";
    			ApplicationContext context=new ClassPathXmlApplicationContext("/"+AppContextPath);
    			BasicDataSource ds=context.getBean("dataSource", BasicDataSource.class);
    			HashMap<String, Object> map = new HashMap<String, Object>();
    		    map.put("REPORTNAME", "Cool report");
    			  JasperPrint  jasperPrint = JasperFillManager.fillReport(JR, map,ds.getConnection());
    The problem is it dies like this:
    OException parsing XML document from class path resource [/Users/kosta/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/xkosteln/WEB-INF/classes/META-INF/spring/applicationContext.xml]; nested exception is class path resource 
    [/Users/kosta/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/xkosteln/WEB-INF/classes/META-INF/spring/applicationContext.xml] cannot be opened because it does not exist
    Well I m just having a hard time accessing the applicationContext.xml file. The funny thing is that I even had to add a "/" in front of my path passed to the constructor otherwise it wouldnt have even a start. So is there any way for me to get the actual info from the applicationContext.xml file?


  • #2
    Don't do that, unless you want to run out of resources. If you need the bean then simply inject it instead of messing around with the context...


    • #3
      The error message is straightforward.
      The file is not in that location.
      maybe you need to escape some characters such as "."

      by the way, why do not use "@Autowired" to get the bean?


      • #4
        or maybe the path is too long, sometimes this is a problem.


        • #5
          As suggested by Marten don't use the datasource this way. Instead inject the datasource into your Controller or handler as follows:

          <bean id="reportHandler" class="com.sample.reports.JasperReportHandler">
          	<property name="ds">
          		<ref bean="dataSource" />


          • #6
            Originally posted by tiger.spring View Post
            As suggested by Marten don't use the datasource this way. Instead inject the datasource into your Controller or handler as follows:

            <bean id="reportHandler" class="com.sample.reports.JasperReportHandler">
            	<property name="ds">
            		<ref bean="dataSource" />
            Sorry for the noob question (just learning this amazing goodness called spring) how do I retrieve this bean after that in my java code? I need to set it as an argument for fillReport function...


            • #7
              public class JasperReportHandler {
              	private DataSource ds;
              	public void setDs(DataSource ds) {
              		this.ds = ds;
              	//now pass the "ds" in your code where it is needed


              • #8
                holy crap

                Holy crap this is AMAZINGLY EASY!

                I love spring! It makes my Java life!


                • #9
                  doesnt work

                  This is a part of my class creating the report:

                  private DataSource injectedBeanDataSource;

                  public DataSource getInjectedBeanDataSource() {
                  return injectedBeanDataSource;

                  public void setInjectedBeanDataSource(DataSource injectedBeanDataSource) {
                  this.injectedBeanDataSource = injectedBeanDataSource;
                  System.out.println("setting bean");

                  public void createReport (){


                  => every single time when or IF i use the injectedBeanDataSource property in my code it dies without any error it just dies (like this:- there is no exception message there is just the line where I used the property
                  I know that the bean gets set ok (It wrote a debug msg to terminal)

         Controller.createreport(JasperReportController.jav a:30)
                  sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
                  org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.invokeHandlerMethod(An
                  org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.handle(AnnotationMetho
                  org.springframework.web.servlet.DispatcherServlet. doDispatch(
                  org.springframework.web.servlet.DispatcherServlet. doService(
                  org.springframework.web.servlet.FrameworkServlet.p rocessRequest(

                  I set the bean exactly as described in webmvc-config.xml ... What am I missing? Is there any annotation that I need to use? I dont think so. I am absolutely sure the bean gets set, I have no idea why it dies then tho...

                  any help is MUCH appreciated!!!