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

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

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

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

解決Springboot @Autowired 無(wú)法注入問(wèn)題

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

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

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

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

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)用上下文
  // 下面的這個(gè)方法上加了@override注解,原因是繼承applicationcontextaware接口是必須實(shí)現(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)程序啟動(dòng)時(shí),實(shí)例化 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ù)器啟動(dòng)測(cè)試!");
}

    2)在使用 @service 的方法中,通過(guò)@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 無(wú)法注入問(wèn)題,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品女主播自在线拍 | chinses台湾男同志hd | 亚州人成网在线播放 | 亚洲国产自拍在线 | 国产免费午夜 | 国产宅男 | 亚洲成在人线久久综合 | 男人视频网 | 国产亚洲福利精品一区二区 | 香蕉久久一区二区不卡无毒影院 | 亚洲国产精品综合一区在线 | 国产一区二区精品久久 | 九9热这里只有真品 | 日本偷偷操 | 99视频免费在线观看 | 男人天堂网av | 成年视频在线观看免费 | 大学生按摩黄a级中文片 | 国产欧美视频一区二区三区 | 青草香蕉精品视频在线观看 | japanese乱子mate | 女生被爆操 | 国产视频分类 | 亚洲 小说 欧美 激情 另类 | 关晓彤被调教出奶水的视频 | 日韩a无吗一区二区三区 | 亚洲欧美日韩精品久久亚洲区 | 四虎最新永久在线精品免费 | 日韩在线观看网址 | 99福利影院| 18美女光胸光屁屁洗澡 | 韩国美女主播在线 | 欧美成人免费观看的 | 草草精品视频 | 好大好热 | 婷婷日韩 | 国产51| 精品女同同性视频很黄很色 | 1024视频色版在线网站 | china精品对白普通话 | 欧美性bbbbbxxxxxxx |