Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8834097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:56:09+00:00 2026-06-14T08:56:09+00:00

I’m having some trouble getting a data source bean, defined in an xml file,

  • 0

I’m having some trouble getting a data source bean, defined in an xml file, into a class in Spring using @Autowired annotation.

Would anyone have an idea what’s happening here? It seems as though dataSource in the *Impl class is not being wired in.

dao-context.xml

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:8889/spring" />
    <property name="username" value="user" />
    <property name="password" value="1234" />
</bean>

PersonDaoImpl.java

public class PersonDaoImpl implements PersonDao {

@Autowired
private DataSource dataSource;  
private JdbcTemplate jdbcTemplate = new JdbcTemplate();


public DataSource getDataSource() {
    return dataSource;
}

public void setDataSource(DataSource dataSource) {
    System.out.println("SETTING DATA SOURCE");
    this.dataSource = dataSource;
}

public List<Person> getPersonList() {
    // TODO Auto-generated method stub
    return null;
}

public void savePerson(Person person) {
    // TODO Auto-generated method stub

}

public Person getPersonById(int id) {
    System.out.println("Getting Person: " + id);        
    return null;
}

@Override
public int getPersonCount(){
    String sql = "SELCECT COUNT(*) FROM PERSON";
    jdbcTemplate.setDataSource(getDataSource());
    int count = jdbcTemplate.queryForInt(sql);
    System.out.println("Count of Person: " + count);
    return count;
}

public JdbcTemplate getJdbcTemplate() {
    return jdbcTemplate;
}

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
}

}

…and finally, the exception.

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No DataSource specified
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

java.lang.IllegalArgumentException: No DataSource specified
    org.springframework.util.Assert.notNull(Assert.java:112)
    org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:97)
    org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
    org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:382)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:456)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:464)
    org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:472)
    org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:477)
    org.springframework.jdbc.core.JdbcTemplate.queryForInt(JdbcTemplate.java:486)
    com.myPackage.rest.dao.impl.PersonDaoImpl.getPersonCount(PersonDaoImpl.java:50)
    com.myPackage.rest.HomeController.home(HomeController.java:36)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

Any help would be greatly appreciated, cheers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:56:10+00:00Added an answer on June 14, 2026 at 8:56 am

    Your data source needs to be set in the JdbcTemplate (I don’t see you doing that). Also since you put the @Autowired annotation on the field, I don’t think it uses the setter so that would be why your setDataSource method is not being called. Since your exception indicates that you called jdbcTemplate.execute and dataSource is null, this makes sense.

    If you look here: http://static.springsource.org/spring/docs/current/spring-framework-reference/html/jdbc.html there is an example of autowiring a data source, but the idea is:

    public class  MyDao {
    
        private JdbcTemplate jdbcTemplate;
    
        @Autowired
        public void setDataSource(DataSource dataSource) {
            this.jdbcTemplate = new JdbcTemplate(dataSource);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have just tried to save a simple *.rtf file with some websites and
In my XML file chapters tag has more chapter tag.i need to display chapters
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm parsing an XML file, the creators of it stuck in a bunch social
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.