Spring Repository instanziiert/automatisiert nicht in der KonfigurationsklasseJava

Java-Forum
Guest
 Spring Repository instanziiert/automatisiert nicht in der Konfigurationsklasse

Post by Guest »

Ich habe eine Projekt-App erstellt und wollte JWT-Sicherheit einbeziehen. Ich habe ein Tutorial befolgt und habe ein Problem mit diesem Code-Snippet.

Code: Select all

@Configuration
@RequiredArgsConstructor
public class ApplicationConfig {

private final UserRepository repository;

@Bean
public UserDetailsService userDetailsService() {
return username -> repository.findByUsername(username)
.orElseThrow(() -> new UsernameNotFoundException("User not found"));
}

@Bean
public AuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
authProvider.setUserDetailsService(userDetailsService());
authProvider.setPasswordEncoder(passwordEncoder());
return authProvider;
}

@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
return config.getAuthenticationManager();
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
Ich erhalte diese Fehlermeldung:

Code: Select all

ApplicationConfig.java:21: error: variable userRepository not initialized in the default constructor
private final UserRepository userRepository;
^
Aufgrund umfangreicher Google-/Stackoverflow-Lesungen glaube ich, dass das Repository nicht von Spring instanziiert wird, bevor Spring diese Konfigurationsklasse lädt. Ich habe unter anderem versucht, das Repository automatisch zu verkabeln.
Ich habe noch niemanden gesehen, der dieses Problem auf der GitHub-Seite gepostet hat, der ich gefolgt bin, und ich bin mir nicht sicher, wie ich es beheben kann. Ratschläge willkommen!
Weitere relevante Codeausschnitte folgen:

Code: Select all

@Repository
public interface UserRepository extends JpaRepository {
Optional findByUsername(String username);
}

Code: Select all

@SpringBootApplication
@EnableConfigurationProperties(ConfigProperties.class)
@EnableJpaRepositories("com.browna.teller_back.repositories")
public class TellerBackApplication {

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

}
Benutzerentitätsklasse

Code: Select all

@Builder
@Entity
@Table(name = "users",
uniqueConstraints = {
@UniqueConstraint(columnNames = "username"),
@UniqueConstraint(columnNames = "email")
})
public class User implements UserDetails {

@Getter
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@NotBlank
@Size(max = 20)
private String username;

@NotBlank
@Size(max = 50)
@Email
private String email;

@NotBlank
@Size(max = 120)
private String password;

@Enumerated(EnumType.STRING)
private Role role;

public User() {
}

public User(String username, String email, String password) {
this.username = username;
this.email = email;
this.password = password;
}

public @NotBlank @Size(max = 20) String getUsername() {
return username;
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post