于工具类中@Autowired注入为NULL的问题记录

2019/05/09 17:07
阅读数 54

<div id="cnblogs_post_body" class="blogpost-body"><div>&nbsp;</div> <p>记录:在实体类中加入@Component注解和@Autowired注解时Service不能注入成功。</p> <div class="cnblogs_Highlighter sh-gutter"> <div><div id="highlighter_42067" class="syntaxhighlighter java"><div class="toolbar"><span><a href="#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@Component</code> <code class="java comments">//把普通pojo实例化到spring容器中 0</code></div><div class="line number2 index1 alt1"><code class="java spaces">&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">MyUtil{</code></div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java comments">// 这里是需要注入的Service ①</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Autowired</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">private</code> <code class="java plain">MyService myService; </code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">private</code> <code class="java keyword">static</code> <code class="java plain">MyUtil myUtil; </code></div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java comments">//初始化 ②</code></div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@PostConstruct</code>&nbsp;</div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">init() {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </code></div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">myUtil = </code><code class="java keyword">this</code><code class="java plain">; </code></div><div class="line number11 index10 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">myUtil.myService = </code><code class="java keyword">this</code><code class="java plain">.myService; </code></div><div class="line number12 index11 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">} </code></div><div class="line number13 index12 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java comments">//调用 ③</code></div><div class="line number14 index13 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">static</code> <code class="java keyword">void</code> <code class="java plain">insertParam(</code><code class="java keyword">int</code> <code class="java plain">id){ </code></div><div class="line number15 index14 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java comments">// 调用方法 ④ </code></div><div class="line number16 index15 alt1"><code class="java plain">myUtil.myService.testInsert(</code><code class="java string">"xxx"</code><code class="java plain">);</code></div><div class="line number17 index16 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number18 index17 alt1"><code class="java spaces">&nbsp;</code><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div> </div> <p>注释:0 将普通pojo实例化到容器中、如果无注解myUtil为null<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ① spring自动注入,如果在静态方法中调用此注入类的方法,发现注入为'null';这里不是因为spring未注入,而是被static方法给'清空'了,在无法先于static方法初始化时可以使用@PostConstruct进行初始化。<br>   (注:@PostConstruct修饰的方法会在服务器加载Servle的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。)</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;②使用@PostConstruct注解的方法public void init()先给该类赋值,然后通过@Autowired注入进来。这样不会影响dao等service下面调用的注入。<br>   ③ static方法<br>   ④ 调用方法 myService现在是作为myUtil的属性</p> <p>&nbsp;</p> <p><a href="https://www.cnblogs.com/lovekxc/articles/6899755.html" target="_blank">参考</a></p></div>

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部