How to start a project with spring MVC with profiles, multiple connections to the database .. and run tests with gradle?
Some time ago I'm trying to make these settings and I'm not getting what I'm doing wrong? (Sorry for my english I'm using google translate.)
Here are my settings
Dev Settings
db.driver=org.postgresql.Driver
db.url=jdbc:postgresql://localhost:5432/nextinfoerp
db.username=myuser
db.password=mypass
#Hibernate Configuration:
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.show_sql=true
hibernate.format_sql =true
entitymanager.packages.to.scan=br.com.nextinfo.erp
Test Settings
#Test properties:
db.driver=org.postgresql.Driver
db.url=jdbc:postgresql://localhost:5432/nextinfoerptestdb
db.username=myuser
db.password=mypass
#Hibernate Configuration:
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.show_sql=true
hibernate.format_sql =true
entitymanager.packages.to.scan=br.com.nextinfo.erp
Gradle Settings
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
apply plugin: 'jetty'
sourceCompatibility = 1.8
targetCompatibility = 1.8
ext.springVersion= '4.1.7.RELEASE'
ext.jacksondataTypeHibernate4 ='2.5.3'
ext.easycriteriaVersion ='3.0.0'
ext.hibernateCoreVersion ='4.3.10.Final'
ext.hibernateValidadeVersion ='5.1.3.Final'
ext.springDataVersion ='1.8.0.RELEASE'
ext.mysqlConnectionVersion= '5.1.6'
ext.servletApiVersion ='2.5'
dependencies {
testCompile 'junit:junit:4.12'
compile project(':utils')
compile"javax.servlet:jstl:1.2"
compile "org.thymeleaf:thymeleaf-spring4:2.1.2.RELEASE"
compile group: 'javax.servlet', name: 'servlet-api', version: servletApiVersion
compile group: 'nz.net.ultraq.thymeleaf', name: 'thymeleaf-layout-dialect', version: '1.2.9'
compile group: 'org.springframework.security', name: 'spring-security-web', version: '4.0.2.RELEASE'
compile group: 'org.springframework.security', name: 'spring-security-core', version: '4.0.2.RELEASE'
compile group: 'org.springframework.security', name: 'spring-security-config', version: '4.0.2.RELEASE'
compile 'org.thymeleaf.extras:thymeleaf-extras-springsecurity4:2.1.2.RELEASE'
compile group: 'org.springframework', name: 'spring-context', version: springVersion
compile group: 'org.springframework', name: 'spring-web', version: springVersion
compile group: 'org.springframework', name: 'spring-test', version: springVersion
compile group: 'org.springframework', name: 'spring-webmvc', version: springVersion
compile group: 'org.springframework.data', name: 'spring-data-jpa', version:springDataVersion
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate4', version: jacksondataTypeHibernate4
compile "org.postgresql:postgresql:9.3-1100-jdbc4"
compile group: 'uaihebert.com', name: 'EasyCriteria', version: easycriteriaVersion
compile group: 'org.hibernate', name: 'hibernate-validator', version: hibernateValidadeVersion
compile group: 'org.hibernate', name: 'hibernate-core', version: hibernateCoreVersion
compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: hibernateCoreVersion
}
class for database configuration
package br.com.nextinfo.erp.web.config;
import java.util.Properties;
import javax.annotation.Resource;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@Profile("dev")
@EnableJpaRepositories("br.com.nextinfo.erp.web.dao")
@PropertySource("classpath:dev.properties")
public class DevProfileDatabase extends AbstractDatabaseConfig implements PerfilDataBase {
private static final Logger log = LoggerFactory.getLogger(DevProfileDatabase.class);
@Resource
private Environment env;
@Bean
public DataSource dataSource() {
log.warn("settings data Source "+this.getClass().getCanonicalName());
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));
return dataSource;
}
@Bean
protected Properties hibProperties() {
Properties properties = new Properties();
properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
properties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_FORMAT_SQL));
return properties;
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
txManager.getEntityManagerFactory().getMetamodel().getEntities();
return txManager;
}
@Bean
public EntityManagerFactory entityManagerFactory() {
log.warn("settings data entity manager "+this.getClass().getCanonicalName());
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
vendorAdapter.setShowSql(true);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
factory.setDataSource(dataSource());
factory.afterPropertiesSet();
return factory.getObject();
}
}
My class incializer
import javax.servlet.Filter;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
@Order(1)
public class Inicializer extends AbstractAnnotationConfigDispatcherServletInitializer implements WebApplicationInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {ThymeleafConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {ThymeleafConfig.class,ProdProfileDatabase.class,DevProfileDatabase.class,TestProfileDatabase.class};
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
protected Filter[] getServletFilters() {
return new Filter[] { };
}
@Override
protected WebApplicationContext createRootApplicationContext() {
WebApplicationContext context =
(WebApplicationContext)super.createRootApplicationContext();
((ConfigurableEnvironment)context.getEnvironment()).setActiveProfiles("dev");
return context;
}
I'm using spring-data
the error Servlet.init() for servlet dispatcher threw exception
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [br/com/nextinfo/erp/web/config/ProdProfileDatabase.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.transaction.PlatformTransactionManager]: Factory method 'transactionManager' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [br/com/nextinfo/erp/web/config/ProdProfileDatabase.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.persistence.EntityManagerFactory]: Factory method 'entityManagerFactory' threw exception; nested exception is org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1119)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1014)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:664)
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:536)
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:490)
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
javax.servlet.GenericServlet.init(GenericServlet.java:158)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1526)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1482)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)
If I remove the annotation profiles and classes that use the annotation inicializer function normally, How do I solve this?