我正在使用 Spring Boot,但在理解 Beans 時遇到了一些麻煩。我相信 Beans 取代了new關(guān)鍵字。我發(fā)現(xiàn)僅使用 Autowire 時,我的 Beans 不會在對象上創(chuàng)建新實(shí)例,并且我的 REST 應(yīng)用程序?qū)⒎祷赜脩羰紫纫蟮南嗤憫?yīng)(即,如果我最初訪問了 url/id/1,然后訪問了 url /id/2 REST 響應(yīng)將與 url/id/1 相同)。我試圖通過創(chuàng)建一個 @Configuration 文件來定義一個 bean 來解決這個問題。@Configurationpublic class UserConfig { @Autowired UserDAO DAO; @Bean public User getUser(int uid) { try { return DAO.getUser(uid); } catch (SIDException e) { return null; } }}但我在運(yùn)行時不斷收到此錯誤: Parameter 0 of method getUser in com.application.Config.UserConfig required a bean of type 'int' that could not be found.我不明白這一點(diǎn),因為我試圖在配置文件中定義 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
提交
取消