[SpringBoot] The SpringBoot @Value annotation cannot introduce the correct solution to the value in the configuration file

Uncle Cthulhu 2022-11-24 21:24:09 阅读数:988


1. The following code is a tool class of Qiniu Cloud, and some keys of Qiniu Cloud need to be introduced

@Componentpublic class QiNiuFileUp {@Value("${qiniu. accessKey}")private String accessKey;@Value("${qiniu. secretKey}")private String secretKey;@Value("${qiniu. domainOfBucket}")private String domainOfBucket;@Value("${qiniu. basename}")private String basename;...........

2. If you want to use the main methods in this class, such as uploading, parsing Url, etc., you must use

in other classes
@Servicepublic class UserServiceImpl implements UserService {private final UserDao userDaoImpl;private final SessionDao sessionDaoImpl;private final QiNiuFileUp qiNiuFileUp;@Autowiredpublic UserServiceImpl(UserDao userDao, SessionDao sessionDaoImpl, QiNiuFileUp qiNiuFileUp) {this. userDaoImpl = userDao;this.sessionDaoImpl = sessionDaoImpl;this.qiNiuFileUp = qiNiuFileUp;}........

This form, the @Autowired annotation form, can be read correctly

The Springboot project, the class assigned with @Value, and other classes using such methods must be handed over to spring management in the form of @Autowired annotations before they can use the values ​​in such methods;

copyright:author[Uncle Cthulhu],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/328/202211242120378253.html