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);
                      
                    }
         }
        

Newton21205Asked:
Who is Participating?
 
DataCruncherConnect With a Mentor Commented:
Just to know more about your background, are you new to Spring and dependency injection?

If you are working with Spring MVC portlet, I guess you must be using a Controller ? (something like org.springframework.web.portlet.mvc.AbstractController, I'm used to the Servlet side not Portlet though, I rather mention that right away)

If that's the case (you are using a controller), it must be declared as a bean like all the rest and then you should not have to get the Spring Application Context to get specific beans as Spring can inject them where you need them for you. Note that this code is not complete and I just intended to show the general logic here:

<?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>

      <!-- Here is declared the bean for your DAO class -->
      <bean id="jdbcDao" class="com.hp.it.dao.support.JdbcDao">
          <!-- here it will call method JdbcDao.setJdbcTemplate() -->
            <property name="jdbcTemplate" ref="jdbcTemplate"/>
      </bean>
      
      <!-- Here is declared on of your Spring MVC Portlet Controller -->
      <bean name="myPortletController" class="com.mycompany.MyController">
            <!-- here it will call MyController.setJdbcDao() -->
            <property name="jdbcDao" ref="jdbcDao"/>
      </bean>

</beans>


And then your classes would look something like this:

public class JdbcDao {
    private JdbcTemplate jdbcTemplate;

      /* This method will be called by Spring to inject the JdbcTemplate */
      public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
      }
      
      public JdbcTemplate getJdbcTemplate() {
            return this.jdbcTemplate;
      }
                  
      public deleteProduct()
                if(this.jdbcTemplate != null) {
                  String sql="delete from product where id=p1";
                  jdbcTemplate.execute(sql);
            } else {
                  /* raising an error here */
            }
    }
}

public class MyController extends org.springframework.web.portlet.mvc.AbstractController {
      private JdbcDao jdbcDao;
      
      /* This method will be called by Spring to inject the JdbcDao */
      public void setJdbcDao(JdbcDao jdbcDao) {
            this.jdbcDao = jdbcDao;
      }
      
      public JdbcDao getJdbcDao() {
            return this.jdbcDao;
      }
      
      public ModelAndView handleRenderRequestInternal(RenderRequest request, RenderResponse response) {
            /* Do something */
            jdbcDao.deleteProduct();
            /* Do something else */
    }
}


Hoping this helps...
0
 
Pramod KumarConnect With a Mentor Commented:
I guess you are in a web application. Please try below once:

ServletContext servletContext = request.getSession().getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
DataSource dataSource  = (DataSource) wac.getBean("dataSource");
0
 
Newton21205Author Commented:
im working on spring mvc portlet application.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.