Results 1 to 9 of 9

Thread: get configured bean from applicationContext

  1. #1
    Join Date
    Dec 2010
    Posts
    15

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

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

    Default

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

  5. #5
    Join Date
    Dec 2010
    Posts
    175

    Smile

    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>

  6. #6
    Join Date
    Dec 2010
    Posts
    15

    Default

    Quote 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...

  7. #7
    Join Date
    Dec 2010
    Posts
    175

    Smile

    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
    }

  8. #8
    Join Date
    Dec 2010
    Posts
    15

    Default holy crap

    Holy crap this is AMAZINGLY EASY!

    I love spring! It makes my Java life!

  9. #9
    Join Date
    Dec 2010
    Posts
    15

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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •