Nicht @Autowire-repository-Schnittstelle Spring Boot
Das problem ich bin vor etwa @Autowire
repository Schnittstelle (UserRepository in meinem Fall), ich weiß nicht warum, aber die @Autowire
versagt.
UserController
- Klasse ruft eine @Service
Klasse und dies nennt man eine @Component
(DAO-Klasse), die DAO-Klasse @Autowiring
die @Repository
.
Spring boot wichtigsten
package com.leagueofsummoners;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.ErrorPage;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.http.HttpStatus;
import com.leagueofsummoners.persistence.interfaces.UserRepository;
@SpringBootApplication
public class LeagueofsummonersApplication {
public static void main(String[] args) {
SpringApplication.run(LeagueofsummonersApplication.class, args);
}
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return (container -> {
ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");
container.addErrorPages(error401Page, error404Page, error500Page);
});
}
}
DTO-KLASSE (Entity)
@Entity(name = "user")
@Table(name = "users")
public class UserDTO implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "id_user")
private Long idUser;
@Column(nullable = false, name = "summoner_name")
private String summonerName;
@Column(nullable = false)
private String username;
@Column(nullable = false)
private String password;
@Column(nullable = false)
private String email;
@Column(nullable = false)
private String avatar;
@Column(nullable = false)
private String firma;
@Column(nullable = false, name = "permission_level")
private PermissionLevels permissionLevel;
public UserDTO() {
}
Repository-Schnittstelle
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.Repository;
import com.leagueofsummoners.model.dto.UserDTO;
@org.springframework.stereotype.Repository
public interface UserRepository extends Repository<UserDTO, Long> {
Page<UserDTO> findAll(Pageable pageable);
UserDTO findByUsernameIgnoringCase(String username);
UserDTO findByIdUser(int idUser);
}
DAO-Klasse (diese Versagen, wenn autowiring repository-Klasse)
@Component
public class UserDAO{
@Autowired
private UserRepository userRepository;
public UserDTO findByUsernameIgnoringCase(String username) {
return this.userRepository.findByUsernameIgnoringCase(username);
}
}
Hier ein link mit der log von der Konsole
- Wer ist downvoting mir, ich würde gerne wissen, warum er/Sie es tun :·) (Nicht schlechten mod nur für lernen pruposes) x)
Du musst angemeldet sein, um einen Kommentar abzugeben.
Die Sie brauchen, um zu Scannen
JpaRepositories
fügen Sie diese Anmerkung auf Ihre Bewerbung-Klasse :EDIT:
Zur Konfiguration des
entityManager
müssen Sie die folgende Abhängigkeit :wenn Sie hinzufügen dieser Abhängigkeit wird die Konfiguration der repositories automatisch für Sie, so dass Sie nicht brauchen, um hinzuzufügen, die
@EnableJpaRepositories
.@EnableJparepositories
wenn mit Spring boot wird automatisch für Sie erledigt. Der Schlüssel ist, zu konfigurieren, JPA richtig und haben die richtige Abhängigkeitspring-boot-starter-data-jpa
. Wenn Sie die letztere Abhängigkeit, die Sie entfernen können die@EnableJpaRepositories
annotation.