Issue 1:
Sam Brannen opened SPR-8450 and commented

Background

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 through 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.

Note that self-autowiring by name via @Resource is permitted by the framework; whereas, self-autowiring by type is not permitted by the framework as can be seen in the following code snippet from DefaultListableBeanFactory's findAutowireCandidates(String, Class, DependencyDescriptor) method.

for (String candidateName : candidateNames) {
if (!candidateName.equals(beanName) && isAutowireCandidate(candidateName, descriptor)) {
result.put(candidateName, getBean(candidateName));
}
}

The name of the bean (i.e., the bean that's trying to autowire itself) is beanName. That bean is in fact an autowire candidate, but the above if-condition returns false (since candidateName equals the beanName). Thus you simply cannot autowire a bean with itself by type (at least not as of Spring 3.1 M2).

Goal

Add support for self-autowiring using @Autowired on fields and methods.

Stack Overflow Discussion

This topic was brought to our attention via a discussion on Stack Overflow.

Affects: 3.0.5

Issue Links:

#21186 Self autowiring does not work for transactional beans

#19532 Self reference fallback in 4.3 is not meant to apply to collection elements

#16794 @Autowired does not work for target bean of type Collection

#18973 Define and document rules for @Autowired self-injection in case of ambiguity

18 votes, 23 watchers

Issue 2:
marc schipperheyn opened SPR-9366 and commented

@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{

This leads to

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)

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.

Issue Links:

#13891 Controller extension doesn't allow extending REST Urls ("is duplicated by")