title: How to add mysql datasource to applicationContext.xml
body: <p>I am making a dynamic web application.
  In my application I use Spring and Hibernate.</p>
  
  <p>Here is my pom.xml</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;groupId&gt;rasika.gayan.nought&lt;/groupId&gt;
  &lt;artifactId&gt;mit-site-temp&lt;/artifactId&gt;
  &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  &lt;packaging&gt;war&lt;/packaging&gt;
  
  &lt;!-- spring framework spring framework spring framework --&gt;
  
  &lt;!-- Shared version number properties --&gt;
  &lt;properties&gt;
      &lt;org.springframework.version&gt;4.1.1.RELEASE&lt;/org.springframework.version&gt;
  &lt;/properties&gt;
  
  &lt;dependencies&gt;
      &lt;!-- Core utilities used by other modules. Define this if you use Spring 
          Utility APIs (org.springframework.core.*/org.springframework.util.*) --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Expression Language (depends on spring-core) Define this if you use 
          Spring Expression APIs (org.springframework.expression.*) --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-expression&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Bean Factory and JavaBeans utilities (depends on spring-core) Define 
          this if you use Spring Bean APIs (org.springframework.beans.*) --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-beans&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Aspect Oriented Programming (AOP) Framework (depends on spring-core, 
          spring-beans) Define this if you use Spring AOP APIs (org.springframework.aop.*) --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-aop&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Application Context (depends on spring-core, spring-expression, spring-aop, 
          spring-beans) This is the central artifact for Spring's Dependency Injection 
          Container and is generally always defined --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Various Application Context utilities, including EhCache, JavaMail, 
          Quartz, and Freemarker integration Define this if you need any of these integrations --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-context-support&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Transaction Management Abstraction (depends on spring-core, spring-beans, 
          spring-aop, spring-context) Define this if you use Spring Transactions or 
          DAO Exception Hierarchy (org.springframework.transaction.*/org.springframework.dao.*) --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-tx&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- JDBC Data Access Library (depends on spring-core, spring-beans, spring-context, 
          spring-tx) Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*) --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA, 
          and iBatis. (depends on spring-core, spring-beans, spring-context, spring-tx) 
          Define this if you need ORM (org.springframework.orm.*) --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Object-to-XML Mapping (OXM) abstraction and integration with JAXB, 
          JiBX, Castor, XStream, and XML Beans. (depends on spring-core, spring-beans, 
          spring-context) Define this if you need OXM (org.springframework.oxm.*) --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-oxm&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Web application development utilities applicable to both Servlet and 
          Portlet Environments (depends on spring-core, spring-beans, spring-context) 
          Define this if you use Spring MVC, or wish to use Struts, JSF, or another 
          web framework with Spring (org.springframework.web.*) --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Spring MVC for Servlet Environments (depends on spring-core, spring-beans, 
          spring-context, spring-web) Define this if you use Spring MVC with a Servlet 
          Container such as Apache Tomcat (org.springframework.web.servlet.*) --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Spring MVC for Portlet Environments (depends on spring-core, spring-beans, 
          spring-context, spring-web) Define this if you use Spring MVC with a Portlet 
          Container (org.springframework.web.portlet.*) --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-webmvc-portlet&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Support for testing Spring applications with tools such as JUnit and 
          TestNG This artifact is generally always defined with a 'test' scope for 
          the integration testing framework and unit testing stubs --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-test&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;!-- javax servlet --&gt;
  
       &lt;dependency&gt;
          &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
          &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
          &lt;version&gt;3.0.1&lt;/version&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;!-- javax servlet jsp --&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;javax.servlet.jsp&lt;/groupId&gt;
          &lt;artifactId&gt;javax.servlet.jsp-api&lt;/artifactId&gt;
          &lt;version&gt;2.2.1&lt;/version&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;!-- hibernate --&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
          &lt;version&gt;4.3.6.Final&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- spring data jpa --&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
          &lt;artifactId&gt;spring-data-jpa&lt;/artifactId&gt;
          &lt;version&gt;1.7.0.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- jdbc driver --&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;mysql&lt;/groupId&gt;
          &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
          &lt;version&gt;5.1.6&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;commons-dbcp&lt;/groupId&gt;
          &lt;artifactId&gt;commons-dbcp&lt;/artifactId&gt;
          &lt;version&gt;1.4&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;javax.persistence&lt;/groupId&gt;
          &lt;artifactId&gt;persistence-api&lt;/artifactId&gt;
          &lt;version&gt;1.0&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate.javax.persistence&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-jpa-2.0-api&lt;/artifactId&gt;
          &lt;version&gt;1.0.1.Final&lt;/version&gt;
      &lt;/dependency&gt;
  
  
      &lt;!-- slf4j --&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
          &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
          &lt;version&gt;1.7.7&lt;/version&gt;
      &lt;/dependency&gt;
  
  &lt;/dependencies&gt;
  
  &lt;!-- jetty and compiler --&gt;
  
  &lt;build&gt;
      &lt;plugins&gt;
  
          &lt;plugin&gt;
              &lt;groupId&gt;org.eclipse.jetty&lt;/groupId&gt;
              &lt;artifactId&gt;jetty-maven-plugin&lt;/artifactId&gt;
              &lt;version&gt;9.2.3.v20140905&lt;/version&gt;
          &lt;/plugin&gt;
  
          &lt;plugin&gt;
              &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
              &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
              &lt;version&gt;3.2&lt;/version&gt;
              &lt;configuration&gt;
                  &lt;source&gt;1.6&lt;/source&gt;
                  &lt;target&gt;1.6&lt;/target&gt;
              &lt;/configuration&gt;
          &lt;/plugin&gt;
  
      &lt;/plugins&gt;
  &lt;/build&gt;
  </code></pre>
  
  <p></p>
  
  <p>in my pom.xml I include <strong>org.hibernate.javax.persistence</strong> and <strong>javax.persistence</strong> <strong>.
  Is that correct????</strong></p>
  
  <p>Then here is my applicationContext.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"
  xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
      http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"&gt;
  
  
  &lt;!-- this will scan other all in directory --&gt;
  &lt;context:component-scan base-package="com.rasika.gayan"&gt;
      &lt;context:exclude-filter type="annotation"
          expression="org.springframework.stereotype.Controller" /&gt;
  &lt;/context:component-scan&gt;
  
  &lt;!-- ***************jpa configeration ********************** --&gt;
  
  &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
  &lt;bean class="org.springframework.orm.jpa.JpaTransactionManager"
      id="transactionManager"&gt;
      &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"&gt;
      &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
      &lt;property name="url" value="jdbc:mysql://localhost:3306/finalyearproject" /&gt;
      &lt;property name="username" value="root" /&gt;
      &lt;property name="password" value="r00t" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
  
      &lt;property name="annotatedClasses"&gt;
          &lt;list&gt;
              &lt;value&gt;com.xxx.yyy.entity.Complains&lt;/value&gt;
              &lt;value&gt;com.xxx.yyy.entity.LogFileContent&lt;/value&gt;
              &lt;value&gt;com.xxx.yyy.entity.OrderDetail&lt;/value&gt;
              &lt;value&gt;com.xxx.yyy.entity.SendDeliverableDetail&lt;/value&gt;
              &lt;value&gt;com.xxx.yyy.entity.SkillDetail&lt;/value&gt;
              &lt;value&gt;com.xxx.yyy.entity.TaskDetail&lt;/value&gt;
              &lt;value&gt;com.xxx.yyy.entity.TaskRateDetail&lt;/value&gt;
              &lt;value&gt;com.xxx.yyy.entity.TempUser&lt;/value&gt;
              &lt;value&gt;com.xxx.yyy.entity.UserComments&lt;/value&gt;
              &lt;value&gt;com.xxx.yyy.entity.UserDetail&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
              &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  
  
  
  &lt;!-- **org.springframework.jdbc.datasource.DriverManagerDataSource --&gt;
  &lt;!-- org.apache.commons.dbcp.BasicDataSource --&gt;
  </code></pre>
  
  <p></p>
  
  <p>after I build my application I got his error.........................................</p>
  
  <pre><code>        SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
          SLF4J: Defaulting to no-operation (NOP) logger implementation
          SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
          [INFO] Scanning for projects...
          [INFO]                                                                         
          [INFO] ------------------------------------------------------------------------
          [INFO] Building mit-site-temp 0.0.1-SNAPSHOT
          [INFO] ------------------------------------------------------------------------
          [INFO] 
          [INFO] &gt;&gt;&gt; jetty-maven-plugin:9.2.3.v20140905:run (default-cli) @ mit-site-temp &gt;&gt;&gt;
          [INFO] 
          [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ mit-site-temp ---
          [debug] execute contextualize
          [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
          [INFO] Copying 0 resource
          [INFO] 
          [INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ mit-site-temp ---
          [WARNING] The POM for org.apache.maven:maven-plugin-api:jar:2.0.9 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
          [WARNING] The POM for org.apache.maven:maven-artifact:jar:2.0.9 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
          [WARNING] The POM for org.apache.maven:maven-core:jar:2.0.9 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
          [INFO] Nothing to compile - all classes are up to date
          [INFO] 
          [INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ mit-site-temp ---
          [debug] execute contextualize
          [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
          [INFO] Copying 0 resource
          [INFO] 
          [INFO] --- maven-compiler-plugin:3.2:testCompile (default-testCompile) @ mit-site-temp ---
          [INFO] Nothing to compile - all classes are up to date
          [INFO] 
          [INFO] &lt;&lt;&lt; jetty-maven-plugin:9.2.3.v20140905:run (default-cli) @ mit-site-temp &lt;&lt;&lt;
          [INFO] 
          [INFO] --- jetty-maven-plugin:9.2.3.v20140905:run (default-cli) @ mit-site-temp ---
          2014-10-24 20:55:54.158:INFO::main: Logging initialized @3946ms
          [INFO] Configuring Jetty for project: mit-site-temp
          [INFO] webAppSourceDirectory not set. Trying src\main\webapp
          [INFO] Reload Mechanic: automatic
          [INFO] Classes = D:\MITSite\target\classes
          [INFO] Context path = /
          [INFO] Tmp directory = D:\MITSite\target\tmp
          [INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
          [INFO] Web overrides =  none
          [INFO] web.xml file = file:/D:/MITSite/src/main/webapp/WEB-INF/web.xml
          [INFO] Webapp directory = D:\MITSite\src\main\webapp
          2014-10-24 20:55:54.230:INFO:oejs.Server:main: jetty-9.2.3.v20140905
          2014-10-24 20:55:58.044:INFO:/:main: No Spring WebApplicationInitializer types detected on classpath
          2014-10-24 20:55:58.096:INFO:/:main: Initializing Spring root WebApplicationContext
          log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
          log4j:WARN Please initialize the log4j system properly.
          log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
          2014-10-24 20:55:58.503:WARN:oejw.WebAppContext:main: Failed startup of context o.e.j.m.p.JettyWebAppContext@4857e34a{/,file:/D:/MITSite/src/main/webapp/,STARTING}{file:/D:/MITSite/src/main/webapp/}
      org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined
  
      ////// minimize the error log ................
  
      at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
      Caused by: 
      org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined
  
      ////// minimize the error log .......
  
      at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
      2014-10-24 20:55:58.508:WARN:oejsh.RequestLogHandler:main: !RequestLog
      2014-10-24 20:55:58.526:WARN:oejuc.AbstractLifeCycle:main: FAILED ServerConnector@45c01e6{HTTP/1.1}{0.0.0.0:8080}: java.net.BindException: Address already in use: bind
      java.net.BindException: Address already in use: bind
  
  
  ////// minimize the error log.......
  
      at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
      2014-10-24 20:55:58.526:WARN:oejuc.AbstractLifeCycle:main: FAILED org.eclipse.jetty.maven.plugin.MavenServerConnector@3480a546: java.net.BindException: Address already in use: bind
      java.net.BindException: Address already in use: bind
  
  ///// minimize the error log..........
  at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
  2014-10-24 20:55:58.526:WARN:oejuc.AbstractLifeCycle:main: FAILED org.eclipse.jetty.maven.plugin.JettyServer@26ed15da: java.net.BindException: Address already in use: bind
  java.net.BindException: Address already in use: bind
  
  ///// at last in error log
  
  [INFO] Jetty server exiting.
  [INFO] ------------------------------------------------------------------------
  [INFO] BUILD FAILURE
  [INFO] ------------------------------------------------------------------------
  [INFO] Total time: 7.172s
  [INFO] Finished at: Fri Oct 24 20:55:58 IST 2014
  [INFO] Final Memory: 31M/260M
  
  [INFO] ------------------------------------------------------------------------
  [ERROR] Failed to execute goal org.eclipse.jetty:jetty-maven-plugin:9.2.3.v20140905:run (default-cli) on project mit-site-temp: Failure: Address already in use: bind -&gt; [Help 1]
  [ERROR] 
  [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
  [ERROR] Re-run Maven using the -X switch to enable full debug logging.
  [ERROR] 
  [ERROR] For more information about the errors and possible solutions, please read the following articles:
  [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
  </code></pre>
  
  <p><strong>Can please help me on this ???</strong></p>