三分钟学会@Autowired@Qualifier@Primary注解
<!-- 面包屑end -->
<div class="detail-title-wrap">
<h1 class="detail-title">
<span class="d-t js-title">三分钟学会@Autowired@Qualifier@Primary注解</span>
</h1>
<div class="dc-profile clearfix">
<div class="l">
<span class="spacer">2018.10.08 20:24</span>
<span class="spacer spacer-2">154浏览</span>
</div>
<div class="r path-right-box">
<div class="rinfo l">
</div>
</div>
</div>
</div>
<!-- 手记详情 -->
<div class="detail-content-wrap">
<div class="detail-content js-lookimg">
<div><p>今天主要简单的跟大家介绍一下spring自动装配相关的<strong>@Autowired,@Qualifier和@Primary注解</strong></p><p><strong>1,@Autowired注解的使用</strong></p><p>继续上文深入理解spring注解之@ComponentScan注解中的例子,现在我们需要在UserService中调用UserDao相关操作,那我们可以在UserService中增加如下代码:</p><section><pre><code class="hljs java"><span class=""><span class="hljs-keyword"><span class="hljs-keyword">import</span></span></span> com.zhang.dao.UserDao;<br><span class=""><span class="hljs-meta"><span class="hljs-meta">@Service</span></span></span><br><span class=""><span class="hljs-meta"><span class="hljs-meta">public</span></span></span> <span class=""><span class=""><span class="hljs-class"><span class="hljs-keyword"><span class="hljs-class"><span class="hljs-keyword">class</span></span></span></span></span><span class="hljs-class"><span class="hljs-class"> </span></span><span class=""><span class="hljs-class"><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">UserService</span></span></span></span></span><span class="hljs-class"><span class="hljs-class"> </span></span></span>{<br> <span class=""><span class="hljs-meta"><span class="hljs-meta">@Autowired</span></span></span><br> <span class=""><span class="hljs-keyword"><span class="hljs-keyword">private</span></span></span> UserDao userDao;<br> <span class=""><span class="hljs-comment"><span class="hljs-comment">/**</span></span><br><span class="hljs-comment"><span class="hljs-comment"> * 增加一个tostring方法 方便测试</span></span><br><span class="hljs-comment"><span class="hljs-comment"> */</span></span></span><br> <span class=""><span class="hljs-meta"><span class="hljs-meta">@Override</span></span></span><br> <span class=""><span class=""><span class="hljs-function"><span class="hljs-keyword"><span class="hljs-function"><span class="hljs-keyword">public</span></span></span></span></span><span class="hljs-function"><span class="hljs-function"> String </span></span><span class=""><span class="hljs-function"><span class="hljs-title"><span class="hljs-function"><span class="hljs-title">toString</span></span></span></span></span><span class=""><span class="hljs-function"><span class="hljs-params"><span class="hljs-function"><span class="hljs-params">()</span></span></span></span></span><span class="hljs-function"><span class="hljs-function"> </span></span></span>{<br> <span class=""><span class="hljs-keyword"><span class="hljs-keyword">return</span></span></span> <span class=""><span class="hljs-string"><span class="hljs-string">"UserService [userDao="</span></span></span> + userDao + <span class=""><span class="hljs-string"><span class="hljs-string">"]"</span></span></span>;<br> }<br>}<br></code></pre></section><p>测试代码如下:</p><section><pre><code class="hljs scala"><span class="hljs-type"><span class="hljs-type">AnnotationConfigApplicationContext</span></span> applicationContext2 = <span class=""><span class="hljs-keyword"><span class="hljs-keyword">new</span></span></span> <span class="hljs-type"><span class="hljs-type">AnnotationConfigApplicationContext</span></span>(<span class="hljs-type"><span class="hljs-type">MainScanConfig</span></span>.<span class="hljs-keyword"><span class="hljs-keyword">class</span></span>);<br><span class="hljs-type"><span class="hljs-type">UserService</span></span> <span class=""><span class="hljs-class"><span class="hljs-keyword"><span class="hljs-class"><span class="hljs-keyword">object</span></span></span></span></span><span class="hljs-class"><span class="hljs-class"> </span></span>= (<span class="hljs-type"><span class="hljs-type">UserService</span></span>) applicationContext2.getBean(<span class=""><span class="hljs-string"><span class="hljs-string">"userService"</span></span></span>);<br><span class="hljs-type"><span class="hljs-type">System</span></span>.<span class="">out</span>.println(<span class=""><span class="hljs-string"><span class="hljs-string">"实例bean为 === "</span></span></span>+<span class=""><span class="hljs-class"><span class="hljs-keyword"><span class="hljs-class"><span class="hljs-keyword">object</span></span></span></span></span><span class="hljs-class"><span class="hljs-class">)</span></span>;<br></code></pre></section><p>运行结果如下:</p><section><pre><code class="hljs diff">实例bean为 <span class=""><span class="hljs-comment"><span class="hljs-comment">=== UserService [userDao=com.zhang.dao.UserDao@51b279c9]</span></span></span><br></code></pre></section><p>根据运行结果我们可以发现<strong>userDao已经成功注入到UserService</strong>中了</p><p>假设现在业务中有一种情况是UserDao是<strong>第三方提供的服务</strong>,我们也不能保证其是否可以成功加入到spring容器中,<strong>那我们也不能因为UserDao没能成功注入到spring容器而使我们整个UserService服务都不能使用</strong>,那这边我们就来演示一下这种情况,如下我们注释掉UserDao的@Repository注解:</p><section><pre><code class="hljs coffeescript"><span class=""><span class="hljs-keyword"><span class="hljs-keyword">import</span></span></span> org.springframework.stereotype.Repository;<br><span class=""><span class="hljs-regexp"><span class="hljs-regexp">//</span></span>@Repository</span><br><span class="">public</span> <span class=""><span class=""><span class="hljs-class"><span class="hljs-keyword"><span class="hljs-class"><span class="hljs-keyword">class</span></span></span></span></span><span class="hljs-class"><span class="hljs-class"> </span></span><span class=""><span class="hljs-class"><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">UserDao</span></span></span></span></span><span class="hljs-class"><span class="hljs-class"> {</span></span></span><br><span class="hljs-class"><span class="hljs-class">}</span></span><br></code></pre></section><p>这个时候你再启动测试类会报如下错误:</p><section><pre><code class="hljs haskell">警告: <span class="hljs-type"><span class="hljs-type">Exception</span></span> encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.<span class="hljs-type"><span class="hljs-type">UnsatisfiedDependencyException</span></span>: <span class="hljs-type"><span class="hljs-type">Error</span></span> creating bean with name <span class="">'userService'</span>: <span class="hljs-type"><span class="hljs-type">Unsatisfied</span></span> dependency expressed through field <span class="">'userDao'</span>; nested exception <span class="">is</span> org.springframework.beans.factory.<span class="hljs-type"><span class="hljs-type">NoSuchBeanDefinitionException</span></span>: <span class="hljs-type"><span class="hljs-type">No</span></span> qualifying bean <span class="hljs-keyword"><span class="hljs-keyword">of</span></span> <span class="hljs-class"><span class="hljs-keyword"><span class="hljs-class"><span class="hljs-keyword">type</span></span></span><span class="hljs-class"> </span></span><span class=""><span class="hljs-class"><span class="hljs-class">'com.zhang.dao.</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">UserDao'</span></span></span></span></span><span class="hljs-class"><span class="hljs-class"> available: expected at least </span></span><span class=""><span class="hljs-class"><span class="hljs-class">1</span></span></span><span class="hljs-class"><span class="hljs-class"> bean which qualifies </span></span><span class=""><span class="hljs-class"><span class="hljs-class">as</span></span></span><span class="hljs-class"><span class="hljs-class"> autowire candidate. </span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">Dependency</span></span></span><span class="hljs-class"> annotations: {</span></span><span class=""><span class="hljs-class"><span class="hljs-class">@</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">org</span></span></span></span></span><span class="hljs-class"><span class="hljs-class">.</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">springframework</span></span></span><span class="hljs-class">.</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">beans</span></span></span><span class="hljs-class">.</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">factory</span></span></span><span class="hljs-class">.</span></span><span class=""><span class="hljs-class"><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">annotation</span></span></span></span></span><span class="hljs-class"><span class="hljs-class">.</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">Autowired</span></span></span><span class="hljs-class">(</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">required</span></span></span><span class="hljs-class">=</span></span><span class=""><span class="hljs-class"><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">true</span></span></span></span></span><span class="hljs-class"><span class="hljs-class">)}</span></span><br><span class="hljs-class"><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">Exception</span></span></span><span class="hljs-class"> </span></span><span class=""><span class="hljs-class"><span class="hljs-class">in</span></span></span><span class="hljs-class"><span class="hljs-class"> thread </span></span><span class=""><span class="hljs-class"><span class="hljs-class">"main"</span></span></span><span class="hljs-class"><span class="hljs-class"> org.springframework.beans.factory.</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">UnsatisfiedDependencyException</span></span></span><span class="hljs-class">: </span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">Error</span></span></span><span class="hljs-class"> creating bean with name </span></span><span class=""><span class="hljs-class"><span class="hljs-class">'userService'</span></span></span><span class="hljs-class"><span class="hljs-class">: </span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">Unsatisfied</span></span></span><span class="hljs-class"> dependency expressed through field </span></span><span class=""><span class="hljs-class"><span class="hljs-class">'userDao'</span></span></span><span class="hljs-class"><span class="hljs-class">; nested exception </span></span><span class=""><span class="hljs-class"><span class="hljs-class">is</span></span></span><span class="hljs-class"><span class="hljs-class"> org.springframework.beans.factory.</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">NoSuchBeanDefinitionException</span></span></span><span class="hljs-class">: </span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">No</span></span></span><span class="hljs-class"> qualifying bean of </span><span class="hljs-keyword"><span class="hljs-class"><span class="hljs-keyword">type</span></span></span><span class="hljs-class"> </span></span><span class=""><span class="hljs-class"><span class="hljs-class">'com.zhang.dao.</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">UserDao'</span></span></span></span></span><span class="hljs-class"><span class="hljs-class"> available: expected at least </span></span><span class=""><span class="hljs-class"><span class="hljs-class">1</span></span></span><span class="hljs-class"><span class="hljs-class"> bean which qualifies </span></span><span class=""><span class="hljs-class"><span class="hljs-class">as</span></span></span><span class="hljs-class"><span class="hljs-class"> autowire candidate. </span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">Dependency</span></span></span><span class="hljs-class"> annotations: {</span></span><span class=""><span class="hljs-class"><span class="hljs-class">@</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">org</span></span></span></span></span><span class="hljs-class"><span class="hljs-class">.</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">springframework</span></span></span><span class="hljs-class">.</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">beans</span></span></span><span class="hljs-class">.</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">factory</span></span></span><span class="hljs-class">.</span></span><span class=""><span class="hljs-class"><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">annotation</span></span></span></span></span><span class="hljs-class"><span class="hljs-class">.</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">Autowired</span></span></span><span class="hljs-class">(</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">required</span></span></span><span class="hljs-class">=</span></span><span class=""><span class="hljs-class"><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">true</span></span></span></span></span><span class="hljs-class"><span class="hljs-class">)}</span></span><br><span class="hljs-class"><span class="hljs-class"> at org.springframework.beans.factory.</span></span><span class=""><span class="hljs-class"><span class="hljs-class">annotation</span></span></span><span class="hljs-class"><span class="hljs-class">.</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">AutowiredAnnotationBeanPostProcessor</span></span></span><span class="hljs-class">$</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">AutowiredFieldElement</span></span></span><span class="hljs-class">.inject(</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">AutowiredAnnotationBeanPostProcessor</span></span></span><span class="hljs-class">.</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">java</span></span></span><span class="hljs-class">:</span></span><span class=""><span class="hljs-class"><span class="hljs-class">588</span></span></span><span class="hljs-class"><span class="hljs-class">)</span></span><br><span class="hljs-class"><span class="hljs-class"> at org.springframework.beans.factory.</span></span><span class=""><span class="hljs-class"><span class="hljs-class">annotation</span></span></span><span class="hljs-class"><span class="hljs-class">.</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">InjectionMetadata</span></span></span><span class="hljs-class">.inject(</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">InjectionMetadata</span></span></span><span class="hljs-class">.</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">java</span></span></span><span class="hljs-class">:</span></span><span class=""><span class="hljs-class"><span class="hljs-class">88</span></span></span><span class="hljs-class"><span class="hljs-class">)</span></span><br><span class="hljs-class"><span class="hljs-class"> at org.springframework.beans.factory.</span></span><span class=""><span class="hljs-class"><span class="hljs-class">annotation</span></span></span><span class="hljs-class"><span class="hljs-class">.</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">AutowiredAnnotationBeanPostProcessor</span></span></span><span class="hljs-class">.postProcessPropertyValues(</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">AutowiredAnnotationBeanPostProcessor</span></span></span><span class="hljs-class">.</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">java</span></span></span><span class="hljs-class">:</span></span><span class=""><span class="hljs-class"><span class="hljs-class">366</span></span></span><span class="hljs-class"><span class="hljs-class">)</span></span><br><span class="hljs-class"><span class="hljs-class"> at org.springframework.beans.factory.support.</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">AbstractAutowireCapableBeanFactory</span></span></span><span class="hljs-class">.populateBean(</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">AbstractAutowireCapableBeanFactory</span></span></span><span class="hljs-class">.</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">java</span></span></span><span class="hljs-class">:</span></span><span class=""><span class="hljs-class"><span class="hljs-class">1268</span></span></span><span class="hljs-class"><span class="hljs-class">)</span></span><br><span class="hljs-class"><span class="hljs-class"> at org.springframework.beans.factory.support.</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">AbstractAutowireCapableBeanFactory</span></span></span><span class="hljs-class">.doCreateBean(</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">AbstractAutowireCapableBeanFactory</span></span></span><span class="hljs-class">.</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">java</span></span></span><span class="hljs-class">:</span></span><span class=""><span class="hljs-class"><span class="hljs-class">553</span></span></span><span class="hljs-class"><span class="hljs-class">)</span></span><br><span class="hljs-class"><span class="hljs-class"> at org.springframework.beans.factory.support.</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">AbstractAutowireCapableBeanFactory</span></span></span><span class="hljs-class">.createBean(</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">AbstractAutowireCapableBeanFactory</span></span></span><span class="hljs-class">.</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">java</span></span></span><span class="hljs-class">:</span></span><span class=""><span class="hljs-class"><span class="hljs-class">483</span></span></span><span class="hljs-class"><span class="hljs-class">)</span></span><br><span class="hljs-class"><span class="hljs-class"> at org.springframework.beans.factory.support.</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">AbstractBeanFactory</span></span></span><span class="hljs-class">$</span></span><span class=""><span class="hljs-class"><span class="hljs-class">1.</span></span></span><span class="hljs-class"><span class="hljs-class">getObject(</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">AbstractBeanFactory</span></span></span><span class="hljs-class">.</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">java</span></span></span><span class="hljs-class">:</span></span><span class=""><span class="hljs-class"><span class="hljs-class">312</span></span></span><span class="hljs-class"><span class="hljs-class">)</span></span><br><span class="hljs-class"><span class="hljs-class"> at org.springframework.beans.factory.support.</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">DefaultSingletonBeanRegistry</span></span></span><span class="hljs-class">.getSingleton(</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">DefaultSingletonBeanRegistry</span></span></span><span class="hljs-class">.</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">java</span></span></span><span class="hljs-class">:</span></span><span class=""><span class="hljs-class"><span class="hljs-class">230</span></span></span><span class="hljs-class"><span class="hljs-class">)</span></span><br><span class="hljs-class"><span class="hljs-class"> at org.springframework.beans.factory.support.</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">AbstractBeanFactory</span></span></span><span class="hljs-class">.doGetBean(</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">AbstractBeanFactory</span></span></span><span class="hljs-class">.</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">java</span></span></span><span class="hljs-class">:</span></span><span class=""><span class="hljs-class"><span class="hljs-class">308</span></span></span><span class="hljs-class"><span class="hljs-class">)</span></span><br><span class="hljs-class"><span class="hljs-class"> at org.springframework.beans.factory.support.</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">AbstractBeanFactory</span></span></span><span class="hljs-class">.getBean(</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">AbstractBeanFactory</span></span></span><span class="hljs-class">.</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">java</span></span></span><span class="hljs-class">:</span></span><span class=""><span class="hljs-class"><span class="hljs-class">197</span></span></span><span class="hljs-class"><span class="hljs-class">)</span></span><br><span class="hljs-class"><span class="hljs-class"> at org.springframework.beans.factory.support.</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">DefaultListableBeanFactory</span></span></span><span class="hljs-class">.preInstantiateSingletons(</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">DefaultListableBeanFactory</span></span></span><span class="hljs-class">.</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">java</span></span></span><span class="hljs-class">:</span></span><span class=""><span class="hljs-class"><span class="hljs-class">761</span></span></span><span class="hljs-class"><span class="hljs-class">)</span></span><br><span class="hljs-class"><span class="hljs-class"> at org.springframework.context.support.</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">AbstractApplicationContext</span></span></span><span class="hljs-class">.finishBeanFactoryInitialization(</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">AbstractApplicationContext</span></span></span><span class="hljs-class">.</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">java</span></span></span><span class="hljs-class">:</span></span><span class=""><span class="hljs-class"><span class="hljs-class">867</span></span></span><span class="hljs-class"><span class="hljs-class">)</span></span><br><span class="hljs-class"><span class="hljs-class"> at org.springframework.context.support.</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">AbstractApplicationContext</span></span></span><span class="hljs-class">.refresh(</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">AbstractApplicationContext</span></span></span><span class="hljs-class">.</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">java</span></span></span><span class="hljs-class">:</span></span><span class=""><span class="hljs-class"><span class="hljs-class">543</span></span></span><span class="hljs-class"><span class="hljs-class">)</span></span><br><span class="hljs-class"><span class="hljs-class"> at org.springframework.context.</span></span><span class=""><span class="hljs-class"><span class="hljs-class">annotation</span></span></span><span class="hljs-class"><span class="hljs-class">.</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">AnnotationConfigApplicationContext</span></span></span><span class="hljs-class">.<init>(</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">AnnotationConfigApplicationContext</span></span></span><span class="hljs-class">.</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">java</span></span></span><span class="hljs-class">:</span></span><span class=""><span class="hljs-class"><span class="hljs-class">84</span></span></span><span class="hljs-class"><span class="hljs-class">)</span></span><br><span class="hljs-class"><span class="hljs-class"> at com.zhang.</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">ApplicationTest</span></span></span><span class="hljs-class">.main(</span><span class="hljs-type"><span class="hljs-class"><span class="hljs-type">ApplicationTest</span></span></span><span class="hljs-class">.</span><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">java</span></span></span><span class="hljs-class">:</span></span><span class=""><span class="hljs-class"><span class="hljs-class">31</span></span></span><span class="hljs-class"><span class="hljs-class">)</span></span><br></code></pre></section><p>其实很简单这个时候我们只需要在<strong>@Autowired注解</strong>中加上如下属性:</p><section><pre><code class="hljs css">@<span class=""><span class="hljs-keyword"><span class="hljs-keyword">Autowired</span></span></span>(<span class=""><span class="hljs-keyword"><span class="hljs-keyword">required</span></span></span>=<span class=""><span class="hljs-keyword"><span class="hljs-keyword">false</span></span></span>)<br><span class="hljs-keyword"><span class="hljs-keyword">private</span></span> UserDao userDao;<br></code></pre></section><p>再次运行测试类你会发现错误已经消失只是这个时候userDao是null如下:</p><section><pre><code class="hljs javascript">实例bean为 === UserService [userDao=<span class=""><span class="hljs-literal"><span class="hljs-literal">null</span></span></span>]<br></code></pre></section><p>把UserDao代码修改为如下:</p><section><pre><code class="hljs cmake"><span class="">import</span> org.springframework.stereotype.Repository;<br><span class="">@Repository</span><br><span class="">public</span> <span class=""><span class="">class</span> <span class="">UserDao</span> </span>{<br> <span class="">// 给一个默认值</span><br> <span class="">private</span> Integer version = <span class=""><span class="hljs-number"><span class="hljs-number">0</span></span></span>;<br> <span class="">/**<br> * 增加tostring方便测试<br> */</span><br> <span class="">@Override</span><br> <span class=""><span class="">public</span> <span class="hljs-keyword"><span class="hljs-keyword">String</span></span> <span class="">toString</span><span class="">()</span> </span>{<br> <span class=""><span class="hljs-keyword"><span class="hljs-keyword">return</span></span></span> <span class=""><span class="hljs-string"><span class="hljs-string">"UserDao [version="</span></span></span> + version + <span class=""><span class="hljs-string"><span class="hljs-string">"]"</span></span></span>;<br> }<br> <span class="">/**<br> * <span class="">@<span class="hljs-keyword"><span class="hljs-keyword">return</span></span></span> the version<br> */</span><br> <span class=""><span class="">public</span> Integer <span class="">getVersion</span><span class="">()</span> </span>{<br> <span class=""><span class="hljs-keyword"><span class="hljs-keyword">return</span></span></span> version;<br> }<br> <span class="">/**<br> * <span class="">@param</span> version the version to <span class="hljs-keyword"><span class="hljs-keyword">set</span></span><br> */</span><br> <span class=""><span class="">public</span> <span class="">void</span> <span class="">setVersion</span><span class="">(Integer version)</span> </span>{<br> <span class="">this</span>.version = version;<br> }<br>}<br></code></pre></section><p>同时在配置类中增加一个@Bean的UserDao配置如下:</p><section><pre><code class="hljs java"><span class="hljs-meta"><span class="hljs-meta">@Bean</span></span>(<span class="">value</span> = <span class=""><span class="hljs-string"><span class="hljs-string">"userDao2"</span></span></span>)<br><span class=""><span class=""><span class="hljs-function"><span class="hljs-keyword"><span class="hljs-function"><span class="hljs-keyword">public</span></span></span></span></span><span class="hljs-function"><span class="hljs-function"> UserDao </span></span><span class=""><span class="hljs-function"><span class="hljs-title"><span class="hljs-function"><span class="hljs-title">getUserDao</span></span></span></span></span><span class="hljs-function"><span class="hljs-params"><span class="hljs-function"><span class="hljs-params">(</span></span></span></span><span class=""></span><span class="hljs-function"><span class="hljs-params"><span class="hljs-function"><span class="hljs-params">)</span></span></span></span></span>{<br> UserDao userDao = <span class=""><span class="hljs-keyword"><span class="hljs-keyword">new</span></span></span> UserDao();<br> userDao.setVersion(<span class=""><span class="hljs-number"><span class="hljs-number">2</span></span></span>);<br> <span class=""><span class="hljs-keyword"><span class="hljs-keyword">return</span></span></span> <span class=""></span>userDao;<br>}<br></code></pre></section><p>运行测试类结果如下:</p><section><pre><code class="hljs diff">实例bean为 <span class=""><span class="hljs-comment"><span class="hljs-comment">=== UserService [userDao=UserDao [version=0]]</span></span></span><br></code></pre></section><p>我们可以发现这个时候用的是默认的扫描到的UserDao,这个时候我们把UserService中注入的UserDao改成如下:</p><section><pre><code class="hljs css">@<span class=""><span class="hljs-keyword"><span class="hljs-keyword">Autowired</span></span></span>(<span class=""><span class="hljs-keyword"><span class="hljs-keyword">required</span></span></span>=<span class=""><span class="hljs-keyword"><span class="hljs-keyword">false</span></span></span>)<br><span class="hljs-keyword"><span class="hljs-keyword">private</span></span> UserDao userDao2;<br></code></pre></section><p>继续运行测试类结果如下:</p><section><pre><code class="hljs diff">实例bean为 <span class=""><span class="hljs-comment"><span class="hljs-comment">=== UserService [userDao=UserDao [version=2]]</span></span></span><br></code></pre></section><section><pre><code class="hljs less"><span class=""><span class="hljs-variable"><span class="hljs-variable">@Qualifier</span></span>(value=<span class=""><span class="hljs-string"><span class="hljs-string">"userDao"</span></span></span>)</span><br><span class=""><span class="hljs-variable"><span class="hljs-variable">@Autowired</span></span>(required=false)</span><br><span class="">private</span> UserDao userDao2;<br></code></pre></section><p>这个时候不管你UserDao定义什么名字永远只会注入userDao这个bean了</p><section><pre><code class="hljs java"><span class=""><span class="hljs-meta"><span class="hljs-meta">@Primary</span></span></span><br><span class=""><span class="hljs-meta"><span class="hljs-meta">@Bean</span></span></span>(value = <span class=""><span class="hljs-string"><span class="hljs-string">"userDao2"</span></span></span>)<br><span class=""><span class=""><span class="hljs-function"><span class="hljs-keyword"><span class="hljs-function"><span class="hljs-keyword">public</span></span></span></span></span><span class="hljs-function"><span class="hljs-function"> UserDao </span></span><span class=""><span class="hljs-function"><span class="hljs-title"><span class="hljs-function"><span class="hljs-title">getUserDao</span></span></span></span></span><span class=""><span class="hljs-function"><span class="hljs-params"><span class="hljs-function"><span class="hljs-params">()</span></span></span></span></span></span>{<br> UserDao userDao = <span class=""><span class="hljs-keyword"><span class="hljs-keyword">new</span></span></span> UserDao();<br> userDao.setVersion(<span class=""><span class="hljs-number"><span class="hljs-number">2</span></span></span>);<br> <span class=""><span class="hljs-keyword"><span class="hljs-keyword">return</span></span></span> userDao;<br>}<br></code></pre></section><p>这个时候运行测试类你会发现UserService注入的就是userDao2了</p><p> </p></div>
<div>
</div>
</div>
<!-- 标签 -->
<div class="cat-box">
<div class="cat-wrap clearfix">
</div>
</div>
<!-- 推荐 -->
</div>
原文地址:http://www.imooc.com/article/252878
</div>
三分钟学会@Autowired@Qualifier@Primary注解的更多相关文章
- 三分钟学会使用Derby数据库
Derby数据库是一个纯用Java实现的内存数据库,属于Apache的一个开源项目.由于是用Java实现的,所以可以在任何平台上运行:另外一个特点是体积小,免安装,java1.6开始集成了derby数 ...
- 20、自动装配-@Autowired&@Qualifier&@Primary
20.自动装配-@Autowired&@Qualifier&@Primary 自动装配:Spring 利用依赖注入(DI),完成对IOC容器中各个依赖关系赋值 20.1 @Autowi ...
- [转]三分钟学会.NET Core Jwt 策略授权认证
[转]三分钟学会.NET Core Jwt 策略授权认证 一.前言# 大家好我又回来了,前几天讲过一个关于Jwt的身份验证最简单的案例,但是功能还是不够强大,不适用于真正的项目,是的,在真正面对复杂而 ...
- 【Spring注解驱动开发】使用@Autowired@Qualifier@Primary三大注解自动装配组件,你会了吗?
写在前面 [Spring专题]停更一个多月,期间在更新其他专题的内容,不少小伙伴纷纷留言说:冰河,你[Spring专题]是不是停更了啊!其实并没有停更,只是中途有很多小伙伴留言说急需学习一些知识技能, ...
- Spring @Resource,@Autowired,@Qualifier的注解注入和区别
spring2.5提供了基于注解(Annotation-based)的配置,我们可以通过注解的方式来完成注入依赖.在Java代码中可以使用 @Resource或者@Autowired注解方式来经行注入 ...
- 中小研发团队架构实践之生产环境诊断工具WinDbg 三分钟学会.NET微服务之Polly 使用.Net Core+IView+Vue集成上传图片功能 Fiddler原理~知多少? ABP框架(asp.net core 2.X+Vue)模板项目学习之路(一) C#程序中设置全局代理(Global Proxy) WCF 4.0 使用说明 如何在IIS上发布,并能正常访问
中小研发团队架构实践之生产环境诊断工具WinDbg 生产环境偶尔会出现一些异常问题,WinDbg或GDB是解决此类问题的利器.调试工具WinDbg如同医生的听诊器,是系统生病时做问题诊断的逆向分析工具 ...
- 三分钟学会用SpringMVC搭建最小系统(超详细)_转载
前言 做 Java Web 开发的你,一定听说过SpringMVC的大名,作为现在运用最广泛的Java框架,它到目前为止依然保持着强大的活力和广泛的用户群. 本文介绍如何用eclipse一步一步搭建S ...
- [计划任务 - Linux]三分钟学会cron
cron——计划任务,是任务在约定的时间执行已经计划好的工作,是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业. 也就是说cron只适合于linux系统,用windows电脑的同学 ...
- 三分钟学会funsioncharts
由于项目需求需要做一个报表,选择FusionCharts作为工具使用.由于以前没有接触过报表,网上也没有比较详细的fusionCharts教程,所以决定好好研究FusionCharts,同时做一个比较 ...
随机推荐
- 【JZOJ3601】【广州市选2014】Tree(tree)
╰( ̄▽ ̄)╭ 每个非叶子节点,其左右子树叶子节点的权值之和相等.我们称这种二叉树叫平衡二叉树. 我们将一棵平衡二叉树叶子节点的权值从左到右列出来,假如这个权值序列是另一个序列A的子序列,我们称这棵平 ...
- iOS常量(const)、enum以及宏(#define)
http://www.cocoachina.com/ios/20160530/16483.html 本文投稿文章,作者:SuperMario_Nil(简书) 前言:本文主要梳理iOS中如何使用常量.e ...
- 【转载】【技巧总结】PyCharm怎么克隆github上开源的项目
PyCharm怎么clone github上开源的项目 一.先要确保PyCharm正确的配置了Git 如果你已经在PyCharm中配置好了Git,可以跳过此步骤,直接看下一步. 那么怎么在Py ...
- linux 下配置多个tomcat同时运行
一个服务器上内存通常有2G或者更多,一个tomcat 运行管理这么多内存有点力不从心,并且貌似一个进程所能建立的线程数量是有限的,于是我们想要在一个服务器上运行多个tomcat.如下是摘抄自:http ...
- python自定义函数和内置函数
函数 1.定义 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 先定义,后使用 1.2分类 系统函数 自定义函数 1.3语法: def functionname(parameter ...
- JavaScript--缓动动画+轮播图
上效果: 实现步骤: 最重要的是运动公式!!! <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...
- 未能加载文件或程序集 XXX 或它的一个依赖项。参数错误
引发原因 :电脑突然蓝屏重启 解决方法:删除 C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files 下的所有文件 ...
- List容器-LinkedList链表
LinkedList--链表 特点: 删除,增加 用LinkedList性能高 层次查找不适合 查询用ArrayList 数组下标查找 插入和删除慢缺点是要做移位操作 总结:Link ...
- MVC设计之从零打造到实际操作(总汇)
我们为什么要自己搭建一个MVC架构的框架? 1.为了更快的开发效率 2.为了更高的运行效率 3.为了更好的证明自己 在别的框架中,有些方法我们使用起来可能会比较麻烦,我们可以在自己的框架中写一些自己想 ...
- spring调试
spring-beans DefaultListableBeanFactory preInstantiateSingletons:650 RequestMappingHandlerMapping Ab ...