I am trying to create a spring boot app jar file which I need to use in another spring boot app. spring boot app1 is doing some DB operation. I want to add spring boot app1 to my spring boot app2 as a dependency and want to call service method which makes DB operations using MongoRepository.
I am attaching my code below. Can some help me with my problem?
Error:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.7)
2023-01-06 22:51:27.472 INFO 13892 --- [ main] com.example.apptwo.ApptwoApplication : Starting ApptwoApplication using Java 17.0.1 on HP with PID 13892 (C:\Users\saurabh\Desktop\apptwo\apptwo\target\classes started by saurabh in C:\Users\saurabh\Desktop\apptwo\apptwo)
2023-01-06 22:51:27.475 INFO 13892 --- [ main] com.example.apptwo.ApptwoApplication : No active profile set, falling back to 1 default profile: "default"
2023-01-06 22:51:27.926 INFO 13892 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data MongoDB repositories in DEFAULT mode.
2023-01-06 22:51:27.939 INFO 13892 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 8 ms. Found 0 MongoDB repository interfaces.
2023-01-06 22:51:28.178 WARN 13892 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'appTwoService': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'appone': Unsatisfied dependency expressed through field 'repo'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.appone.repo.PersonRepo' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2023-01-06 22:51:28.191 INFO 13892 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-01-06 22:51:28.217 ERROR 13892 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field repo in com.example.appone.service.PersonService required a bean of type 'com.example.appone.repo.PersonRepo' 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 'com.example.appone.repo.PersonRepo' in your configuration.
APP1
package com.example.appone.entity;
import lombok.ToString;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "test1")
@ToString
public class Person {
@Id
private String _id;
private String name;
private String email;
}
package com.example.appone.repo;
import com.example.appone.entity.Person;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PersonRepo extends MongoRepository<Person, String> {
}
package com.example.appone.service;
import com.example.appone.entity.Person;
import com.example.appone.repo.PersonRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class PersonService implements CommandLineRunner {
@Autowired
private PersonRepo repo;
public List<Person> getAllTest() {
return repo.findAll();
}
@Override
public void run(String... args) throws Exception {
System.out.println(getAllTest());
}
}
application.properties
spring.data.mongodb.uri=mongodb://localhost:27017/DB1
spring.data.mongodb.database=DB1
APP2
package com.example.apptwo.service;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.example.appone.service.PersonService;
@Configuration
public class AppOneConfig {
@Bean
public PersonService appone() {
return new PersonService();
}
}
package com.example.apptwo.service;
import com.example.appone.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class AppTwoService implements CommandLineRunner {
@Autowired
private PersonService service;
@Override
public void run(String... args) throws Exception {
System.out.println(service.getAllTest());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>apptwo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>apptwo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>appone</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
I tried @Autowired but it did not worked then I created a config file where I am creating bean of the service class of app1, This time bean found for app1 service class but bean not found for repository interface.
Please remove this class from APP2.
and add
scanBasePackagesattribute in@SpringBootApplicationannotation in APP2 main class like this and try again.