Not able to start spring boot project - spring

I've tried to start a spring boot project. It doesn't start. Here are my build.gradle config changes and application local yml file.
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Greenwich.SR3"
mavenBom "io.pivotal.spring.cloud:spring-cloud-services-dependencies:2.1.2.RELEASE"
mavenBom "org.springframework.cloud:spring-cloud-contract-dependencies:2.1.1.RELEASE"
}
I made a bean definition overriding property as well
spring:
main:
allow-bean-definition-overriding: true
However, it is not starting. Here is the main exception
{"#timestamp":"2020-09-09T14:58:53.489-04:00","#version":"1","message":"\r\n\r\n***************************\r\nAPPLICATION FAILED TO START\r\n***************************\r\n\r\nDescription:\r\n\r\n
The bean 'dataSource', defined in BeanDefinition defined in class path resource [org/springframework/boot/autoconfig
ure/jdbc/DataSourceConfiguration$Hikari.class], could not be registered. A bean with that name has already been defi
ned in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfig
uration$Hikari.class]
and overriding is disabled.\r\n\r\nAction:\r\n\r\nConsider renaming one of the beans
or enabling overriding
by setting spring.main.allow-bean-definition-overriding=true\r\n","logger_name":"org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter","thread_name":"main","level":"ERROR","level_value":40000}
What else am I missing?

Related

