我正在使用 Spring Boot,但在理解 Beans 時遇到了一些麻煩。我相信 Beans 取代了new關鍵字。我發現僅使用 Autowire 時,我的 Beans 不會在對象上創建新實例,并且我的 REST 應用程序將返回用戶首先要求的相同響應(即,如果我最初訪問了 url/id/1,然后訪問了 url /id/2 REST 響應將與 url/id/1 相同)。我試圖通過創建一個 @Configuration 文件來定義一個 bean 來解決這個問題。@Configurationpublic class UserConfig { @Autowired UserDAO DAO; @Bean public User getUser(int uid) { try { return DAO.getUser(uid); } catch (SIDException e) { return null; } }}但我在運行時不斷收到此錯誤: Parameter 0 of method getUser in com.application.Config.UserConfig required a bean of type 'int' that could not be found.我不明白這一點,因為我試圖在配置文件中定義 Bean。在我的主文件中,我有這些注釋:@SpringBootApplication(scanBasePackages = {"com.application.Config","com.application"})@ComponentScan({"com.application.Config","com.application"})如果有幫助,我將在這種情況下使用我的 bean:@Servicepublic class UserService { @Autowired private UserDAO DAO; public User getUser(int uid) { try { return DAO.getUser(uid); } catch (SIDException e) { return null; } }}
添加回答
舉報
0/150
提交
取消
