Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@RequestMapping detection breaks when introducing interface [SPR-11589] #16213

Closed
spring-projects-issues opened this issue Mar 24, 2014 · 2 comments
Closed
Assignees
Labels
in: web status: declined type: enhancement

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Mar 24, 2014

Thomas Scheinecker opened SPR-11589 and commented

In my current application I facilitate the @RequestMapping and @RestController annotations for creating RESTful controllers.

To maintain a clean structure I decided to create a interface so all CRUDL methods have the same name.

But the problem is that as soon as my controller implements an interface the request mapping isn't applied anymore. It doesn't even matter which interface, and even marker interfaces like the one in the referenced gist break the mapping.

I tracked the problem down to the following steps:

During the mapping process in

...

protected void initHandlerMethods() {
	if (logger.isDebugEnabled()) {
		logger.debug("Looking for request mappings in application context: " + getApplicationContext());
	}

	String[] beanNames = (this.detectHandlerMethodsInAncestorContexts ?
				BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) :
				getApplicationContext().getBeanNamesForType(Object.class));

	for (String beanName : beanNames) {
		if (isHandler(getApplicationContext().getType(beanName))){
			detectHandlerMethods(beanName);
		}
	}
	handlerMethodsInitialized(getHandlerMethods());
}
...

the type is retreived in

...
@Override
public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
	String beanName = transformedBeanName(name);

	// Check manually registered singletons.
	Object beanInstance = getSingleton(beanName, false);
	if (beanInstance != null) {
		if (beanInstance instanceof FactoryBean && !BeanFactoryUtils.isFactoryDereference(name)) {
			return getTypeForFactoryBean((FactoryBean<?>) beanInstance);
		}
		else {
			return beanInstance.getClass();
		}
	}
...
}

...

in this particular code block codereturn beanInstance.getClass();code (line 574) is executed, which resolves to com.sun.proxy.$Proxy82@XXXX
which in turn leads to the problem that

...
	
@Override
protected boolean isHandler(Class<?> beanType) {
	return ((AnnotationUtils.findAnnotation(beanType, Controller.class) != null) || (AnnotationUtils.findAnnotation(beanType, RequestMapping.class) != null));
}

...

evaluates to false as the proxy class of course doesn't define any annotations. Therefore the request mapping isn't registered.

Everything works fine if I use an abstract class instead, but breaks as soon as I add an interface to that particular abstract class.

As additional info:
i use spring-boot to start the application, and run java 1.7.0_51


Affects: 4.0.1, 4.0.2

Reference URL: https://gist.github.com/tscheinecker/27cb4e683892b078cd9c

1 votes, 3 watchers

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Mar 24, 2014

Juergen Hoeller commented

A key question is: Why are your controllers getting proxied in the first place? Do you have AOP auto-proxying activated, potentially with some very wide pointcuts? Reducing the scope of those pointcuts to not match controllers anymore, or switching the proxy mode to target-class proxies, would solve the problem then.

Also, are you sure this fails against 4.0.2? We fixed #16043 there, not proxying just because of configuration callback interfaces and empty marker interfaces anymore... However, with non-empty interfaces, the problem remains and needs to be addressed along the lines of my suggestions above.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Dec 12, 2018

Brian Clozel commented

Resolving for now due to lack of feedback.

@spring-projects-issues spring-projects-issues added status: declined type: enhancement in: web labels Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web status: declined type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants