如何使用spring引导app1进入spring引导app2作为依赖关系,其中spring引导app1正在做一些DB操作



我正在尝试创建一个spring启动应用程序jar文件,我需要在另一个spring启动应用程序中使用。spring boot app1正在做一些DB操作。我想将spring boot app1添加到我的spring boot app2中作为依赖项,并希望调用service方法,该方法使用MongoRepository进行DB操作。

我附上我的代码下面。有人能帮我解决这个问题吗?

错误:


.   ____          _            __ _ _
/\ / ___'_ __ _ _(_)_ __  __ _    
( ( )___ | '_ | '_| | '_ / _` |    
\/  ___)| |_)| | | | | || (_| |  ) ) ) )
'  |____| .__|_| |_|_| |___, | / / / /
=========|_|==============|___/=/_/_/_/
:: 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:UserssaurabhDesktopapptwoapptwotargetclasses started by saurabh in C:UserssaurabhDesktopapptwoapptwo)
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>

我尝试了@Autowired,但它没有工作,然后我创建了一个配置文件,我正在创建app1服务类的bean,这次bean为app1服务类找到,但没有为存储库接口找到。

请从APP2中删除此类。

@Configuration
public class AppOneConfig {
@Bean
public PersonService appone() {
return new PersonService();
}
}

并在APP2主类的@SpringBootApplication注释中添加scanBasePackages属性,然后再试一次

@SpringBootApplication(scanBasePackages = {"com.example.appone","com.example.apptwo"})
public class SpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}

最新更新