一、新建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;
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;
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());
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。