未找到類型為'com.jpa.test.UserRepository'的合格bean的異常在主線程中發生
P粉170438285
2023-08-29 14:35:44
<p>我是 Spring-boot 的初學者,當我嘗試執行 Spring-boot 應用程式時,當我嘗試執行 Spring-Boot 應用程式時,我遇到了這個問題。 </p>
<pre class="brush:php;toolbar:false;">Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type com. JPA. test. UserException: No qualifying bean of type com. JPA. test. UserExcept
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1148)
at com. JPA. test. Test Application. main(TestApplication.java:17)</pre>
<p>我希望資料能夠正確保存在資料庫中。</p>
<p>目錄資料夾排列:目錄資料夾排列</p>
<p>錯誤頁面:錯誤頁面1 錯誤頁面2</p>
<p>屬性介面:屬性申請頁</p>
<p>主類別代碼:</p>
<pre class="brush:php;toolbar:false;">package com.jpa.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ApplicationContext;
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class TestApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(TestApplication.class, args);
UserRepository ur = context.getBean(UserRepository.class);
User user = new User();
user.setName("XYZ");
user.setStatus("Active");
user.setCity("OOPS");
User save = ur. save(user);
System.out.println(save);
}
}</pre>
<p>實體類別(使用者):</p>
<pre class="brush:php;toolbar:false;">package com.jpa.test;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private String city;
private String status;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(int id, String name, String city, String status) {
super();
this.id = id;
this.name = name;
this.city = city;
this.status = status;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Override
public String toString() {
return "User [id=" id ", name=" name ", city=" city ", status=" status "]";
}
}</前>
<p>Dao(使用者儲存庫)package com.jpa.test;
導入 org.springframework.data.repository.CrudRepository;
導入 org.springframework.stereotype.Repository;
@儲存庫
公共介面 UserRepository 擴充 CrudRepository {
}</pre></p>
嘗試將這些註解新增到您的程式中: