.... Spring Boot 中构造函数的参数 0
Parameter 0 of constructor in .... Spring Boot
我是 Spring 引导和数据 Jpa 中的一条鱼,我试图创建一个基本的 Spring 引导应用程序,但每次我都遇到错误。你能帮帮我吗?
这是我的代码:
Spring 引导应用程序 class:
@SpringBootApplication
@ComponentScan(basePackages = "com.project.*")
@EnableJpaRepositories(basePackages = "com.project.repository.*")
@EntityScan(basePackages = "com.project.entities.*")
@EnableAutoConfiguration
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
控制器Class:
@RestController
@RequestMapping(value = "/api")
public class controller {
private IUserServices userServices;
@Autowired
public controller(IUserServices userServices) {
this.userServices = userServices;
}
@GetMapping(value = "/merhaba")
public String sayHello(){
return "Hello World";
}
@GetMapping(value = "/getall")
public List<User> getAll(){
return this.userServices.getAllUsers();
}
}
存储库Class:
@Repository
public interface UserRepository extends JpaRepository<User,Long> {
}
我服务Class:
@Service
public interface IUserServices {
void saveUser(User user);
List<User> getAllUsers();
}
ServicesImpl Class:
@Service
public class UserServicesImpl implements IUserServices{
private UserRepository userRepository;
@Autowired
public UserServicesImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public void saveUser(User user) {
this.userRepository.save(user);
}
@Override
public List<User> getAllUsers() {
return this.userRepository.findAll();
}
}
实体Class:
@Entity
@Table(catalog = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
public User() {
}
public User(int id, String name) {
this.id = id;
this.name = name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
这是我的错误消息:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.project.services.UserServicesImpl required a bean of
type 'com.project.repository.UserRepository' that could not be found.
Action:
Consider defining a bean of type 'com.project.repository.UserRepository' in your
configuration.
Process finished with exit code 0
SO 这是应用程序属性文件:
spring.jpa.properties.hibernate.dialect =
org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true
spring.datasource.url=jdbc:postgresql://localhost:5432/u
spring.datasource.username=postgres
spring.datasource.password=1234
spring.jpa.properties.javax.persistence.validation.mode = none
有些问题您应该解决。
第一
当你有 spring boot application with @SpringBootApplication 你不需要其他东西比如 @EnableAutoConfiguration
等等,所以把它们全部删除.
您可以阅读更多相关信息 here。
第二
你不需要用@Service注解你的服务接口,因为你在UserServicesImpl
class.
第三
您在用户实体中将 id
定义为整数,但在存储库中,您将 ID 写为 Long
。这是错的。应该是这样的。
@Repository
public interface UserRepository extends JpaRepository<User,Integer> {
}
试试上面的解决方案,让我知道结果。
我是 Spring 引导和数据 Jpa 中的一条鱼,我试图创建一个基本的 Spring 引导应用程序,但每次我都遇到错误。你能帮帮我吗?
这是我的代码: Spring 引导应用程序 class:
@SpringBootApplication
@ComponentScan(basePackages = "com.project.*")
@EnableJpaRepositories(basePackages = "com.project.repository.*")
@EntityScan(basePackages = "com.project.entities.*")
@EnableAutoConfiguration
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
控制器Class:
@RestController
@RequestMapping(value = "/api")
public class controller {
private IUserServices userServices;
@Autowired
public controller(IUserServices userServices) {
this.userServices = userServices;
}
@GetMapping(value = "/merhaba")
public String sayHello(){
return "Hello World";
}
@GetMapping(value = "/getall")
public List<User> getAll(){
return this.userServices.getAllUsers();
}
}
存储库Class:
@Repository
public interface UserRepository extends JpaRepository<User,Long> {
}
我服务Class:
@Service
public interface IUserServices {
void saveUser(User user);
List<User> getAllUsers();
}
ServicesImpl Class:
@Service
public class UserServicesImpl implements IUserServices{
private UserRepository userRepository;
@Autowired
public UserServicesImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public void saveUser(User user) {
this.userRepository.save(user);
}
@Override
public List<User> getAllUsers() {
return this.userRepository.findAll();
}
}
实体Class:
@Entity
@Table(catalog = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
public User() {
}
public User(int id, String name) {
this.id = id;
this.name = name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
这是我的错误消息:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.project.services.UserServicesImpl required a bean of
type 'com.project.repository.UserRepository' that could not be found.
Action:
Consider defining a bean of type 'com.project.repository.UserRepository' in your
configuration.
Process finished with exit code 0
SO 这是应用程序属性文件:
spring.jpa.properties.hibernate.dialect =
org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true
spring.datasource.url=jdbc:postgresql://localhost:5432/u
spring.datasource.username=postgres
spring.datasource.password=1234
spring.jpa.properties.javax.persistence.validation.mode = none
有些问题您应该解决。
第一
当你有 spring boot application with @SpringBootApplication 你不需要其他东西比如 @EnableAutoConfiguration
等等,所以把它们全部删除.
您可以阅读更多相关信息 here。
第二
你不需要用@Service注解你的服务接口,因为你在UserServicesImpl
class.
第三
您在用户实体中将 id
定义为整数,但在存储库中,您将 ID 写为 Long
。这是错的。应该是这样的。
@Repository
public interface UserRepository extends JpaRepository<User,Integer> {
}
试试上面的解决方案,让我知道结果。