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

component scanning incorrectly excludes classes [SPR-4324] #9003

Closed
spring-issuemaster opened this issue Jan 11, 2008 · 7 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

commented Jan 11, 2008

Haroon Rafique opened SPR-4324 and commented

The version bump from 2.5 to 2.5.1 breaks component scanning for me. Symptoms are that lots of beans annotated with @Component are being skipped for inclusion as beans. After digging through the code, I think the following commit is the culprit:

http://fisheye1.cenqua.com/changelog/springframework?cs=MAIN:jhoeller:20080107164524

The commit message says:

added "getEnclosingClassName()", "hasEnclosingClass()" and "isIndependent()" methods to ClassMetadata interface; ClassPathScanningCandidateComponentProvider ("context:component-scan") ignores non-static inner and local classes

I am not sure why beanDefinition.getMetadata().isIndependent() is returning false for my classes. The scenario is as follows:

MyBroker has @Component annotation. MyBroker uses a class MyService (regular instantiation) using something like:
MyService service = new MyService()
MyService, in turn, is the class which DOES have a local inner class. I think the bug is in identifying MyBroker as having an inner class. It should not identify MyBroker as having an inner class (since it doesn't have one).

ClassMetaDataReadingVisitor.java has the following method:

public boolean isIndependent() {
	return (this.enclosingClassName == null || this.independentInnerClass);
}

In that method, while trying to see if MyBroker is a candidate, enclosingClassName is set to MyService.

I think this needs a quick fix.


Affects: 2.5.1

Attachments:

Issue Links:

  • #9134 Weird autowiring bug ("is duplicated by")
  • #9029 context:annotation-config skips classes using inner classes in 2.5.1 ("is duplicated by")
  • #9204 bug in context:component-scan when class have inner classes ("is duplicated by")
  • #9017 Classes with Inner classes are not recognized as independent classes themselves ("is duplicated by")
  • #9363 @Component annotated class that contains a private non-static inner class not considered as candidate component because beanDefinition.getMetadata().isIndependent() returns false ("is duplicated by")
  • #9136 Component-scan does not work correctly for annotated service SimpleJdbcClinic in sample 'petclinic'
@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jan 13, 2008

Sam Brannen commented

Consult the forum for possible further discussion apart from comments here:

http://forum.springframework.org/showthread.php?t=48452

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jan 13, 2008

Haroon Rafique commented

I have edited the forum posting to note that there is indeed a public constructor at the MyBroker class and MyService is obtained using a (factory) method like:

protected MyService getMyService() {
return new MyService()
}

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jan 13, 2008

Juergen Hoeller commented

This is probably due to some semantic mismatch with ASM visiting there...

Can you please provide a minimal test case for this - one that passes for you against 2.5 and fails for you against 2.5.1? I'll make sure to fix this in an upcoming 2.5.2 snapshot as quickly as possible...

Juergen

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jan 14, 2008

Haroon Rafique commented

Find attached a maven project which demonstrates the bug. The pom.xml has a property inside it for spring version. It is currently set to 2.5.1. To see the bug, run:

mvn clean process-classes

To see it work without an error with spring 2.5, edit the pom.xml and change the value of property spring.ver from 2.5.1 to 2.5 and rerun:

mvn clean process-classes

With spring 2.5.1, you will see something like:

[java] org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'cfbs' is defined

basically demonstrating that a bean annotated with @Component was excluded as a candidate bean.

Let me know if you need any further help regarding this issue.

Here is an explanation of the contents of the zip file:

pom.xml (maven project file)
src/main/java/com/acme/spring/CandidateForBeanScanning.java (@Component annotated bean)
src/main/java/com/acme/spring/OuterClassWithNoAnnotation.java (a class containing an inner class)
src/main/java/com/acme/spring/Spring4324Bug.java (driver class)
src/main/resources/context.xml (spring context file)

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jan 14, 2008

Juergen Hoeller commented

It turned out that ClassMetadataReadingVisitor (as used by "context:component-scan") got confused by usage of inner classes (which ASM seems to report as well) as opposed to being defined an inner class... Thanks for reporting this!

This fix should be available in tonight's 2.5.2 snapshot (-368), available from http://static.springframework.org/downloads/nightly/snapshot-download.php?project=SPR
Please give it a try and let me know whether it works for you!

Juergen

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jan 15, 2008

Haroon Rafique commented

verified and fixed. thanks for your quick work.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jan 28, 2008

Cameron Braid commented

I am having the same issue as described here.

Is it possible to have nightly snapshots published to s3 ?

I use maven and would much prefer to use the http://s3.amazonaws.com/maven.springframework.org/snapshot repository instead of manually downloading the nightly build, unzip, and manually install each jar into my local maven repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.