Spring boot 项目中普通类无法注入service的解决方案

1.创建工具类

package com.jeesite.modules.sys.utils;


import com.jeesite.modules.sys.service.CardLoginUsersService;
import com.jeesite.modules.sys.service.UserService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
	/**
     *  2018.8.21 苏子卿 
	 *  spring boot 项目中  普通类中无法注入service问题解决方案
	 *  
     */
@Component
public class ApplicationContextProvider implements ApplicationContextAware {
    /**
     * 上下文对象实例
     */
    private static ApplicationContext applicationContext;

    @SuppressWarnings("static-access")
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * 获取applicationContext
     *
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通过name获取 Bean.
     *
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    /**
     * 通过class获取Bean.
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 通过name,以及Clazz返回指定的Bean
     *
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }

}
//============================================================上面的不动============================================================================
class MyThread implements Runnable {

    public UserService userService;//换成自己的业务层

    public CardLoginUsersService cardLoginUsersService;//换成自己的业务层

    public StringRedisTemplate stringRedisTemplate;//换成自己的业务层


    public MyThread (){
        this.userService= (UserService ) ApplicationContextProvider.getBean("userService");//替换成自己的service层
        this.cardLoginUsersService= (CardLoginUsersService ) ApplicationContextProvider.getBean("cardLoginUsersService");//替换成自己的service层
        this.stringRedisTemplate= (StringRedisTemplate ) ApplicationContextProvider.getBean("stringRedisTemplate");//替换成自己的service层
//或者

//this.userService= ApplicationContextProvider.getBean(UserService .class);
    }


    @Override
    public void run() {

    }
//=============================下面是get set方法=====================================================
    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public CardLoginUsersService getCardLoginUsersService() {
        return cardLoginUsersService;
    }

    public void setCardLoginUsersService(CardLoginUsersService cardLoginUsersService) {
        this.cardLoginUsersService = cardLoginUsersService;
    }

    public StringRedisTemplate getStringRedisTemplate() {
        return stringRedisTemplate;
    }

    public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }
    
}

2.在你需要注入的类中,调用该方法获取ben对象(实现注入)

package com.jeesite.common.shiro.filter;

import java.io.IOException;
import java.util.Map;


import com.jeesite.modules.sys.service.UserService;


/**
 * 
 * 使用工具实现手动在普通类中注入service对象
 * 
 */
public class testBen {
	
	
	@Autowired
	protected  UserService userService;


	/**
	 * 创建登录授权令牌
	 */
	@Override
	protected String createToken(User user) {
		
			//获取的ben对象转为你需要的对象
			UserService aa = (UserService) ApplicationContextProvider.getBean("userService");//使用工具手动注入注入对象信息

			User user1 = aa.getByLoginCode(users);//按照手机号查询单条数据
			System.out.println("查询出来的数据="+user1);
		
		return "成功";

	}

}

3.然后就会获取到ben对象了。

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页