<!-- 面包屑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>
走向现代化数据分析架构:趋势与挑战
Acrobat无法在本页面上执行文本识别
解决QTTabBar2048无法卸载或者安装问题
ArcPy获取要素类的别名
ArcGIS Pro 安装语言包报指定路径为空
使用MxDraw52后台操作dwg文件
DevExpress列表取消右键折叠展开菜单
QT界面开发入门7 — 菜单栏没有转到槽
IO,NIO和AIO是Java网络编程的三种模型