解决‘java.lang.String’报错:Consider defining a bean of type ‘java.lang.String‘ in your configuration

 报错信息:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-06-20 17:41:32.653 ERROR 9192 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : ***************************
APPLICATION FAILED TO START
***************************Description:Parameter 1 of constructor in com.linksoul.zfau.aiops.service.MsgPushServiceImpl 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.
@Service
@AllArgsConstructor
public class MsgPushServiceImpl {@Value("${msg.appId}")private String APPID;
...
}

原因:

经过排查后,因为springioc容器加载bean默认使用无参构造进行初始化,该实现层使用了@AllArgsConstructor注解,而@Value注入一个属性值,其并不会存在于Spring容器,所以报错。

解决方案:

     将@RequiredArgsConstructor注解替换@AllArgsConstructor注解即可。

@Service
@RequiredArgsConstructor
public class MsgPushServiceImpl {@Value("${msg.appId}")private String APPID;
...
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部