springboot中bean的手动注入
想拿高薪吗?想摆脱那些生瓜们写的稀烂的项目吗?,从写日记开始。积累就是高薪,进步就是高薪。你还在等什么?让我们一起来学习springboot吧!

前一段时间学习springboot,遇到bean手动注入的问题,搞了很久终于弄出来了,废话不多说直接上代码:
1.下面示例为实现ApplicationContextAware 的工具类,可以通过其它类引用它以操作spring容器及其中的Bean实例。

package com.springbootMybatis.common.tool;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**

  • ContextUtils注册
  • bean的手动注入
  • 实现ApplicationContextAware接口
    */
    @Component
    public class ApplicationContextRegister implements ApplicationContextAware {
    public void setApplicationContext(ApplicationContext applicationContext)
    throws BeansException {
    ContextUtils.setApplicationContext(applicationContext);
    }
    }

2.写个工具类

package com.springbootMybatis.common.tool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
//import groovy.util.logging.Slf4j;
import java.util.Map;

/**

  • spring上下文工具类

  • bean 的手动注入工具类
    */
    @Slf4j
    public class ContextUtils {
    private static ApplicationContext applicationContext;
    public static void setApplicationContext(ApplicationContext applicationContext) {
    synchronized (ContextUtils.class) {
    ContextUtils.applicationContext = applicationContext;
    ContextUtils.class.notifyAll();
    }
    }

    public static ApplicationContext getApplicationContext() {
    synchronized (ContextUtils.class) {
    while (applicationContext == null) {
    try {
    log.debug(“getApplicationContext, wait…”);
    ContextUtils.class.wait(60000);
    if (applicationContext == null) {
    log.warn(“Have been waiting for ApplicationContext to be set for 1 minute”, new Exception());
    }
    } catch (InterruptedException ex) {
    log.debug(“getApplicationContext, wait interrupted”);
    }
    }
    return applicationContext;
    }
    }

    /**

    • 根据名字获取实体
    • @param name
    • @return
      */
      public static Object getBean(String name) {
      return getApplicationContext().getBean(name);
      }

    /**

    • 根据类型获取实体
    • @param
    • @param cls
    • @return
      */
      public static Map<String, T> getBeansOfType(Class cls) {
      return getApplicationContext().getBeansOfType(cls);
      }

}
3.写个手动注入的测试方法

package com.springbootMybatis.common.tool;
import com.springbootMybatis.demo.service.UserService;
import com.springbootMybatis.demo.utils.GeneralReturn;
/**

  • bean的手动注入测试
  • @author Administrator

*/
public class beanInjection {
private static UserService UserService = (com.springbootMybatis.demo.service.UserService) ContextUtils.getBean(“UserServiceImpl”);
public static GeneralReturn aaa() {
GeneralReturn GR = UserService.selectAll();
System.out.println(GR);
return GR;
}
}

4.在这展示一下接口,具体怎么业务层代码和持久层代码就不贴了。

package com.springbootMybatis.demo.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.springbootMybatis.common.tool.beanInjection;
import com.springbootMybatis.demo.service.UserService;
import com.springbootMybatis.demo.utils.GeneralReturn;
@RestController
@RequestMapping
@Slf4j
public class test {
@Autowired
private UserService UserService;
/**
* bean的自动注入
* @return
/
@RequestMapping("/test1")
public GeneralReturn test(){
return UserService.selectAll();
}
/
*
* bean的手动注入
* @return
*/
@RequestMapping("/test2")
public GeneralReturn test2(){
return beanInjection.aaa();
}
}

最后我要提醒一下,在写service的实现时候,注解@Service(“UserServiceImpl”)需要bean的名称不然找不到。
各位老铁,我是新手,接触springboot框架不久,写的不对的请指教。

项目源码:https://github.com/PythonDjangoWeb/springbootMybatis.git

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