springboot中bean的手动注入

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值