In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian introduces in detail "how to solve the failure of spring object injection in netty", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to solve the failure of spring object injection in netty" can help you solve your doubts.
Failed to inject spring objects in netty
Today, when I was working on a project, I found that injecting service into netty failed, and Baidu couldn't find the answer (@ Component,@PostConstruct) for a long time.
Found out what the problem is.
As shown in the figure:
All of these places must be injected through spring to achieve other dependency injection. Previously, new was used here, so spring injection failed.
Injecting spring components into netty
Not long ago, database data was used in Netty. Because the context after Netty service startup is different from that of Spring, it is a headache to obtain DAO data in Netty and can not be injected with @ Autowired.
Aware means "automatic", and as its name implies, Spring does something automatically. In this special case, Bean needs to implement a function, but the function must rely on the Spring container. At this time, you must first obtain the Spring container, and then use the Spring container to achieve this function.
In order for Bean to get the Spring container it is in, you can have the Bean implement the ApplicationContextAware interface.
You can inject Spring Autowired * / @ Componentpublic class ToolNettySpirngAutowired implements ApplicationContextAware {private static ApplicationContext applicationContext into import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component; / * * @ author shilei * * @ time at 5:17:50 * * @ desc Netty on June 19, 2019. @ Override public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {if (ToolNettySpirngAutowired.applicationContext = = null) {ToolNettySpirngAutowired.applicationContext = applicationContext;}} / / get applicationContext public static ApplicationContext getApplicationContext () {return applicationContext;} / / get Bean through name. Public static Object getBean (String name) {return getApplicationContext (). GetBean (name);} / / get Bean through class. Public static T getBean (Class clazz) {return getApplicationContext () .getBean (clazz);} / / the specified Bean public static T getBean (String name, Class clazz) {return getApplicationContext () .getBean (name, clazz);}} is returned through name and Clazz.
You can add the following code to a business Handler when using it:
Private static NodeServService nodeServService; static {nodeServService = ToolNettySpirngAutowired.getBean (NodeServService.class);} private static NodeJpaRepository nodeDao; static {nodeDao = ToolNettySpirngAutowired.getBean (NodeJpaRepository.class);} read here, this article "how to solve the problem of spring object injection failure in netty" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.