SpringBoot如何实现调用controller和Service层方法
作者:北极的企鹅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示例
