Results 1 to 4 of 4

Thread: problem getting dataSource using ClassPathXmlApplicationContext

  1. #1
    Join Date
    Dec 2011
    Posts
    26

    Default problem getting dataSource using ClassPathXmlApplicationContext

    Hi, I would like execute and jdbctemplate from a java class, to do that i get the datasource from a bean, the problem is that the properties of my datasource comes from a file.

    java class:
    Code:
    ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:datasource.xml");
        	DataSource datasource = (DataSource) ac.getBean("dataSource");
    bean:
    Code:
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
    		<property name="driverClassName" value="${database.driverClassName}" />
    		<property name="url" value="${database.url}" />
    		<property name="username" value="${database.username}" />
    		<property name="password" value="${database.password}" />
    	</bean>
    error:
    Code:
    CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '${database.driverClassName}'
    any solution provided by Spring? thx!

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    And why should spring do anything here? Make sure that there is a PropertyPlaceHolderConfigurer configured and that the placeholders are available as properties. If they aren't it isn't going to work.
    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
    Join Date
    Dec 2011
    Posts
    26

    Default

    done!
    Code:
    XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("beans.xml"));
    
    // bring in some property values from a Properties file
    PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
    cfg.setLocation(new FileSystemResource("jdbc.properties"));
    
    // now actually do the replacement
    cfg.postProcessBeanFactory(factory);
    
    DataSource datasource = (DataSource) factory.getBean("dataSource");

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    No No No...

    Configure the PropertyPlaceholderConfigurer in the application context. I suggest a a read of the reference guide regarding the PropertyPlaceholderConfigurer.
    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

Posting Permissions

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