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:
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 DICode:<?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>, 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?Code:GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); java.sql.Driver driver = ctx.getBean("driverClassName", java.sql.Driver.class);
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!


Reply With Quote