@AllArgsConstructor與@Value共同使用引發的報錯問題
平時我們用lombok的方式來實現Bean的建構函式方式的注入,但是當遇到@Value註解的時候,就會出現問題,看下面這段程式碼:
@RestController
@AllArgsConstructor
@RequestMapping("test")
public class TestController {
@Value("${test}")
private String test;
private TestService testService;
@GetMapping()
public String get() {
System.out.println(test);
return testService.get();
}
}
啟動專案的時候,會丟擲異常
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.good.base.controller.TestController required a bean of type 'java.lang.String' that could not be found.
Action:
Consider defining a bean of type 'java.lang.String' in your configuration.
如何解決這個問題呢?
其實很簡單,將AllArgsConstructor改成RequiredArgsConstructor,然後把需要注入的bean改成final型別的,就可以了
@RestController
@RequiredArgsConstructor
@RequestMapping("test")
public class TestController {
@Value("${test}")
private String test;
private final TestService testService;
@GetMapping()
public String get() {
System.out.println(test);
return testService.get();
}
}
相關文章
- @AllArgsConstructor與@Value共用的問題解決Struct
- 記錄解決HttpServletResponse在引數報錯的問題HTTPServlet
- Kotlin 與 Spring boot整合,@Value的問題KotlinSpring Boot
- 使用DBMS_RANDOM過程引發的問題random
- 【Mysql】into outfile報錯的問題MySql
- 解決vue使用Sass時候的報錯問題Vue
- Druid連線池引數maxWait配置錯誤引發的問題UIAI
- sqlldr經常報錯的問題SQL
- RHCE常見的報錯問題
- mysql checksum 報錯問題。MySql
- 關於使用 Laravel new 新專案 報錯的問題Laravel
- iOS開發執行報錯、常見問題iOS
- Laravel 中 sql 查詢 使用 group by 報錯問題。LaravelSQL
- 使用API28報錯問題及解決API
- 堆疊溢位報錯引發的思考
- Android自帶Json庫使用引發的問題AndroidJSON
- php使用mysqlnd引發的一些問題處理PHPMySql
- httprunner 的安裝報錯問題,rtHTTP
- 關於@override報錯的問題IDE
- vue3.0使用vue-seamless-scroll報錯問題Vue
- go mod init報錯問題Go
- RedisTemplate呼叫increment報錯問題RedisREM
- composer require 報錯問題UI
- mybatis plus saveBatch報錯問題MyBatis
- 【Mysql】字符集引發的主從報錯MySql
- 使用GDB與QEMU除錯核心時的問題分析(轉)除錯
- Can't use function return value in write context 使用empty遇到報錯FunctionContext
- overflow與flex同時使用發現的問題Flex
- reflow和repaint引發的效能問題AI
- 專案叢集引發的問題
- 新建maven專案pom報錯的問題Maven
- 設定autotrace的報錯問題解決
- 解決 Inkscape 報錯 Duplicate 問題
- 聊聊nginx報錯499問題Nginx
- navicate oracle 連線報錯問題Oracle
- JS語法: 由++[[]][+[]]+[+[]] = 10 ?引發的問題JS
- 奇怪的問題: 資料庫使用者登入的時候報錯資料庫
- MYSQL timestamp NOT NULL插入NULL的報錯問題MySqlNull