springboot实现在工具类(util)中调用注入service层方法
作者:勇者神龟 时间:2021-06-17 20:02:51
一、新建BeanUtil类
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class BeanUtil implements ApplicationContextAware, DisposableBean {
private static ApplicationContext applicationContext = null;
/**
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
public static <T> T getBean(Class<T> requiredType) {
if(applicationContext==null){
throw new IllegalStateException("applicaitonContext属性未注入, 请在SpringBoot启动类中注册BeanUtil.");
}
return applicationContext.getBean(requiredType);
}
@Override
public void destroy() {
applicationContext = null;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (BeanUtil.applicationContext != null) {
log.warn("BeanUtil中的ApplicationContext被覆盖, 原有ApplicationContext为:" + BeanUtil.applicationContext);
}
BeanUtil.applicationContext = applicationContext;
}
}
二、在启动类中注入BeanUtil
@SpringBootApplication
public class StartApplication {
public static void main(String[] args) {
SpringApplication.run(StartApplication.class, args);
}
@Bean
public BeanUtil beanUtil() {
return new BeanUtil();
}
}
三、在Util类中获取Service
public class MyUtils {
public static UserDetails getCurrentUser() {
MyUserDetailsService userDetailsService = BeanUtil.getBean(MyUserDetailsService.class);
//TODO 调用service中的方法处理自己的业务
return null;
}
}
springBoot中普通类直接调用spring管理的dao.service等bean
在项目中经常会用到一些不是controller和service层的去访问dao层,或者是普通类访问service层。
前提:
首先确保dao,serivce全部可以在项目启动的时候扫描到
编写一个工具类,通过工具类去获取ApplicationContext对象,然后通过applicationContext对象去加载spring管理的bean
将该工具类在项目启动类进行引入,否则会报空指针异常。
具体使用方法如下:
1)编写工具
package com.bonc.springboot.utils;
/*
* @desc:提供非SPRING管理类调用管理类的功能
* 注意在服务启动的时候进行import,apllication中引入
*/
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class SpringUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if(SpringUtil.applicationContext == null){
SpringUtil.applicationContext = applicationContext;
}
System.out.println("---------------------------------------------------------------------");
System.out.println("========ApplicationContext配置成功,在普通类可以通过调用SpringUtils.getAppContext()获取applicationContext对象,applicationContext="+SpringUtil.applicationContext+"========");
System.out.println("---------------------------------------------------------------------");
}
//获取applicationContext
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
//通过name获取 Bean.
public static Object getBean(String name){
return getApplicationContext().getBean(name);
}
//通过class获取Bean.
public static <T> T getBean(Class<T> clazz){
return getApplicationContext().getBean(clazz);
}
//通过name,以及Clazz返回指定的Bean
public static <T> T getBean(String name,Class<T> clazz){
return getApplicationContext().getBean(name, clazz);
}
}
2)将该工具类,在服务启动类application 中导入
3)普通类中使用APPlicationContent对象获取对应的bean进行使用:
ApplicationContext appCtx = SpringUtil.getApplicationContext();
public TestDao dao = appCtx.getBean(TestDao.class);
也可写成
public TestDao dao = SpringUtil.getBean(TestDao.class);
具体测试如下:
例:比如有一个请求中调用了实体类,实体类的构造方法调用的dao,进行的数据库的访问
请求:
//普通类进行构造的时候,可以调用dao 对象
@RequestMapping(value="/testClassQuery")
public void testClassQuery(){
TestClassQuery testClassQuery =new TestClassQuery();
}
普通类代码:
**
* @Description: 测试普通类如何调用mapper
*/
public class TestClassQuery {
ApplicationContext appCtx = SpringUtil.getApplicationContext();
public TestDao dao = appCtx.getBean(TestDao.class);
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public TestClassQuery(){
testQuery();
}
public void testQuery(){
user = dao.queryUser("1"); //通过id获取值
System.out.println(user.toString());
}
}
来源:https://blog.csdn.net/u011535199/article/details/115007263
标签:springboot,工具类,注入,service
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
IDEA连接Mysql数据库的详细图文教程
2023-10-09 09:51:24
![](https://img.aspxhome.com/file/2023/5/58605_0s.png)
HashMap原理及put方法与get方法的调用过程
2023-10-06 03:53:02
![](https://img.aspxhome.com/file/2023/1/58601_0s.png)
Java设计模式之访问者模式
2023-04-17 20:00:15
![](https://img.aspxhome.com/file/2023/5/60135_0s.png)
关于Mybatis与JPA的优缺点说明
2023-08-23 22:28:33
![](https://img.aspxhome.com/file/2023/0/58450_0s.jpg)
JavaWeb页面中防止点击Backspace网页后退情况
2023-11-10 10:36:36
学生视角看Java 面向对象的继承本质
2022-04-23 21:51:10
![](https://img.aspxhome.com/file/2023/0/59740_0s.png)
Spring boot 集成Dubbox的方法示例
2023-09-22 23:49:04
Java回调函数与观察者模式实例代码
2023-11-16 17:30:11
C++实现softmax函数的面试经验
2023-06-16 02:07:47
![](https://img.aspxhome.com/file/2023/1/66321_0s.png)
SpringBoot配置全局异常处理器捕获异常详解
2023-11-28 05:08:07
Android View移动的六种方法小结
2023-07-06 02:43:39
Java面试题冲刺第二十二天-- Nginx
2023-11-16 13:43:48
![](https://img.aspxhome.com/file/2023/6/58916_0s.png)
executor包执行器功能
2023-07-26 21:07:36
SpringBoot FreeWorker模板技术解析
2023-08-09 09:14:39
![](https://img.aspxhome.com/file/2023/6/57826_0s.png)
浅谈java的接口和C++虚类的相同和不同之处
2023-08-05 12:01:51
Java 用Prometheus搭建实时监控系统过程详解
2023-09-06 12:07:40
![](https://img.aspxhome.com/file/2023/0/58470_0s.jpg)
Java使用iTextPDF生成PDF文件的实现方法
2023-10-15 10:44:13
![](https://img.aspxhome.com/file/2023/7/58687_0s.png)
java数据结构和算法中哈希表知识点详解
2023-08-05 08:28:11
![](https://img.aspxhome.com/file/2023/3/57963_0s.png)
Android RecyclerView基本使用详解
2023-07-24 21:13:30
![](https://img.aspxhome.com/file/2023/6/86016_0s.gif)
Spring boot配置文件加解密详解
2023-11-12 00:17:29
![](https://img.aspxhome.com/file/2023/3/59583_0s.jpg)