[英]How to use spring boot app1 into spring boot app2 as dependency where spring boot app1 is doing some DB operation?
I am trying to create a spring boot app jar file which I need to use in another spring boot app.我正在尝试创建一个 spring 启动应用程序 jar 文件,我需要在另一个 spring 启动应用程序中使用它。 spring boot app1 is doing some DB operation.
spring boot app1 正在做一些数据库操作。 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.
我想将 spring boot app1 添加到我的 spring boot app2 作为依赖项,并想调用使用 MongoRepository 进行 DB 操作的服务方法。
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 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应用程序.properties
spring.data.mongodb.uri=mongodb://localhost:27017/DB1
spring.data.mongodb.database=DB1
APP2 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.我尝试了@Autowired,但它没有用,然后我创建了一个配置文件,我在其中创建了 app1 的服务 class 的 bean,这次找到了 app1 服务 class 的 bean,但是没有找到存储库接口的 bean。
Please remove this class from APP2.请从APP2中删除这个class。
@Configuration
public class AppOneConfig {
@Bean
public PersonService appone() {
return new PersonService();
}
}
and add scanBasePackages
attribute in @SpringBootApplication
annotation in APP2 main class like this and try again.并像这样在 APP2 main class 中的
@SpringBootApplication
注解中添加scanBasePackages
属性,然后重试。
@SpringBootApplication(scanBasePackages = {"com.example.appone","com.example.apptwo"})
public class SpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.