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

  • get configured bean from applicationContext

    Hi i have configured bean:
    Code:
    <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"/>
        </bean>
    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:

    Code:
    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:
    I
    Code:
    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 java.io.FileNotFoundException: 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?

    thanks

  • #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...

    Comment


    • #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?

      Comment


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

        Comment


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

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

          Comment


          • #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:

            Code:
            <bean id="reportHandler" class="com.sample.reports.JasperReportHandler">
            	<property name="ds">
            		<ref bean="dataSource" />
            	</property>
            </bean>
            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...

            Comment


            • #7
              Code:
              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
              }

              Comment


              • #8
                holy crap

                Holy crap this is AMAZINGLY EASY!

                I love spring! It makes my Java life!

                Comment


                • #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 (){

                  this.getInjectedBeanDataSource().getConnection();
                  }

                  => 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)

                  cz.edu.mub.pef.ja.xkosteln.ireport.NarozeninyRepor t.createReport(NarozeninyReport.java:54)
                  cz.edu.mub.pef.ja.xkosteln.controller.JasperReport Controller.createreport(JasperReportController.jav a:30)
                  sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
                  sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
                  sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
                  java.lang.reflect.Method.invoke(Method.java:597)
                  org.springframework.web.bind.annotation.support.Ha ndlerMethodInvoker.invokeHandlerMethod(HandlerMeth odInvoker.java:176)
                  org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.invokeHandlerMethod(An notationMethodHandlerAdapter.java:426)
                  org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.handle(AnnotationMetho dHandlerAdapter.java:414)
                  org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:790)
                  org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:719)
                  org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:644)

                  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!!!

                  Comment

                  Working...
                  X