SpringBoot如何实现调用controller和Service层方法
更新时间:2025年03月08日 14:04:48 作者:北极的企鹅88
文章介绍了在SpringBoot中如何在工具类中调用Controller和Service层的方法,通过创建一个工具类SpringUtil,并在Spring Boot启动类中进行配置扫描注入,工具类就可以访问Controller和Service层的方法
SpringBoot调用controller和Service层方法
说明
- 最近遇到一个问题,如何在工具类中去访问controller层与service层的方法。
- 因为平时在调用service层时都是在controller中,有配置扫描注入,spring会根据配置自动注入所依赖的服务层。
- 但因我们写的工具类不属于controller层,所以当所写接口需要调用服务层是,常常会为NULL。
实现
1.创建一个工具类:SpringUtil
package com.ruoyi.web.controller.employment.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
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;
}
}
//获取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.SpringBoot启动类调用controller和service方法
package com.ruoyi;
import com.ruoyi.web.controller.employment.ProvportController1;
import com.ruoyi.web.controller.employment.ProvportController2;
import com.ruoyi.web.controller.employment.utils.SpringUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ApplicationContext;
/**
* 启动程序
*
* @author ruoyi
*/
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class RuoYiApplication
{
//注入service
private static TestService testService;
//注入controller
private static TestController testController;
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(RuoYiApplication.class, args);
ApplicationContext context = SpringUtil.getApplicationContext();
testService= context.getBean(testService.class);
testController= context.getBean(testController.class);
//调用service方法
testService.getService();
//调用controller方法
testController.getController();
}
}
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- springboot中的controller参数映射问题小结
- springboot中Controller内文件上传到本地及阿里云操作方法
- springboot如何通过controller层实现页面切换
- springboot Controller直接返回String类型带来的乱码问题及解决
- SpringBoot之controller参数校验详解
- springboot中@RestController注解实现
- SpringBoot通过注解监测Controller接口的代码示例
- springboot controller参数注入方式
- SpringBoot中@RestControllerAdvice @ExceptionHandler异常统一处理类失效原因分析
- SpringBoot和MybatisPlus实现通用Controller示例
相关文章
Java中list.foreach()和list.stream().foreach()用法详解
在Java中List是一种常用的集合类,用于存储一组元素,List提供了多种遍历元素的方式,包括使用forEach()方法和使用Stream流的forEach()方法,这篇文章主要给大家介绍了关于Java中list.foreach()和list.stream().foreach()用法的相关资料,需要的朋友可以参考下2024-07-07
SpringCloud Gateway的熔断限流配置实现方法
Spring Cloud Gateway支持通过配置熔断和限流机制来保证服务的稳定性和可用性,可通过Hystrix和Resilience4j两种方式实现。Hystrix通过注解和配置文件实现熔断限流,Resilience4j通过编程式配置实现2023-04-04
深入理解Java8新特性之Stream API的终止操作步骤
Stream是Java8的一大亮点,是对容器对象功能的增强,它专注于对容器对象进行各种非常便利、高效的 聚合操作(aggregate operation)或者大批量数据操作。Stream API借助于同样新出现的Lambda表达式,极大的提高编程效率和程序可读性,感兴趣的朋友快来看看吧2021-11-11


最新评论