[英]java - Autowiring Repository in class that is instantiated in Spring Boot
因此,我現在有一個API客戶端類型類,我正嘗試連接到我的存儲庫,以便可以將數據存儲在MySQL數據庫中。
我遇到的問題是API客戶端類實例化了自身的新對象,因此自動裝配無法正常工作。 我一直在尋找解決此問題的方法,並且看到了幾種選擇,但是對於如何將它們應用於我的問題我感到困惑。
作為參考,以下是一些相關文件的部分:
GeniusApiClient.java:
@Component
public final class GeniusApiClient {
private final OkHttpClient client = new OkHttpClient();
@Autowired
private ArtistDao artistDao;
public static void main(String[] args) throws Exception {
GeniusApiClient geniusApiClient = new GeniusApiClient();
String artistId = (geniusApiClient.getArtistId("Ugly Duckling"));
ArrayList<String> artistSongIds = geniusApiClient.getArtistSongIds(artistId);
System.out.println(geniusApiClient.getAllSongAnnotations(artistSongIds, artistId));
}
public String getAllSongAnnotations(ArrayList<String> songIds, String artistId) {
Artist artist = new Artist("test name for now", "string123", "223");
artistDao.save(artist);
return "finished";
}
}
ArtistDao.java:
@Transactional
public interface ArtistDao extends CrudRepository<Artist, Long> {
public Artist findByGeniusId(String geniusId);
}
ArtistController.java:
@Controller
public class ArtistController {
@Autowired
private ArtistDao artistDao;
/**
* GET /create --> Create a new artist and save it in the database.
*/
@RequestMapping("/create")
@ResponseBody
public String create(String name, String annotations, String genius_id) {
String userId = "";
try {
genius_id = genius_id.replaceAll("/$", "");
Artist artist = new Artist(name, annotations, genius_id);
artistDao.save(artist);
userId = String.valueOf(artist.getId());
}
catch (Exception ex) {
return "Error creating the artist: " + ex.toString();
}
return "User succesfully created with id = " + userId;
}
/**
* GET /get-by-email --> Return the id for the user having the passed
* email.
*/
@RequestMapping("/get")
@ResponseBody
public String getByEmail(String genius_id) {
String artistId = "";
try {
Artist artist = artistDao.findByGeniusId(genius_id);
artistId = String.valueOf(artist.getId());
}
catch (Exception ex) {
return "User not found";
}
return "The user id is: " + artistId;
}
}
問題是在GeniusApiClient.java中的getAllSongAnnotations方法中,嘗試訪問artistDao時出現空指針異常。 我知道我的此類實例化是在弄亂自動裝配,但我對解決此問題的最佳方法感到好奇。
我考慮過將類中的所有方法都設為靜態,這樣就不必實例化新方法,但是我認為這樣做不會很好。 有什么建議么?
謝謝
編輯:
為了清楚起見,刪除了一些不相關的代碼。
編輯2:
添加了ArtistController.java
為了能夠自動連線/插入對象,該對象必須是Spring bean。
在這里,您不能自動連接ArtistDao
因為它不是bean。 有幾種使它成為bean的注釋選項,但在這種情況下最適合的是@Repository
注釋。 它只是您在GeniusApiClient
類中使用的@Component
的專用版本。
所以,
@Repository
@Transactional
public interface ArtistDao extends CrudRepository<Artist, Long> {
public Artist findByGeniusId(String geniusId);
}
應該管用。
我建議您閱讀: http : //docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html
如果閱讀參考文檔聽起來很嚇人,您還可以查看Spring in Action中的 Core Spring部分。
不要使GeniusApiClient.class最終。 Spring將使用CGLIB動態擴展您的類以創建代理。 CGLIB正常工作的要求是讓您的班級最終定下來。
在此獲得更多信息: 最終確定Spring Service Classes?
您在catch塊中嘗試執行的操作對我來說還不清楚,您必須更正該錯誤,並用所需的操作替換該錯誤,以便在發生任何異常情況時采取措施。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.