getting 'Context Initialization' error when adding spring-cloud-sleuth to classpath - spring-boot

Portion of the build file that references spring-cloud-sleuth is provided below.
dependencyManagement {
imports {
//where springCloudVersion = "Dalston.SR5" & springBootVersion = '1.5.9.RELEASE'
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
//in dependencies, added the following line
compile 'org.springframework.cloud:spring-cloud-starter-sleuth'
after adding this dependency, the application fails to start with the following exception (added based on comments)
support.AbstractApplicationContext (AbstractApplicationContext.java:551) -
Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'misds':
Could not bind properties to HikariDataSource (prefix=mis.datasource, ignoreInvalidFields=false, ignoreUnknownFields=true, ignoreNestedProperties=false); nested exception is org.springframework.validation.BindException: org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanPropertyBindingResult: 4 errors
Field error in object 'mis.datasource' on field 'driverClassName': rejected value [oracle.jdbc.OracleDriver];
codes [methodInvocation.mis.datasource.driverClassName,methodInvocation.driverClassName,methodInvocation.java.lang.String,methodInvocation];
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [mis.datasource.driverClassName,driverClassName];
arguments []; default message [driverClassName]]; default message [Property 'driverClassName' threw exception;
nested exception is java.lang.IllegalStateException: The configuration of the pool is sealed once started.
Use HikariConfigMXBean for runtime changes.]
I am using slf4j over log4j for logging framework and the application runs fine when I remove the sleuth dependency.
I noticed that the following 'additional' propertysources are present when spring-cloud dependency is included.
env.MutablePropertySources (MutablePropertySources.java:106) - Adding PropertySource 'springCloudClientHostInfo' with lowest search precedence
env.MutablePropertySources (MutablePropertySources.java:106) - Adding PropertySource 'defaultProperties' with lowest search precedence
env.MutablePropertySources (MutablePropertySources.java:106) - Adding PropertySource 'Management Server' with lowest search precedence

Always check if the latest release train doesn't solve your problems. Honestly, I don't really know which issue is related to this problem :/ Edgware release train is compatible with Boot < 2.0

Related

Spring Boot not able to pick value from yml file

I have a application-local.yml file inside resources folder. I have some properties like
data: https://xyzw/
I am using this property in a different class
#Service
public class Test {
#Value("${data}")
private String data;
}
When I run the Application.java I get the following error:
Application.main(Application.java:13), exception_class=org.springframework.beans.factory.BeanCreationException, exception_message=Error creating bean with name 'Test': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'data' in value "${data}"}","threadID":"main","sourceHost":"H18NPLFI13P0303","logVersion":"1.5","category":"org.springframework.boot.SpringApplication"}
Which is weird. I am not sure why it's not picking the values from the application-local.yml file.
If you start your application with IntelliJ IDEA and want the application-local.yml properties to be used, you need to edit your run configuration so that the local profile is used, else the application-local.yml properties won't be picked up by Spring.

redisson can't work in spring boot 3.0.0-M5 or RC1

my dependencies:
implementation 'org.springframework.boot:spring-boot-starter-web:3.0.0-RC1'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa:3.0.0-RC1'
implementation 'org.springframework.boot:spring-boot-starter-data-redis:3.0.0-RC1'
implementation 'org.redisson:redisson-spring-boot-starter:3.17.7'
my error:
Description:
Parameter 0 of constructor in jp.co.netstars.boss.core.api.aspect.RateLimitAspect required a bean of type 'org.redisson.api.RedissonClient' that could not be found.
Action:
Consider defining a bean of type 'org.redisson.api.RedissonClient' in your configuration.
Process finished with exit code 1
it works fine with version 3.0.0-M4.but not in RC1

org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.class not found on server start

Getting the below error when trying to start config server after upgrade to spring-boot version 2.6.6 and spring-cloud version 2021.0.2. These are defined in parent pom.
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.deloitte.insurcloud.microservices.config.ConfigServiceApplication]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.class] cannot be opened because it does not exist
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:610)
at org.springframework.context.annotation.ConfigurationClassParser.access$800(ConfigurationClassParser.java:111)
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGroupingHandler.lambda$processGroupImports$1(ConfigurationClassParser.java:812)
at java.util.ArrayList.forEach(ArrayList.java:1259)
Tried so many things like nearest version upgrade and downgrade. There is no explicit reference to ServerPropertiesAutoConfiguration in code checked after running dependency tree.

Subsequent calls to JPA failing in SpringBoot

I'm trying to execute tests but only the first call to findBy() is successful. Running with Spring Boot v2.6.7, Spring v5.3.19, hibernate jar = 5.6.8.
Application application = Application.builder()
.appName("APP1")
.productName("PRD1")
.build();
applicationDao.save(application);
Application app1 = applicationDao.findByAppNameIgnoreCaseAndProductNameIgnoreCaseContaining("APP1","PRD");
assertEquals("APP1", app1.getAppName());
The second calls fails in JUnit when called subsequently in the same JUnit test method
Application app2= applicationDao.findByAppNameIgnoreCaseAndProductNameIgnoreCaseContaining("APP1","PRD");
assertEquals("APP1", app2.getAppName());
Error:
org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [APP1] did not match expected type [java.lang.Character (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [APP1] did not match expected type [java.lang.Character (n/a)]
Looks like a bug in hibernate. Worked after downgrading hibernate version.
implementation ('org.springframework.boot:spring-boot-starter-data-jpa'){
exclude group: "org.hibernate", module: "hibernate-core"
}
implementation "org.hibernate:hibernate-core:5.6.5.Final"

Override spring-boot application.properties with external properties

I have a spring-boot application.
I have 3 properties file:
a property file inside the spring-boot jar - myjar.jar called application.properties (which is package inside the jar)
a property file outside the jar, at the jar location under configurations/global.properties
a property file outside the jar, at the jar location under configurations/java.properties
The problem is, I'm running the following command:
java -Dlogging.config=logback.xml -jar "myjar.jar" spring.config.location=classpath:/application.properties,file:./configurations/global.properties,file:./configurations/java.properties
And I get an exception:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myApplication': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'Data.StoresDb.LakeConnectionString' in value "${Data.StoresDb.LakeConnectionString}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:405)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.fa
in myApplication.java, I have:
#Value("${Data.StoresDb.LakeConnectionString}")
String dataLakeStoreDb;
and in my application.properties I do not have Data.StoresDb.LakeConnectionString, but I do have it in my global.properties, I would expect spring to resolve all files before trying to ser the value Data.StoresDb.LakeConnectionString
You pass the arg/value as a raw Java argument :
java -jar "myjar.jar" spring.config.location=...
The argument will be present in the String[] args of the main class but the Spring environment will not be aware about that.
You have to prefix it with -- to make the argument to be Spring environment aware :
java -jar "myjar.jar" --spring.config.location=...
From the official documentation :
By default SpringApplication will convert any command line option
arguments (starting with “--”, e.g. --server.port=9000) to a property
and add it to the Spring Environment
Or as alternative pass it as a system property :
java -Dspring.config.location=... -jar "myjar.jar"
As a side note : beware spring.config.location overrides the default locations while spring.config.additional-location introduced in Spring Boot 2 adds the specified locations to the default locations.

Resources