javax.enterprise.inject.UnsatisfiedResolutionException:Unsatisfied dependency for type com.test.model.TestService and qualifiers [#Default] in Quarkus

I have created Quarkus project and added a SpringBoot project dependency in build.gradle file, dependency downloaded successfully.
Now when I am trying to inject a class which is available in dependancy, the build is getting failed.
Note: The Class which I am trying to inject does not have #ApplicationScope annotation.
getting below error:
Caused by: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type com.test.model.TestService and qualifiers [#Default]
Look here:
https://quarkus.io/guides/cdi-reference
See the section 1 in the document above about creating the bean archive.
What worked for me is adding references to the dependencies in my application.properties.
quarkus.index-dependency.[name].group-id=[group]
quarkus.index-dependency.[name].artifact-id=[artifact]
For instance, in my project I injected some OpenTracing beans, ended up with:
quarkus.index-dependency.opentracing.group-id=io.opentracing
quarkus.index-dependency.opentracing.artifact-id=opentracing-api

Mapstruct: Consider defining a bean of type ''FooMapper" in your configuration

I'm trying to get Mapstruct working with gradle. The implementations generate and work great when I run the service from the boot dashboard in eclipse, but they don't generate when run with gradlew bootrun, and the bean can't be found. Here is the error:
*************************** APPLICATION FAILED TO START
Description:
Field fooMapper in ServiceImpl required a bean of type 'FooMapper'
that could not be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'FooMapper' in your configuration.
And here is my build.gradle file:
plugins {
id "org.springframework.boot"
id "groovy"
id "com.diffplug.eclipse.apt" version "3.31.0"
}
configurations {
// configuration to enable running bootRun locally
// with embedded h2 database
runlocal
}
ext {
mapstructVersion = "1.5.0.Beta1"
}
dependencies {
implementation "org.mapstruct:mapstruct:${mapstructVersion}"
annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"
testImplementation "org.testng:testng:6.10", "org.easytesting:fest-assert:1.4"
implementation project(":common")
implementation "org.springframework.boot:spring-boot-starter-web:$springBootVersion"
implementation "org.springframework.boot:spring-boot-starter-jdbc:$springBootVersion"
implementation "org.springframework.boot:spring-boot-starter-data-jpa:$springBootVersion"
implementation "org.springframework.boot:spring-boot-starter-actuator:$springBootVersion"
implementation "org.springframework.boot:spring-boot-starter-validation:$springBootVersion"
implementation "org.codehaus.gpars:gpars:1.2.1"
developmentOnly "org.springframework.boot:spring-boot-devtools:$springBootVersion"
runlocal project(":test-db")
testImplementation project(":test-db")
}
sourceSets.main.groovy.srcDirs += ["build/generated/sources"]
bootRun {
classpath = sourceSets.main.runtimeClasspath + configurations.runlocal
}
tasks.withType(JavaCompile) {
options.compilerArgs = [
"-Amapstruct.suppressGeneratorTimestamp=true",
"-Amapstruct.defaultComponentModel=spring",
"-Amapstruct.verbose=true"
]
}
Is it because my code is written in groovy? But then why would it work with the boot dashboard in eclipse? I am using #Mapper(componentModel = 'spring'), but that hasn't fixed the issue. Using version 1.4.2.FINAL also doesn't work. Any help would be really appreciated!
had the same problem in eclipse, solved configuring my project properties:
Go to:
Java Compiler > Annotation Processing
Set Enable Project specific settings to true
Go to:
Java Compiler > Annotation Processing > Factory Path
Add external jar:
Here find the location of your mapstruct-processor-1.4.2-Final.jar on the gradle cache directory, i am using debian 11, my path is on:
/.gradle/caches/modules-2/files-2.1/org.mapstruct/mapstruct-processor/1.4.2.Final/e55bd90d51cddd638c07d5bd89fc7535d4e3d069/mapstruct-processor-1.4.2.Final.jar
Apply and close, and gradle should rebuild the project, generating two new folders .apt_generated and .apt_generated_tests on the package explorer with the mapper code

Integrate Liquibase Groovy DSL in Spring Boot

Trying to integrate like this:
build.gradle
dependencies {
implementation 'org.liquibase:liquibase-core'
implementation 'org.liquibase:liquibase-groovy-dsl:2.1.1'
runtimeOnly 'com.h2database:h2'
runtime 'org.liquibase:liquibase-core'
runtime 'org.liquibase:liquibase-groovy-dsl:2.1.1'
}
In Spring Boot application.properties
spring.liquibase.changeLog=classpath:db/changelog/changeset0002.groovy
Have a corresponding changeset0002.groovy file under src/main/resources/db/changelog.
While doing gradle bootRun, getting the following exception:
Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
It works. In my application.properties file, with the following setting, I got it working:
spring.liquibase.change-log=classpath:META-INF/scripts/changeset0001.groovy
where the directories META-INF/scripts exist under src/main/resources of my Gradle project structure and changeset0001.groovy is a usual Groovy DSL file.

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'batchConfiguration'

I have created a maven module for implementing spring batch in our project. My aim is to export data from database table to CSV file (basing on a repository implemented in another maven module).
In my Spring boot main class I have the following:
#SpringBootApplication(scanBasePackages = {"com.test"})
#ComponentScan({"com.test"})
public class MainBatchApplication {
public static void main(String args []){
SpringApplication.run(MainBatchApplication.class,args);
}
}
The package com.test is the path of our implemented UserRepository I want to use to get all users from database.
Once I inject the UserRepository in my Spring Batch created maven module and once I run the spring boot app. I got the following error:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'batchConfiguration': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.test.repositories.UserRepository.getFilteredByMonth(java.lang.String,java.util.Date,java.util.Date)!
The module where com.test if included in my batch maven module as dependency (pom.xml)
You need to define configuration somewhere in your project with:
#Configuration
#EnableBatchProcessing
public class BatchConfiguration {
....
}
Then you would be able to inject your Repository here for writers.
It would need some more bean definition. See: https://spring.io/guides/gs/batch-processing/ for examples

NoSuchBeanDefinitionException for autowired bean when java bean configuration location is changed from src/main/java to src/test/java

I have an existing spring boot application and I am adding junit tests to it. The project has Configuration.java in src/main/java folder containing the bean configuration.
In my junit class when I refer to configuration file present in location src/main using
#SpringApplicationConfiguration(classes = Configuration.class)
then the Autowired bean intializes properly. However when I copy the same configuration file in src/test/java and refer it using Configuration
#SpringApplicationConfiguration(classes = TestConfig.class)
then the autowire bean fails to initilize giving exception NoSuchBeanDefinitionException.
Its the same configuration java file. The bean I am autowiring is present in src/main/java. Does the location of bean configuration java files affects the bean initialization ? If Yes, how to resolve it ?
Note: I am copying the same config java file to src/test/java as I need some different property while testing.
I was able to solve it be using basepackage attribute of #ComponentScan as shown below(in the config file of src/test/java have put the package name belonging to package in src/main/java).
#Configuration
#ComponentScan(excludeFilters = #ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = CommandLineRunner.class),basePackages = "com.mypackage")
#EnableAutoConfiguration(exclude={MetricRepositoryAutoConfiguration.class})
public class TestConfiguration {

Resources