We know that under normal circumstances, a bean of spring depends on other resources, such as properties or other beans. You can use @ Value or @ Autowired directly. These two annotations are equivalent to the property nodes when the spring application context xml file defines beans. Equivalent to calling the set method of each property.
<bean id="person" class="com.myapp.core.spel.xml.Person"> <property name="book" value="book" /> <property name="bookName" value="#{book.name}"/> </bean>
However, when a static method in a java class needs to refer to a spring resource, we need to define static attributes, and then implement the static attribute injection by displaying the set methods that declare them (note that this set method is non-static).
See the following example code, where the set method of the dingdingReceiverMan property injects a configuration "monitor ﹣ log. Dinging. Receiverman" through the @ Value annotation; the set method of the redisutil property injects a bean through the @ Autowired annotation - the class "redisutil" decorated by the @ Component.
@Component @Slf4j public class SendWarningMessage { private static String dingdingReceiverMan; private static RedisUtil redisUtil; static final String ACC_MONITOR_REDIS_KEY_PREFIX = "ACC_MONITOR_"; @Value("${monitor_log.dingding.receiverMan}") public void setDingdingReceiverMan(String value) { dingdingReceiverMan = value; } @Autowired public void setRedisUtil(RedisUtil value) { redisUtil = value; } public static void sendMsg(String mobile, String msg) { ... String redisKey = ACC_MONITOR_REDIS_KEY_PREFIX + MD5Util.md5(warningMsg); if (redisUtil.get(redisKey) != null) { log.info("The mobile phone number has been sent recently. In order to prevent frequent sending, it will not be sent this time. {}", mobile); } else { log.info("Messages sent:{}", warningMsg); DingdingUtil.sendDingDing(dingdingReceiverMan, warningMsg); redisUtil.set(redisKey, warningMsg, 5 * 60); } ... } }