Announcement Announcement Module
No announcement yet.
Getting data access in MVC app Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Getting data access in MVC app

    I created a basic MVC application, it displays the default page, all is well. Now I want to add database support. I am trying to follow the method used in the Pro Spring 3 book where they define a dataSource bean of the class org.apache.commons.dbcp.BasicDataSource.

    So, I am assuming that I want to define this bean in the root-context.xml file, so this is what I have:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
    	<!-- Root Context: defines shared resources visible to all other web components -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        	<property name="driverClassName" value=""/>
        	<property name="url" value="jdbc:sqlserver://localhost;databaseName=ProSpringCh8"/>
        	<property name="username" value="****"/>
        	<property name="password" value="****"/>
        <context:property-placeholder location="" />
    So, the next step in my process of understanding Spring is getting the HomeController to get access to this dataSource bean. I first considered the ways the none MVC apps where using DI
    GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
    java.sql.Driver driver = ctx.getBean("driverClassName", java.sql.Driver.class);
    , but I don't have a GenericXmlApplicationContext, or at least I don't think that is the correct way to get the bean. Then I looked into adding a bean definition in the root-context.xml, but that didn't go over too well, either. So... How do I go about getting this bean in my controller?

    Mind you, I know this is NOT the ideal way of doing things, I am simply trying to take baby steps as to understand what is really going on!

  • #2
    public class YourController {
      private DataSource dataSource;
    I suggest a read of chapter 3 of the reference guide and also a read on dependency injection in general.

    I strongly suggest creating a layered application as doing direct dataaccess from the controller is a red flag in my book (there should at least be a repository) and if you use plain JDBC you are probably better of using a JdbcTemplate. A final note next to the datasource to do something meaningful (like storing data) you also need a transactionmanager and transaction configuration (see the transaction chapter in the reference guide for more information).


    • #3

      Thank you. Sometimes the obvious simply eludes me...

      I do not plan to do direct data access from the controller, nor use JDBC directly. I am simply trying to glue together all these different pieces of info in my head. The goal, which has been achieved, was to run the controller in the debugger and see that the dataSource was an actual object. Now I can move on to adding some more layers... Baby steps!

      Thank you for your help, it is truly appreciated!