In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how SpringBoot implements other common classes such as Service,dao managed by Spring. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Ordinary classes call Service, dao and other bean managed by Spring
In the use of springboot, it is sometimes necessary to call dao or service hosted to spring in other ordinary classes to manipulate the database. Most of the materials on the Internet are about adding some notes, but it doesn't work.
Give an example of a usage scenario.
For example, when the server is in hardware or when the client communicates with Socket, then if the server receives a message and needs to operate the database, how to call Service or dao to operate the database?
Let's take a look at my solution.
(1) first, you need to create a new class to implement the ApplicationContextAware interface.
@ Component public class SpringUtils implements ApplicationContextAware {private static ApplicationContext applicationContext = null; @ Override public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {if (SpringUtils.applicationContext = = null) {SpringUtils.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);} / / return the specified Bean public static T getBean (String name,Class clazz) {return getApplicationContext () .getBean (name, clazz) through name and Clazz;}}
(2) get the ApplicationContext object in the communication class, and then get the service or dao you need.
And then you can call it directly.
It only takes three steps to use classes in service and dao layers in ordinary classes.
In the jsp of a web project, I want to use a class in service to get the data in the database, but after using it, I mistakenly report that it is a null pointer exception. I checked it on the Internet and summed up the solution. It only takes three steps.
1. Write a utility class SpringUtilpackage com.shop.util;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;public final class SpringUtil implements ApplicationContextAware {private static ApplicationContext applicationContext = null; @ Override public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {/ / TODO Auto-generated method stub if (SpringUtil.applicationContext = = null) {SpringUtil.applicationContext = applicationContext System.out.println ("= ApplicationContext configuration is successful. In ordinary classes, you can get the applicationContext object by calling ToolSpring.getAppContext (), applicationContext=" + applicationContext + "="). }} public static ApplicationContext getApplicationContext () {return applicationContext;} / / obtain Bean through public static Object getBean (String name) {return getApplicationContext (). GetBean (name);} / / through class. Public static T getBean (Class clazz) {return getApplicationContext () .getBean (clazz);}} 2. Import the utility class in the Application startup class
Otherwise it still can't be used.
3. Call package com.shop.JZShop;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import com.shop.serviceImpl.GoodsServiceImpl;import com.shop.util.SpringUtil in the ApplicationTests test class @ RunWith (SpringRunner.class) @ SpringBootTestpublic class JzShopApplicationTests {@ Testpublic void contextLoads () {/ / GoodsServiceImpl is the class GoodsServiceImpl goodsServiceImpl = (GoodsServiceImpl) SpringUtil.getBean (GoodsServiceImpl.class) in the service layer I want to get; System.out.println (goodsServiceImpl.getGoodsByID (27). GetGoodsName ()) }} this is the end of this article on "how SpringBoot implements other ordinary classes to call Service,dao and other bean managed by Spring". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.