gpt4 book ai didi

java - 从依赖 jar 中找不到类

转载 作者:行者123 更新时间:2023-12-02 05:45:14 25 4
gpt4 key购买 nike

我有一些项目试图构建微服务架构。有些项目用于为可调用微服务生成 jar 文件(我将这些项目称为“库项目”。)。这些微服务之一负责用户/客户注册和查找。所有数据都保存在 Cassandra 集群中。其中一个库项目负责提供各种基于 Cassandra 的服务/值对象等...

在微服务pom文件中,我包含了库生成的jar项目。它可以编译,但是当我启动客户服务时,找不到该 jar 中的类。

***************************
APPLICATION FAILED TO START
***************************

Description:

Field repository in com.besztercekk.tao.customer.handler.PostUserHandler required a bean of type 'com.besztercekk.tao.cassandra.repository.ReactiveUserRepository' 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.besztercekk.tao.cassandra.repository.ReactiveUserRepository' in your configuration.

这里有一些代码片段,但我可以附加您需要的其他任何内容。

这是微服务的 pom 文件。未找到的类在此依赖项中:tao-elszamolas-cassandra。

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>tao-elszamolas-customer-service</artifactId>
<packaging>jar</packaging>

<name>tao-elszamolas-customer-service</name>
<description>TAO elszamolas customer backend service</description>

<parent>
<groupId>com.besztercekk.tao</groupId>
<artifactId>tao-elszamolas-spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<dependencies>
<!-- Spring Cloud Eureka client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<!-- Spring Cloud config client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>

<!-- Webflux -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- Reactive cassandra -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra-reactive</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.1-jre</version>
</dependency>

<!-- TAO elszamolas dependencies -->
<dependency>
<groupId>com.besztercekk.tao</groupId>
<artifactId>tao-elszamolas-cassandra</artifactId>
</dependency>

<dependency>
<groupId>com.besztercekk.tao</groupId>
<artifactId>tao-elszamolas-jwt</artifactId>
</dependency>
</dependencies>


<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>11</release>
</configuration>
</plugin>

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

这是包含缺少的类的项目的 pom 文件:

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<artifactId>tao-elszamolas-cassandra</artifactId>
<packaging>jar</packaging>

<name>tao-elszamolas-cassandra</name>
<description>TAO elszámolás Spring alapú több projektben használt komponensek gyűjtő projektje</description>

<parent>
<groupId>com.besztercekk.tao</groupId>
<artifactId>tao-elszamolas-spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<dependencies>
<!-- Reactive cassandra -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra-reactive</artifactId>
</dependency>
</dependencies>

</project>

这是错误来源的类:

@Component
@Validator(UserDataValidator.class)
public class PostUserHandler extends AbstractValidationHandler<User, ChainableValidator> {

@Autowired
private ReactiveUserRepository repository;

@Autowired
private PasswordEncoder passwordEncoder;

public PostUserHandler() {
super(User.class);
}

@Override
protected Mono<ServerResponse> processRequest(User body, ServerRequest request) {
return repository.save(enrichUser(body, UUIDs.random().toString()))
.flatMap(updatedUser -> {
sendRegistrationCode();
return ok(updatedUser);
})
.onErrorResume(IllegalArgumentException.class, e -> ServerResponse.badRequest().build())
.switchIfEmpty(ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).build());
}

@Override
protected Mono<User> extractContent(ServerRequest request) {
return request.bodyToMono(User.class);
}

private void sendRegistrationCode() {
//TODO: Send Kafka message to email component
}

private Mono<ServerResponse> ok(User c) {
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromPublisher(Mono.just(c), User.class));
}

private User enrichUser(User user, String activationCode) {
return User.builder()
.authorities(user.getAuthorities())
.email(user.getEmail())
.enabled(user.isEnabled())
.firstName(user.getFirstName())
.id(UUIDs.timeBased())
.lastName(user.getLastName())
.middleName(user.getMiddleName())
.password(passwordEncoder.encode(user.getPassword()))
.registrationTime(LocalDateTime.now())
.passwordExpiryTime(LocalDateTime.now())
.roles(user.getRoles())
.username(user.getUsername())
.accountExpired(false)
.accountLocked(false)
.activationCode(activationCode)
.build();
}

}

如有任何帮助,我们将不胜感激!

更新(添加了一些额外信息):

这是包含组件扫描的主类。

@SpringBootApplication
@EnableEurekaClient
@ComponentScan(basePackages = {"com.besztercekk.tao.customer", "com.besztercekk.tao.cassandra"})
public class CustomerBackendApplication {

public static void main(String[] args) {
SpringApplication.run(CustomerBackendApplication.class, args);
}
}

这是存储库类:

package com.besztercekk.tao.cassandra.repository;

import java.util.UUID;

import org.springframework.data.cassandra.repository.ReactiveCassandraRepository;
import org.springframework.stereotype.Component;

import com.besztercekk.tao.cassandra.model.User;

import reactor.core.publisher.Mono;

/**
* This interface provides reactive {@link User} services.
*/
public interface ReactiveUserRepository extends ReactiveCassandraRepository<User, UUID> {

Mono<User> findByUsername(String username);

Mono<User> findByEmail(String email);
}

也尝试给这个接口(interface)添加@Component和@Repository注解,只是为了看看它的效果。 :-)

我的另一个想法是在重新启动后以编程方式将 bean 添加到上下文中。但这只是一个界面,所以不确定它是否有效。

第三个想法是将这个接口(interface)包装到一个服务类中,并且很容易以编程方式添加。

但我真的希望它能够自动添加,而不需要编写代码。

This is the package hierarchy in the library project

最佳答案

尝试将以下注释添加到任何 @Configuration 类:

@EnableReactiveCassandraRepositories
@Configuration
class MyConfiguration() {
//code
}

上面的代码使 spring 能够自动扫描并为您的接口(interface)创建默认的实现 bean ReactiveUserRepository

还可以对界面进行注释,例如:

@Repository
public interface ReactiveUserRepository extends ReactiveCassandraRepository<User, UUID> {

Mono<User> findByUsername(String username);

Mono<User> findByEmail(String email);
}

欲了解更多详情,请阅读: https://dzone.com/articles/reactive-streams-with-spring-data-cassandra

此外,如果您想了解有关注释的更多详细信息,请阅读类似的文章:

how annotation @Repository in java spring work?

关于java - 从依赖 jar 中找不到类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56101401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com