Announcement Announcement Module
Collapse
No announcement yet.
Getting data access in MVC app Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    	
    	<!-- Root Context: defines shared resources visible to all other web components -->
    		
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        	<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
        	<property name="url" value="jdbc:sqlserver://localhost;databaseName=ProSpringCh8"/>
        	<property name="username" value="****"/>
        	<property name="password" value="****"/>
        </bean>
        
        <context:property-placeholder location="jdbc.properties" />
    
    </beans>
    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
    Code:
    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
    Code:
    @Controller
    public class YourController {
    
      @Autowired
      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).

    Comment


    • #3
      Marten,

      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!

      Comment

      Working...
      X