Link to home
Start Free TrialLog in
Avatar of Nika Gudge
Nika GudgeFlag for United States of America

asked on

No bean named 'dataSource' is defined

im trying to connect to database and execute the query.
But i get error: No bean named 'dataSource' is defined
 
My applicationContext.xml is placed under WEB-INF. it contains:
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "/WEB-INF/dtd/spring-beans.dtd">

<beans>

      <!-- Message Properties -->
      <bean id="messageSource"
            class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
            <property name="cacheSeconds" value="900"></property>
            <property name="basenames">
                  <list>
                        <value>classpath:portlet/i18n/reference/reference</value>
                  </list>
            </property>
      </bean>
      
      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
            <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>  
            <property name="url" value="jdbc:oracle:thin:@localhost:1521/XE"/>  
            <property name="username" value="root"/>  
        <property name="password" value="root"/>  
    </bean>
   
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
          <property name="dataSource" ref="dataSource"/>  
    </bean>
     
</beans>


----------------------------------------------------------
this is my JDBCDao.java class.

package com.hp.it.dao.support;

import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;


public class JdbcDao {
        
              public static void main(String[]args) {
                    ApplicationContext ac = new ClassPathXmlApplicationContext("classpath*:/WEB-INF/applicationContext.xml");
                    DataSource dataSource = (DataSource) ac.getBean("dataSource");
                    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
                      
                    String sql="delete from product where id=p1";
                    jdbcTemplate.execute(sql);
                      
                    }
         }
        

SOLUTION
Avatar of Pramod Kumar
Pramod Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nika Gudge

ASKER

im working on spring mvc portlet application.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial