一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務(wù)器之家 - 編程語言 - Java教程 - 解決Springboot @Autowired 無法注入問題

解決Springboot @Autowired 無法注入問題

2021-05-24 13:28不靠譜斯基 Java教程

WebappApplication 一定要在包的最外層,否則Spring無法對所有的類進(jìn)行托管,會造成@Autowired 無法注入。接下來給大家介紹解決Springboot @Autowired 無法注入問題,感興趣的朋友一起看看吧

特別提醒:一定要注意文件結(jié)構(gòu)

  webappapplication 一定要在包的最外層,否則spring無法對所有的類進(jìn)行托管,會造成@autowired 無法注入

1.  添加工具類獲取在 spring 中托管的 bean

  (1)工具類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.common;
import org.springframework.beans.beansexception;
import org.springframework.beans.factory.nosuchbeandefinitionexception;
import org.springframework.context.applicationcontext;
import org.springframework.context.applicationcontextaware;
/**
 * @program: ipc_1p
 * @description: 獲取在spring中托管的bean
 * @author: johnny
 * @create: 2018-08-03 16:24
 **/
public class springcontextutil {
  private static applicationcontext applicationcontext; // spring應(yīng)用上下文
  // 下面的這個方法上加了@override注解,原因是繼承applicationcontextaware接口是必須實現(xiàn)的方法
  public static void setapplicationcontext(applicationcontext applicationcontext)
      throws beansexception {
    springcontextutil.applicationcontext = applicationcontext;
  }
  public static applicationcontext getapplicationcontext() {
    return applicationcontext;
  }
  public static object getbean(string name) throws beansexception {
    return applicationcontext.getbean(name);
  }
  public static object getbean(string name, class requiredtype)
      throws beansexception {
    return applicationcontext.getbean(name, requiredtype);
  }
  public static boolean containsbean(string name) {
    return applicationcontext.containsbean(name);
  }
  public static boolean issingleton(string name)
      throws nosuchbeandefinitionexception {
    return applicationcontext.issingleton(name);
  }
  public static class gettype(string name)
      throws nosuchbeandefinitionexception {
    return applicationcontext.gettype(name);
  }
  public static string[] getaliases(string name)
      throws nosuchbeandefinitionexception {
    return applicationcontext.getaliases(name);
  }
}

  (2)使用

    1)程序啟動時,實例化 springcontextutil

?
1
2
3
4
5
6
7
8
9
10
@springbootapplication
public class webappapplication {
  private static applicationcontext applicationcontext;
  public static void main(string[] args) {
    applicationcontext = springapplication.run(webappapplication.class, args);
    //
    springcontextutil springcontextutil = new springcontextutil();
    springcontextutil.setapplicationcontext(applicationcontext);
    system.out.println("服務(wù)器啟動測試!");
}

    2)在使用 @service 的方法中,通過@autowired 注入,使用springcontexutil 獲取bean上下文

?
1
2
3
4
5
6
7
8
9
10
@autowired
  senderservice senderservice;
public class package_state {
  @autowired
  senderservice senderservice;
  @component
  private package_state() {
    senderservice = (senderservice)springcontextutil.getbean("senderservice");
  }
}

總結(jié)

以上所述是小編給大家介紹的解決springboot @autowired 無法注入問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!

原文鏈接:https://www.cnblogs.com/sylarken/archive/2018/08/07/9435076.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产微拍精品一区 | 麻豆网站在线免费观看 | 欧美区视频 | 四虎网址 | a级亚洲片精品久久久久久久 | 亚洲精品一区二区久久这里 | 逼逼狗影院 | 欧美成人精品福利网站 | 精品国产自在现线拍国语 | 亚洲青草| 91国内精品久久久久怡红院 | 武侠古典久久亚洲精品 | 果冻传媒九一制片厂网站 | 日日爽日日操 | 99影视在线视频免费观看 | 欧美一级欧美三级 | 国产亚洲女人久久久久久 | 欧美日韩一区二区三区免费不卡 | 精品国产国偷自产在线观看 | 毛片免费视频观看 | 国产精品久久久久久久久99热 | 校花小雪灌满了男人们的浓浆 | 精品国产一级毛片大全 | 日韩人成| 成人午夜影院在线观看 | 国产成人精品一区二三区在线观看 | 亚洲 欧美 中文字幕 在线 | 无码国产成人午夜在线观看不卡 | 边打电话边操 | 香蕉久久夜色精品国产尤物 | 国产精品嫩草影院在线看 | 国产第9页 | 村妇超级乱淫伦小说全集 | 国产欧美日韩精品在线 | 精品一久久香蕉国产二月 | 欧美vpswindows | 亚洲国产精品嫩草影院久久 | 和岳m的小说| 白丝萝莉喷水 | 性欧美高清理论片 | 波多野结衣在线观看中文字幕 |