<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=sbrannen" rel="nofollow">Sam Brannen</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-8450?redirect=false" rel="nofollow">SPR-8450</a></strong> and commented</p>
<h4 dir="auto">Background</h4>
<p dir="auto">Autowiring a bean with an instance of itself is not something one would normally do, but there are cases where it might be useful -- for example, to route method calls <em>through</em> the proxy that wraps the bean. There are of course alternatives to this, such as using load-time weaving with AspectJ proxies instead of JDK dynamic proxies.</p>
<p dir="auto">Note that self-autowiring <em>by name</em> via <code class="notranslate">@Resource</code> <strong>is</strong> permitted by the framework; whereas, self-autowiring <em>by type</em> is <strong>not</strong> permitted by the framework as can be seen in the following code snippet from <code class="notranslate">DefaultListableBeanFactory</code>'s <code class="notranslate">findAutowireCandidates(String, Class, DependencyDescriptor)</code> method.</p>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="for (String candidateName : candidateNames) {
    if (!candidateName.equals(beanName) &amp;&amp; isAutowireCandidate(candidateName, descriptor)) {
        result.put(candidateName, getBean(candidateName));
    }
}"><pre class="notranslate"><span class="pl-k">for</span> (<span class="pl-smi">String</span> <span class="pl-s1">candidateName</span> : <span class="pl-s1">candidateNames</span>) {
    <span class="pl-k">if</span> (!<span class="pl-s1">candidateName</span>.<span class="pl-en">equals</span>(<span class="pl-s1">beanName</span>) &amp;&amp; <span class="pl-en">isAutowireCandidate</span>(<span class="pl-s1">candidateName</span>, <span class="pl-s1">descriptor</span>)) {
        <span class="pl-s1">result</span>.<span class="pl-en">put</span>(<span class="pl-s1">candidateName</span>, <span class="pl-en">getBean</span>(<span class="pl-s1">candidateName</span>));
    }
}</pre></div>
<p dir="auto">The name of the bean (i.e., the bean that's trying to autowire itself) is <code class="notranslate">beanName</code>. That bean is in fact an autowire candidate, but the above if-condition returns <code class="notranslate">false</code> (since <code class="notranslate">candidateName</code> equals the <code class="notranslate">beanName</code>). Thus you simply cannot autowire a bean with itself <em>by type</em> (at least not as of Spring 3.1 M2).</p>
<h4 dir="auto">Goal</h4>
<p dir="auto">Add support for self-autowiring using <code class="notranslate">@Autowired</code> on fields and methods.</p>
<h4 dir="auto">Stack Overflow Discussion</h4>
<p dir="auto">This topic was brought to our attention via a discussion on <a href="http://stackoverflow.com/questions/5152686/self-injection-with-spring" rel="nofollow">Stack Overflow</a>.</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 3.0.5</p>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398225102" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/21186" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/21186/hovercard" href="https://github.com/spring-projects/spring-framework/issues/21186">#21186</a> Self autowiring does not work for transactional beans</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398201427" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/19532" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/19532/hovercard" href="https://github.com/spring-projects/spring-framework/issues/19532">#19532</a> Self reference fallback in 4.3 is not meant to apply to collection elements</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398171632" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/16794" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/16794/hovercard" href="https://github.com/spring-projects/spring-framework/issues/16794">#16794</a> <code class="notranslate">@Autowired</code> does not work for target bean of type Collection</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398194717" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/18973" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/18973/hovercard" href="https://github.com/spring-projects/spring-framework/issues/18973">#18973</a> Define and document rules for <code class="notranslate">@Autowired</code> self-injection in case of ambiguity</li>
</ul>
<p dir="auto">18 votes, 23 watchers</p>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=mschipperheyn" rel="nofollow">marc schipperheyn</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-9366?redirect=false" rel="nofollow">SPR-9366</a></strong> and commented</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="@Controller
@RequestMapping(&quot;/my&quot;)
public class MyController extends ExtendedController

@RequestMapping(method = RequestMethod.POST, value = &quot;/post/&quot;)
	public String post(
		@ModelAttribute(&quot;vo&quot;) MyVO vo,
	        BindingResult result,
	        ModelMap model,
	        NativeWebRequest request) throws Exception{
[...]

@Controller
@RequestMapping(&quot;/my/extend&quot;)
public class MyController extends ExtendedController

@RequestMapping(method = RequestMethod.POST, value = &quot;/post/&quot;)
	public String extendPost(
		@ModelAttribute(&quot;vo&quot;) MyVO vo,
	        BindingResult result,
	        ModelMap model,
	        NativeWebRequest request) throws Exception{
"><pre class="notranslate"><code class="notranslate">@Controller
@RequestMapping("/my")
public class MyController extends ExtendedController

@RequestMapping(method = RequestMethod.POST, value = "/post/")
	public String post(
		@ModelAttribute("vo") MyVO vo,
	        BindingResult result,
	        ModelMap model,
	        NativeWebRequest request) throws Exception{
[...]

@Controller
@RequestMapping("/my/extend")
public class MyController extends ExtendedController

@RequestMapping(method = RequestMethod.POST, value = "/post/")
	public String extendPost(
		@ModelAttribute("vo") MyVO vo,
	        BindingResult result,
	        ModelMap model,
	        NativeWebRequest request) throws Exception{

</code></pre></div>
<p dir="auto">This leads to</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path '/my/extend/post/': {public java.lang.String nlproject.controller.MyExtendController.extendPost(nl.project.VO.MyVO,org.springframework.validation.BindingResult,org.springframework.ui.ModelMap,org.springframework.web.context.request.NativeWebRequest) throws java.lang.Exception, public java.lang.String nl.project.controller.MyController.post(nl.project.VO.MyVO,org.springframework.validation.BindingResult,org.springframework.ui.ModelMap,org.springframework.web.context.request.NativeWebRequest)}. If you intend to handle the same path in multiple methods, then factor them out into a dedicated handler class with that path mapped at the type level!
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodResolver.resolveHandlerMethod(AnnotationMethodHandlerAdapter.java:642)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:431)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)"><pre class="notranslate"><code class="notranslate">java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path '/my/extend/post/': {public java.lang.String nlproject.controller.MyExtendController.extendPost(nl.project.VO.MyVO,org.springframework.validation.BindingResult,org.springframework.ui.ModelMap,org.springframework.web.context.request.NativeWebRequest) throws java.lang.Exception, public java.lang.String nl.project.controller.MyController.post(nl.project.VO.MyVO,org.springframework.validation.BindingResult,org.springframework.ui.ModelMap,org.springframework.web.context.request.NativeWebRequest)}. If you intend to handle the same path in multiple methods, then factor them out into a dedicated handler class with that path mapped at the type level!
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodResolver.resolveHandlerMethod(AnnotationMethodHandlerAdapter.java:642)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:431)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
</code></pre></div>
<p dir="auto">The exception suggests that the paths are identical, which they are not. Apparently, Spring checks for path uniqueness at the method level without taking into account the base mapping at the classlevel.</p>
<hr>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398118182" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/13891" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/13891/hovercard" href="https://github.com/spring-projects/spring-framework/issues/13891">#13891</a> Controller extension doesn't allow extending REST Urls (<em><strong>"is duplicated by"</strong></em>)</li>
</ul>