[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

No bean named 'dataSource' is defined

Posted on 2011-10-12
3
Medium Priority
?
2,472 Views
Last Modified: 2012-05-12
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);
                      
                    }
         }
        

0
Comment
Question by:Newton21205
3 Comments
 
LVL 10

Assisted Solution

by:Pramod Kumar
Pramod Kumar earned 800 total points
ID: 36960177
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
 

Author Comment

by:Newton21205
ID: 36963903
im working on spring mvc portlet application.
0
 
LVL 2

Accepted Solution

by:
DataCruncher earned 1200 total points
ID: 36966360
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question