title: Configuration of eclipselink + Spring MVC + MySql using Java Configuration (No XML configuration)
body: <p>I am trying to integrate <code>eclipselink JPA</code> into my Spring Web application.
  <strong>Is there any good guide oh how to configure eclipseLink for MySql database in Spring MVC using Java based configuration only?</strong>. No dispatcher-servlet.xml. </p>
  
  <p>I am familiar with Java based Spring configuration. (Using <code>@Configuration, @EnableWebMvc, @ComponentScan, @EnableTransactionManagement</code> etc). But not familiar with eclipseLink java based configuration in Spring MVC. </p>
  
  <p>Following is the <code>pom.xml</code></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/maven-v4_0_0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;roster3&lt;/groupId&gt;
      &lt;artifactId&gt;roster3&lt;/artifactId&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;roster3 Maven Webapp&lt;/name&gt;
      &lt;url&gt;http://maven.apache.org&lt;/url&gt;
      &lt;properties&gt;
          &lt;spring.version&gt;4.0.2.RELEASE&lt;/spring.version&gt;
          &lt;hibernate.version&gt;4.3.4.Final&lt;/hibernate.version&gt;
          &lt;hibernate.validator.version&gt;5.1.0.Final&lt;/hibernate.validator.version&gt;
          &lt;mysql.connector.version&gt;5.1.29&lt;/mysql.connector.version&gt;
          &lt;eclipselink.version&gt;2.5.0&lt;/eclipselink.version&gt;
          &lt;aspectj.version&gt;1.5.4&lt;/aspectj.version&gt;
      &lt;/properties&gt;
      &lt;repositories&gt;
          &lt;repository&gt;
              &lt;id&gt;oss.sonatype.org&lt;/id&gt;
              &lt;name&gt;OSS Sonatype Staging&lt;/name&gt;
              &lt;url&gt;https://oss.sonatype.org/content/groups/staging&lt;/url&gt;
          &lt;/repository&gt;
      &lt;/repositories&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;version&gt;3.8.1&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Spring dependencies --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-tx&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt; --&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Servlet &amp; jstl --&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;dependency&gt;
              &lt;groupId&gt;jstl&lt;/groupId&gt;
              &lt;artifactId&gt;jstl&lt;/artifactId&gt;
              &lt;version&gt;1.2&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Hibernate Validation &amp; DB Validation --&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;version&gt;${mysql.connector.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
              &lt;version&gt;${hibernate.validator.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
  
          &lt;!-- eclipselink --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.eclipse.persistence&lt;/groupId&gt;
              &lt;artifactId&gt;eclipselink&lt;/artifactId&gt;
              &lt;version&gt;${eclipselink.version}&lt;/version&gt;
              &lt;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;org.eclipse.persistence&lt;/groupId&gt;
                      &lt;artifactId&gt;commonj.sdo&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.eclipse.persistence&lt;/groupId&gt;
              &lt;artifactId&gt;javax.persistence&lt;/artifactId&gt;
              &lt;version&gt;2.1.0&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-instrument&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;aspectj&lt;/groupId&gt;
              &lt;artifactId&gt;aspectjrt&lt;/artifactId&gt;
              &lt;version&gt;${aspectj.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.aspectj&lt;/groupId&gt;
              &lt;artifactId&gt;aspectjweaver&lt;/artifactId&gt;
              &lt;version&gt;1.7.4&lt;/version&gt;
          &lt;/dependency&gt;       
  
      &lt;/dependencies&gt;
      &lt;build&gt;
  
          &lt;!-- To define the plugin version in your parent POM --&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.tomcat.maven&lt;/groupId&gt;
                  &lt;artifactId&gt;tomcat7-maven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.3-SNAPSHOT&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;url&gt;http://127.0.0.1:8080/roster3&lt;/url&gt;
                  &lt;/configuration&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.1&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;source&gt;1.7&lt;/source&gt;
                      &lt;target&gt;1.7&lt;/target&gt;
                      &lt;encoding&gt;UTF-8&lt;/encoding&gt;
                      &lt;argLine&gt;-javaagent:eclipselink.jar&lt;/argLine&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;!-- This plugin ensures the EclipseLink static weaving --&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;eclipselink-staticweave-maven-plugin&lt;/artifactId&gt;
                  &lt;groupId&gt;au.com.alderaan&lt;/groupId&gt;
                  &lt;version&gt;1.0.4&lt;/version&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;weave&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;phase&gt;process-classes&lt;/phase&gt;
                          &lt;configuration&gt;
                              &lt;logLevel&gt;ALL&lt;/logLevel&gt;
                              &lt;!-- &lt;includeProjectClasspath&gt;true&lt;/includeProjectClasspath&gt; --&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
                  &lt;dependencies&gt;
                      &lt;dependency&gt;
                          &lt;groupId&gt;org.eclipse.persistence&lt;/groupId&gt;
                          &lt;artifactId&gt;eclipselink&lt;/artifactId&gt;
                          &lt;version&gt;${eclipselink.version}&lt;/version&gt;
                      &lt;/dependency&gt;
                  &lt;/dependencies&gt;
              &lt;/plugin&gt;
  
  
          &lt;/plugins&gt;
          &lt;pluginManagement&gt;
              &lt;plugins&gt;
                  &lt;!--This plugin's configuration is used to store Eclipse m2e settings 
                      only. It has no influence on the Maven build itself. --&gt;
                  &lt;plugin&gt;
                      &lt;groupId&gt;org.eclipse.m2e&lt;/groupId&gt;
                      &lt;artifactId&gt;lifecycle-mapping&lt;/artifactId&gt;
                      &lt;version&gt;1.0.0&lt;/version&gt;
                      &lt;configuration&gt;
                          &lt;lifecycleMappingMetadata&gt;
                              &lt;pluginExecutions&gt;
                                  &lt;pluginExecution&gt;
                                      &lt;pluginExecutionFilter&gt;
                                          &lt;groupId&gt;
                                              au.com.alderaan
                                          &lt;/groupId&gt;
                                          &lt;artifactId&gt;
                                              eclipselink-staticweave-maven-plugin
                                          &lt;/artifactId&gt;
                                          &lt;versionRange&gt;
                                              [1.0.4,)
                                          &lt;/versionRange&gt;
                                          &lt;goals&gt;
                                              &lt;goal&gt;weave&lt;/goal&gt;
                                          &lt;/goals&gt;
                                      &lt;/pluginExecutionFilter&gt;
                                      &lt;action&gt;
                                          &lt;execute&gt;
                                              &lt;runOnIncremental&gt;true&lt;/runOnIncremental&gt;
                                          &lt;/execute&gt;
                                      &lt;/action&gt;
                                  &lt;/pluginExecution&gt;
                              &lt;/pluginExecutions&gt;
                          &lt;/lifecycleMappingMetadata&gt;
                      &lt;/configuration&gt;
                  &lt;/plugin&gt;
              &lt;/plugins&gt;
          &lt;/pluginManagement&gt;
          &lt;finalName&gt;roster3&lt;/finalName&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>Following is what I tried as of now.</p>
  
  <p>Configuration class <code>Config.java</code></p>
  
  <pre><code>package com.ecw.roster.config;
  
  import java.util.Properties;
  
  
  import org.eclipse.persistence.platform.database.DatabasePlatform;
  import org.eclipse.persistence.platform.database.MySQLPlatform;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.context.MessageSource;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.PropertySource;
  import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
  import org.springframework.context.support.ResourceBundleMessageSource;
  import org.springframework.orm.jpa.JpaTransactionManager;
  import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  import org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect;
  import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
  import org.springframework.transaction.PlatformTransactionManager;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  import org.springframework.web.servlet.view.JstlView;
  import org.springframework.web.servlet.view.UrlBasedViewResolver;
  
  @Configuration //Marks this class as configuration
  @EnableWebMvc
  //Specifies which package to scan
  @ComponentScan("com.ecw.roster")
  @PropertySource(value = "classpath:application.properties")
  //Enable Spring's annotations
  @EnableTransactionManagement
  public class Config extends WebMvcConfigurerAdapter{
  
      private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
      private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
      private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan";
  
      private static final String PROPERTY_NAME_DATABASEPLATFORM = "databasePlatform";
      private static final String PROPERTY_NAME_GENERATE_DDL = "generateDdl";
      private static final String PROPERTY_NAME_SHOW_SQL = "showSql";
  
      @Value("${jdbc.driverClassName}")
      private String driverClassName;
  
      @Value("${jdbc.url}")
      private String url;
  
      @Value("${jdbc.username}")
      private String username;
  
      @Value("${jdbc.password}")
      private String password;
  
      @Value("${hibernate.dialect}")
      private String hibernate_dialect;
  
      @Value("${hibernate.show_sql}")
      private String hibernate_show_sql;
  
      @Value("${entitymanager.packages.to.scan}")
      private String entity_package_to_scan;
  
      @Bean
      public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() {
          return new PropertySourcesPlaceholderConfigurer();
      }
  
      @Bean
      public UrlBasedViewResolver setupViewResolver() {
          UrlBasedViewResolver resolver = new UrlBasedViewResolver();
          System.out.println("Roster 3 : Initializing UrlBasedViewResolver........");
          resolver.setPrefix("/WEB-INF/views/");
          resolver.setSuffix(".jsp");
          resolver.setViewClass(JstlView.class);
          return resolver;
      }
  
      @Bean
      public PlatformTransactionManager transactionManager(){     
          System.out.println("Roster 3 : LocalContainerEntityManagerFactoryBean *************");  
         JpaTransactionManager transactionManager = new JpaTransactionManager();       
         transactionManager.setEntityManagerFactory(entityManagerFactoryBean().getObject());       
         return transactionManager;
      }
  
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {          
          LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();       
          em.setPackagesToScan(new String[] { "com.ecw.roster.beans" });
          em.setPersistenceUnitName("roster3");       
          DatabasePlatform dp = new MySQLPlatform();        
          em.setJpaVendorAdapter(getEclipseLinkJpaVendorAdapter());       
          return em;
      }   
  
      private EclipseLinkJpaVendorAdapter getEclipseLinkJpaVendorAdapter(){
          System.out.println("Roster 3 : EclipseLinkJpaVendorAdapter initialization ***************");
  
          EclipseLinkJpaVendorAdapter vendorAdapter = new EclipseLinkJpaVendorAdapter();      
          vendorAdapter.setDatabasePlatform("org.eclipse.persistence.platform.database.MySQLPlatform");
          vendorAdapter.setGenerateDdl(false);
          vendorAdapter.setShowSql(true);
          return vendorAdapter;
      }
  
      private Properties eclipseLinkProperty(){
          Properties properties = new Properties();       
          properties.put("eclipselink.deploy-on-startup", "true");
          properties.put("eclipselink.ddl-generation", "create-or-extend-tables");
          properties.put("eclipselink.ddl-generation.output-mode", "database");
          properties.put("eclipselink.weaving", "static");
          properties.put("eclipselink.weaving.lazy", "true");
          properties.put("eclipselink.weaving.internal", "true");
          properties.put("eclipselink.logging.level", "SEVERE");
          properties.put("eclipselink.query-results-cache.type", "WEAK");
          properties.put("eclipselink.jdbc.batch-writing", "JDBC");
          properties.put("eclipselink.jdbc.batch-writing.size", "1000");
          return properties;
      }
  
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
          System.out.println("In Resouce Handler");
          registry.addResourceHandler("/resources/**").addResourceLocations("/resources/defaultTheme/");
      }
  
      @Bean
      public MessageSource messageSource() { 
          ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
          messageSource.setBasename("/WEB-INF/messages/messages/");
          return messageSource;
      }
  
  }
  </code></pre>
  
  <p>persistence.xml file for eclipselink:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.1"
      xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"&gt;
      &lt;persistence-unit name="roster3" transaction-type="RESOURCE_LOCAL"&gt;
          &lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt;
          &lt;class&gt;com.ecw.roster.beans.Roster&lt;/class&gt;
          &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
          &lt;properties&gt;
              &lt;property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /&gt;
              &lt;property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/roster" /&gt;
              &lt;property name="javax.persistence.jdbc.user" value="root" /&gt;
              &lt;property name="javax.persistence.jdbc.password" value="mysqladmin" /&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>I am able to build &amp; deploy the project but I am getting connection leaks &amp; then my context is stopped by tomcat container.</p>
  
  <p>Please advice me what I am doing wrong with it.</p>
  
  <p>Thanks</p>