Announcement Announcement Module
Collapse
No announcement yet.
problem getting dataSource using ClassPathXmlApplicationContext Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


    • #3
      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");

      Comment


      • #4
        No No No...

        Configure the PropertyPlaceholderConfigurer in the application context. I suggest a a read of the reference guide regarding the PropertyPlaceholderConfigurer.

        Comment

        Working...
        X