I try to migrate an app from spring-boot1 to spring-boot 2, but I have a lot of difficulty with the spring-boot part:
I have this log:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration$LiquibaseConfiguration': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Cannot find changelog location: class path resource [db/changelog/db.changelog-master.yaml] (please add changelog or check your Liquibase configurati
with this yaml:
spring:
application:
name: xxx
jpa:
generate-ddl: false
liquibase:
change-log: "classpath:/liquibase/xxx-db/db.changelog.json"
datasource:
platform: oracle
liquibase and spring-boot 2 seem ignore the liquibase part of my application.yml
any idea?
my pom:
<spring-boot.version>2.0.3.RELEASE</spring-boot.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
<liquibase.version>3.4.2</liquibase.version>
So, I have found:
I was using this:
spring.config.location=other.yml
whiche was working find in springboot1, but not anymore in springboot2
during the migration from spring-boot 1 to spring-boot 2, the behavior of the parameter spring.config.location ( where the application.yml- like file are) have change. (source:https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide)
In springboot1, spring.config.location=other.yml add to the default file application.yml other.yml file
In springboot2, spring.config.location replace the default application.yml file by the other.yml file.
To correct, I will need to do this:
spring.config.location=application.yml,other.yml
Related
My Spring Boot version is 2.3.4-RELEASE. I configure DataSource this way in application-dev.yml:
spring:
profiles: dev
datasource:
master:
driver-class-name: com.mysql.jdbc.Driver
url: myUrl
slave:
driver-class-name: com.mysql.jdbc.Driver
url: myUrl
When I activate the 'dev' profile, I can start my Spring Boot container successfully, but when I restart it, I get this error:
Description:
Failed to configure a DataSource: 'url' attribute is not specified and
no embedded datasource could be configured.
Reason:
Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently
active).
I tried to add the config: spring.datasource.url. It works, but I need to distinguish master and slave. How can I do that?
You must activate your profile using property spring.profiles.active
If you have database settings to be loaded from a particular profile
you may need to activate it (no profiles are currently active).
Read set the active Spring profiles to see how to do it
I have tried to create the environment specific configuration in micronaut
with main application.yml as
micronaut:
application:
name: xyz
server:
port: 9090
environments: local
and local configuration file as
with name as application-local.yml
xyz:
aws:
accessKey: <access_key>
secretKey: <secret_key>
In code I am trying to access as
#Value("${xyz.aws.accessKey}")
I while trying to access them in code getting following error
Message: Error resolving field value [${xyz.aws.accessKey}]. Property doesn't exist or cannot be converted
I found out that there is no such properties in micronaut similar to spring's
spring.profiles.active
here instead we have to pass the external file as VM options instead
-Dmicronaut.environments=local
then it started to work
I am developing a desktop application with spring boot using the STS 4 IDE. Everything works fine inside STS, I have different properties files for different profiles and the main properties file application.properties all inside the resources folder
src/main/resources
application.properties
application-dev.properties
application-prod.properties
In the application.properties I define the environment and the packages to scan to find the entities for the two app databases:
spring.profiles.active=dev
wms.basepackage=com.wms
app.basepackage=es.app
I run the app using boot dasboard and everything is OK. The problem is when exporting the JAR and I execute it from the command using the instruction
java -jar -Dspring.profiles.active=dev RunableApp4.jar
The application fails with the error:
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [es.app.InterfaceApplication]; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'wms.basepackage' in value "${wms.basepackage}"
I noticed in the log Differences in steps for run the application from the command line than when run using STS:
STS STEPS (USING BOOT DASHBOARD):
es.app.InterfaceApplication Starting InterfaceApplication on matias-pc with PID 27945 (/home/matias/desarrollo/InterfaceApp/bin/main started by matias in /home/matias/desarrollo/InterfaceApp)
es.app.InterfaceApplication The following profiles are active: dev
o.s.boot.SpringApplication Loading source class es.app.InterfaceApplication
o.s.b.c.c.ConfigFileApplicationListener Activated activeProfiles dev
o.s.b.c.c.ConfigFileApplicationListener Profiles already activated, '[dev]' will not be applied
o.s.b.c.c.ConfigFileApplicationListener Loaded config file 'file:/home/matias/desarrollo/InterfaceApp/bin/main/application.properties' (classpath:/application.properties)
o.s.b.c.c.ConfigFileApplicationListener Loaded config file 'file:/home/matias/desarrollo/InterfaceApp/bin/main/application-dev.properties' (classpath:/application-dev.properties) for profile dev
m.s.d.r.c.RepositoryConfigurationDelegate Bootstrapping Spring Data JPA repositories in DEFAULT mode.
m.s.d.r.c.RepositoryConfigurationDelegate Finished Spring Data repository scanning in 60ms. Found 13 JPA repository interfaces.
m.s.d.r.c.RepositoryConfigurationDelegate Bootstrapping Spring Data JPA repositories in DEFAULT mode.
m.s.d.r.c.RepositoryConfigurationDelegate Finished Spring Data repository scanning in 8ms. Found 4 JPA repository interfaces.
The ConfigFileApplicationListener enters the scene and loads the main
application.properties file and the application-dev.properties environment file. Then the RepositoryConfigurationDelegate correctly loads the repositories
COMMAND LINE STEPS:
java -jar -Dspring.profiles.active=dev RunableApp4.jar
13:07:49.453 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Found key 'spring.profiles.active' in PropertySource 'systemProperties' with value of type String
13:07:49.455 [main] DEBUG org.springframework.core.env.StandardEnvironment - Activating profiles [dev]
13:07:49.455 [main] DEBUG org.springframework.core.env.StandardEnvironment - Activating profiles [dev]
3:07:49.537 [main] INFO es.app.InterfaceApplication - Starting InterfaceApplication on matias-pc with PID 32173 (/home/matias/Documentos/JARS/RunableApp4.jar started by matias in /home/matias/Documentos/JARS)
13:07:49.537 [main] DEBUG es.app.InterfaceApplication - Running with Spring Boot, Spring
13:07:49.537 [main] INFO es.app.InterfaceApplication - The following profiles are active: dev
13:07:49.538 [main] DEBUG org.springframework.boot.SpringApplication - Loading source class es.app.InterfaceApplication
13:07:49.580 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext#7a1ebcd8
13:07:49.584 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
13:07:49.642 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/home/matias/Documentos/JARS/RunableApp4.jar!/mx/gm3s/configuracion/ConfiguracionDSErp.class]
.
.
Read candidate components
13:07:49.861 [main] WARN org.springframework.context.annotation.AnnotationConfigApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [es.app.InterfaceApplication]; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'wms.basepackage' in value "${wms.basepackage}"
13:07:49.864 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [es.app.InterfaceApplication]; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'wms.basepackage' in value "${wms.basepackage}"
The ConfigFileApplicationListener and RepositorConfigurationDelegate do not arrive at the scene, The AnnotationConfigApplicationContext starts to scan components and fails due to being unable to resolve properties values.
I already tried to send properties file as argument but the result is the same:
java -jar -Dspring.profiles.active=dev -Dspring.config.location=file:/InterfaceApp/src/main/resources/application.properties RunableApp4.jar
The main classes are:
#SpringBootApplication
#Import(ConfigInterfaceApp.class)
#EnableScheduling
public class InterfaceApp {
public static void main(String[] args) {
SpringApplication.run(InterfaceApp.class, args);
}
#Configuration
#EnableAutoConfiguration
#ComponentScan(basePackages = {"${wms.basepackage}", "${app.basepackage}"})
public class ConfigInterfaceApp {
}
What am I doing wrong?
Read application.properties in your application as follows:
ClassLoader.class.getResourceAsStream("/path/to/application.properties");
Alternatively,
ClassLoader.getResourceAsStream("/application.properties");
would you try this one
java -jar RunableApp4.jar --spring.config.location=<relative-path-to-configs>/InterfaceApp/src/main/resources/ --spring.profiles.active=dev
I get this except in AWS Elastic Beanstalk, but the app works locally.
01-Aug-2018 07:44:54.815 SEVERE [localhost-startStop-1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource
Caused by: org.flywaydb.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://d-use1-xx.xxxxxxxxxxx.us-east-1.rds.amazonaws.com:3306/xxxxxxxxxxx
The JDBC URL is specified correctly. I have this in file.war/WEB-INF/classes/application.properties
spring.datasource.url = jdbc:mysql://d-use1-xx.xxxxxxxxxx.us-east-1.rds.amazonaws.com:3306/xxxxxxxxx
spring.datasource.username = xxxx
spring.datasource.password = xxxxxxxxx
I do have \WEB-INF\lib\mysql-connector-java-5.1.46.jar in the WAR file.
I don't know if it matters but I recently added a JDBC TokenStore for Spring Security OAuth2 and added this in the main class.
#SpringBootApplication
#MapperScan("com.xxxxxx.xxxxxx.mapper")
public class XxxxxxxxxxxxApplication extends SpringBootServletInitializer {
#Bean(name = "OAuth")
#ConfigurationProperties(prefix="spring.datasource")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
I don't know if it matters (never did before) but there was an empty environment variable set
01-Aug-2018 07:44:38.290 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -DJDBC_CONNECTION_STRING=
You need to add the spring.datasource.driver-class-name configuration property:
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
JDBC automatic driver loading only works for drivers on the initial (system) class path of the application, but drivers located in WEB-INF/lib are added to a context class path at a later time and cannot be automatically loaded.
This means you need to explicitly load them, which is what Spring Boot does if you specify spring.datasource.driver-class-name (or the environment variable SPRING_DATASOURCE_DRIVER_CLASS_NAME as you found out).
I was able to fix it by setting environment variable
SPRING_DATASOURCE_DRIVER_CLASS_NAME=com.mysql.jdbc.Driver
Which AWS turns into
01-Aug-2018 17:01:03.505 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -DSPRING_DATASOURCE_DRIVER_CLASS_NAME=com.mysql.jdbc.Driver
I don't know why it suddenly required that property. I am using flyway-core-3.2.1.jar. I didn't specify a version in https://flywaydb.org/documentation/plugins/springboot so that's what it picked up.
I am using Spring Boot 1.4.3 and have a whole bunch of tests that are annotated with #DataJpaTest. By default, they run against an in-memory database. I would like to be able to run all of them against a local MySQL temporarily. How can I do this in an easy way?
I have found that I can make it work for one by adding #ActiveProfiles("local") where I have an application-local.properties that points to my local MySQL, but it is just too much work to add that everywhere, run the tests and then remove it again (since I only want to run this manually against MySQL, the CI environment will run against the in memory db).
I am using Maven if that would matter.
UPDATE:
So I have an application-local.properties which contains the db properties to connect to my local MySQL database (Which I use already to run my application against the local MySQL)
Then I right-click in IntelliJ on a package and select "Run all tests in package". In the settings of that run configuration, I add -Dspring.profiles.active=local to the "VM options" field.
I would have thought that this would activate the local profile during the tests, but it does not. If I stop the local MySQL, the tests still run fine.
In the docs it states that you are able to remove the autoconfigured h2 datasource with #AutoConfigureTestDatabase(replace= Replace.NONE) on the test class https://docs.spring.io/spring-boot/docs/1.4.2.RELEASE/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test.
Also you then need to provide your db setup in properties, so that it does not use your app properties e.g.:
# Database
spring.datasource.url=jdbc:mariadb://localhost:3303/test
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
i put this in application.properties in the test package
You can add the profile with the MySQL datasource properties in the same application.properties (or .yml) as:
application.yml
# Existing properties
---
spring:
profiles: h2
# More h2-related properties
---
spring:
profiles: postgres
database:
driverClassName: org.postgresql.Driver
datasource:
url: jdbc:postgresql://localhost:5432/db_dvdrental
username: user_dvdrental
password: changeit
jpa:
database: POSTGRESQL
generate-ddl: false
# More postgres-related properties
and either use #ActiveProfiles("postgres") in an integration test class or start teh container using VM argument as:
java -Dspring.profiles.active=h2 ...
Add application.yml(properties) with jdbc connection into src/test/resources
Run your JPA test with #AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE) - it disables using embedded database (h2), otherwise :
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'dataSource': Invocation of init method
failed; nested exception is java.lang.IllegalStateException: Failed to
replace DataSource with an embedded database for tests. If you want an
embedded database please put a supported one on the classpath or tune
the replace attribute of #AutoConfigureTestDatabase.