In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "SpringBoot project how to inject Bean into the ordinary class", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "SpringBoot project how to inject Bean into the ordinary class" bar!
How to inject Bean into a normal class Spring managed classes get an injected Bean way
@ Autowired is an annotation that can mark member variables, methods, and constructors to automatically assemble and execute the current method. If the method has parameters, it will automatically look for parameters of the same type in the IOC container to pass values.
For example, injecting Bean into Controller can be written as follows:
@ RestControllerpublic class TestController {@ Autowired public TestBean bean;// profile bean} non-Spring managed classes get an injected Bean mode
The above method of injection through the @ Autowired annotation can only be used in a class managed by Spring, while the Bean obtained in a normal class in this way is null.
You can inject Bean into a normal class using the way the Spring context ApplicationContext gets Bean
Get Beanpublic class Test {/ / declare the Bean variable to be injected private static TestBean bean; / / get Beanpublic static void setApplicationContext (ApplicationContext applicationContext) {bean = applicationContext.getBean (TestBean.class) through the applicationContext context in the ordinary class through the ApplicationContext context;}}
Modify the SpringBoot startup class
Pass ApplicationContext to the normal class @ SpringBootApplicationpublic class TestrApplication {public static void main (String [] args) {final ApplicationContext applicationContext = SpringApplication.run (TestApplication.class, args); / / pass the context to the Test class to detect whether the tcp connection is normal Test.setApplicationContext (applicationContext);}}
In this way, the Bean of Spring can be injected into ordinary classes.
How to get Bean node / * * file name: SpringContextUtil.java * description: get bean instance tool class * modifier: Wang Chinda * modification time: 2018-10-30 * modified content: added * / package com.chinda.utils;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component / * * get bean instance utility class * @ author Wang Chinda * @ date 2018-10-30 * @ see * @ since 1.0 * / @ Componentpublic class SpringContextUtil implements ApplicationContextAware {/ * contextual object instance * / private static ApplicationContext applicationContext; @ Override public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {this.applicationContext = applicationContext } / * get applicationContext * * @ return * / public static ApplicationContext getApplicationContext () {return applicationContext;} / * get Bean through name. * * @ param name * @ return * / public static Object getBean (String name) {return getApplicationContext () .getBean (name);} / * obtain Bean through class. * * @ param clazz * @ param * @ return * / public static T getBean (Class clazz) {return getApplicationContext () .getBean (clazz) } / * * return the specified Bean * * @ param name * @ param clazz * @ param * @ return * / public static T getBean (String name, Class clazz) {return getApplicationContext () .getBean (name, clazz) via name and Class }} Thank you for your reading, the above is the content of "how the SpringBoot project injects Bean into the ordinary class". After the study of this article, I believe you have a deeper understanding of how the SpringBoot project injects Bean into the ordinary class, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.