Nika Gudge
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.s upport.Rel oadableRes ourceBundl eMessageSo urce">
<property name="cacheSeconds" value="900"></property>
<property name="basenames">
<list>
<value>classpath:portlet/i 18n/refere nce/refere nce</value >
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons. dbcp.Basic DataSource " destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver. OracleDriv er"/>
<property name="url" value="jdbc:oracle:thin:@l ocalhost:1 521/XE"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework .jdbc.core .JdbcTempl ate">
<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.contex t.Applicat ionContext ;
import org.springframework.contex t.support. ClassPathX mlApplicat ionContext ;
import org.springframework.jdbc.c ore.JdbcTe mplate;
public class JdbcDao {
public static void main(String[]args) {
ApplicationContext ac = new ClassPathXmlApplicationCon text("clas spath*:/WE B-INF/appl icationCon text.xml") ;
DataSource dataSource = (DataSource) ac.getBean("dataSource");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql="delete from product where id=p1";
jdbcTemplate.execute(sql);
}
}
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
<beans>
<!-- Message Properties -->
<bean id="messageSource"
class="org.springframework
<property name="cacheSeconds" value="900"></property>
<property name="basenames">
<list>
<value>classpath:portlet/i
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.
<property name="driverClassName" value="oracle.jdbc.driver.
<property name="url" value="jdbc:oracle:thin:@l
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework
<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.contex
import org.springframework.contex
import org.springframework.jdbc.c
public class JdbcDao {
public static void main(String[]args) {
ApplicationContext ac = new ClassPathXmlApplicationCon
DataSource dataSource = (DataSource) ac.getBean("dataSource");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql="delete from product where id=p1";
jdbcTemplate.execute(sql);
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER