Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the failure of spring object injection in netty

Shulou Source: shulou.com Published: 2022-06-01 17:50:31 09月27日 Update

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.

Tags: Objects containers functions data articles upper and lower context content location different proper aura special headache as the name implies business things code functions can be obtained through the Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MariaDB Shulou Information Linux Redmi