Spring yaml environment not working together with datasource - java

I have an application.yml that works and is being used when connecting to the database:
spring:
datasource:
driverClassName: org.postgresql.Driver
url: jdbc:postgresql://10.100.247.112:5432/ofbizBuchhandel
username: buchhandel
password: buchhandel
When I add an environment profile to the configuration:
spring:
profiles: development
datasource:
driverClassName: org.postgresql.Driver
url: jdbc:postgresql://10.100.247.112:5432/ofbizBuchhandel
username: buchhandel
password: buchhandel
I'm getting this error:
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'demoApplication': Injection of autowired
dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: com.example.DbAccess com.example.DemoApplication.dbd;
nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'dbAccess': Injection of autowired
dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: org.springframework.jdbc.core.JdbcTemplate
com.example.DbAccess.jdbcTemplate; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name
'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration':
Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: private javax.sql.DataSource
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration.dataSource;
nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'dataSource' defined in class path resource
[org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]:
Bean instantiation via factory method failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw
exception; nested exception is
org.springframework.beans.factory.BeanCreationException: Cannot
determine embedded database driver class for database type NONE. If
you want an embedded database please put a supported one on the
classpath.
From my understanding here http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-change-configuration-depending-on-the-environment the setting for profiles looks correct.

The correct answer is that I just made a typo :(

Related

unable to deploy to heroku since jhipster update

I upgrade my project from jhipster 5.8.2 to 6.5.1.
All works fine in develop mode, on my Windows computer
Now, I try to deploy on my Test heroku platform, but I have an exception :
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException:
Unable to start web server; nested exception is java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'servletEndpointRegistrar'
defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.class]:
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]:
Factory method 'servletEndpointRegistrar' threw exception;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'auditEventsEndpoint'
defined in class path resource [org/springframework/boot/actuate/autoconfigure/audit/AuditEventsEndpointAutoConfiguration.class]:
Unsatisfied dependency expressed through method 'auditEventsEndpoint' parameter 0;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'customAuditEventRepository'
defined in URL [jar:file:/app/build/libs/yvidya-0.0.1-SNAPSHOT.war!/WEB-INF/classes!/fr/yoni/yvidya/repository/CustomAuditEventRepository.class]:
Unsatisfied dependency expressed through constructor parameter 0;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'persistenceAuditEventRepository':
Cannot create inner bean '(inner bean)#75cf0de5' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager';
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name '(inner bean)#75cf0de5': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]:
Invocation of init method failed;
This is my CustomAuditEventRepository
#Repository
public class CustomAuditEventRepository implements AuditEventRepository {
private static final String AUTHORIZATION_FAILURE = "AUTHORIZATION_FAILURE";
/**
* Should be the same as in Liquibase migration.
*/
protected static final int EVENT_DATA_COLUMN_MAX_LENGTH = 255;
private final PersistenceAuditEventRepository persistenceAuditEventRepository;
private final AuditEventConverter auditEventConverter;
private final Logger log = LoggerFactory.getLogger(getClass());
public CustomAuditEventRepository(PersistenceAuditEventRepository persistenceAuditEventRepository,
AuditEventConverter auditEventConverter) {
this.persistenceAuditEventRepository = persistenceAuditEventRepository;
this.auditEventConverter = auditEventConverter;
}
For deploy, I use gitlab-ci, scripts launch are :
./gradlew compileJava -x check -PnodeInstall --no-daemon
./gradlew -Pint -Pwar clean bootWar
rm -rf build/libs/yvidya-*original.war
heroku plugins:install java
heroku deploy:jar build/libs/*.war --app yvidya-int
And my config Database :
#Configuration
#EnableJpaRepositories("fr.yoni.yvidya.repository")
#EnableJpaAuditing(auditorAwareRef = "springSecurityAuditorAware")
#EnableTransactionManagement
public class DatabaseConfiguration {
private final Logger log = LoggerFactory.getLogger(DatabaseConfiguration.class);
}
My config JDBC
spring:
profiles:
include:
- swagger
devtools:
restart:
enabled: false
livereload:
enabled: false
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:postgres://XXXX.compute.amazonaws.com:5432/YYYYYY
username: YYIYYI
password: TTTTTTTTTTTTTTTTTTTTTTTTTTTTT
hikari:
poolName: Hikari
auto-commit: false
jpa:
database-platform: io.github.jhipster.domain.util.FixedPostgreSQL95Dialect
database: POSTGRESQL
have you an idea why I can not deploy on heroku since upgrade ?

Moved config, entity and other classes

I have a big problem.
I have an application with Spring-Boot and Spring-Data.
It goes well before that i moved some classes and refactory the directory.
I have two database and before i moved classes, i had this config class for a second database:
#Configuration
#EnableTransactionManagement
#EnableJpaRepositories(
basePackages="it.phoops.rt.grigliaprocessi.util",
entityManagerFactoryRef = "organoEntityManager",
transactionManagerRef = "organoTransactionManager")
public class OrganoConfig {
#Bean(name = "organoDataSource")
#ConfigurationProperties(prefix="organo.datasource")
public DataSource organoDataSource(){
return DataSourceBuilder.create().build();
}
#Bean(name="organoEntityManager")
public LocalContainerEntityManagerFactoryBean
organoEntityManagerFactory(EntityManagerFactoryBuilder builder,
#Qualifier("organoDataSource") DataSource dataSource){
return
builder.
dataSource(organoDataSource()).
packages("it.phoops.rt.grigliaprocessi.util.entity").
persistenceUnit("organo").build();
}
#Bean("organoTransactionManager")
public PlatformTransactionManager
organoTransactionManager()
{
return new
DataSourceTransactionManager(organoDataSource());
}
}
Now, i moved in "it.phoops.rt.grigliaprocessi.organo" and i change the value of "basePackages" from "it.phoops.rt.grigliaprocessi.util" to "it.phoops.rt.grigliaprocessi.organo" and
the string "it.phoops.rt.grigliaprocessi.util.entity" to it.phoops.rt.grigliaprocessi.organo.entity".
I moved Entity, Service and Repository classes from:
it.phoops.rt.grigliaprocessi.util.controller
it.phoops.rt.grigliaprocessi.util.entity
it.phoops.rt.grigliaprocessi.util.repository
to
it.phoops.rt.grigliaprocessi.organo.controller
it.phoops.rt.grigliaprocessi.organo.entity
it.phoops.rt.grigliaprocessi.organo.repository
But now, i have this error, caused of nested exception:
Caused by: java.lang.IllegalArgumentException: Not a managed type:
class it.phoops.rt.grigliaprocessi.organo.entity.Albero
......
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'alberoRepository': Invocation of init
method failed; nested exception is
java.lang.IllegalArgumentException:
Not a managed type: class
it.phoops.rt.grigliaprocessi.organo.entity.Albero
....
Caused by:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error
creating bean with name 'alberoService': Unsatisfied dependency
expressed through field 'repo'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating
bean with name 'alberoRepository': Invocation of init method failed;
nested exception is java.lang.IllegalArgumentException: Not a managed
type: class it.phoops.rt.grigliaprocessi.organo.entity.Albero
....
Caused by:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error
creating bean with name 'alberoController': Unsatisfied dependency
expressed through field 'alberoService'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error
creating bean with name 'alberoService': Unsatisfied dependency
expressed through field 'repo'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating
bean with name 'alberoRepository': Invocation of init method failed;
nested exception is java.lang.IllegalArgumentException: Not a managed
type: class it.phoops.rt.grigliaprocessi.organo.entity.Albero
Can you help me ?
Thanks
Change you basepackages declaration from it.phoops.rt.grigliaprocessi.util to it.phoops.rt.grigliaprocessi.organo. Also please check the basepackages in componentScan in your Spring boot main class.

H2 DB Url error

I have the following H2 datasource configuration
jdbc.driverClassName=org.h2.Driver
jdbc.url=jdbc:h2:mem:test;INIT=CREATE SCHEMA IF NOT EXISTS TESTSCHEMA\;RUNSCRIPT FROM '~/schema-test.sql'\;RUNSCRIPT FROM '~/data-test.sql';DB_CLOSE_DELAY=-1
jdbc.user=xyzapp
jdbc.pass=x161jq3
When I run it, I'm getting the following error
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in com.allegis.search.ConfigTest: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceInitializer': Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.UncategorizedScriptException: Failed to execute database script; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; ne
sted exception is org.h2.jdbc.JdbcSQLException: URL format error; must be "jdbc:h2:{ {.|mem:}[name] | [file:]fileName | {tcp|ssl}:[//]server[:port][,server2[:port]]/name
}[;key=value...]" but is "jdbc:h2:mem:test" [90046-196]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193) ~[spring-beans-4.3.12.R
ELEASE.jar:4.3.12.RELEASE]
This exception is caused by incorrect jdbc.url in your properties file. You need to escape backslash character:
jdbc.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS TESTSCHEMA\\;RUNSCRIPT FROM '~/schema-test.sql'\\;RUNSCRIPT FROM '~/data-test.sql'
Try this solution and tell me whether it's helpful or not.

Spring Boot and Teradata UnsatisfiedDependencyException

I am trying to create a simple Spring application with Teradata database.
It was working with mySql database and driver but after changing to Teradata driver/databased i receive following exception :
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalArgumentException: URL must start with 'jdbc'
Application Properties :
spring.datasource.url =jdbc:teradata://servername/db
spring.datasource.username = dbc
spring.datasource.password = dbc
spring.datasource.driverClassName=com.ncr.teradata.TeraDriver
I think that is problem of Spring you might be using #Autowire on constructor but spring can't inject class with that name. To solve the problem, just define a DataSource in your context - at which point Spring will be able to inject the bean and correctly bootstrap the context.

Bean Creation exception, Injection of autowired dependency failed

I'm using spring hibernate in my application. I used all the required jar files but still getting bean creation exception saying the following:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'voteController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.mindtree.service.VoteService com.mindtree.controller.VoteController.voteService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'voteServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.mindtree.dao.VoteDao com.mindtree.serviceImpl.VoteServiceImpl.voteDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'voteDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.orm.hibernate3.HibernateTemplate com.mindtree.daoImpl.VoteDaoImpl.hibernateTemplate; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'sessionFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool
In Java, read the stacktrace from the last line, which will be the root cause of the problem. If you fix the java.lang.NoClassDefFoundError, by adding the respective jar file in your classpath, then it will work fine.
As you can see in the stacktrace:
nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool
The class GenericObjectPool is missing in your classpath.
So you have to add the commons-pool.jar to your project which contains this class.

Categories