spring将spring-asm重新打包原因
以前就发现通过eclipse上面的m2e插件不能将spring-asm模块的源代码取下来,从search.maven.org中搜索也发现,并没有提供source.jar,观察spring-asm模块具体的类名发现,它们都是来源于ASM这个项目,后来发现SpringSource官方博客上的这篇文章,Juergen Hoeller说他们基于ASM 2.2.3重新打包了spring-asm,包名变成org.springframework.asm.*,为什么呢?我将这些类都用ASM 2.2.3提供的类还原掉,相应依赖spring-asm模块的代码也更改后,测试没有任何问题。我当时想,Spring是觉得把别人的代码放到自己项目里,感到不好意思,才不提供源代码的吧。
但今天的测试让我发现,spring这样改还是有原因的。在Spring 3特别是3.1版本发布后,java-based的配置得到了极大的增强,而基于注释的这种配置(@Configuration)需要依赖cglib,而cglib依赖asm,于是问题来了,前文提到spring-asm基于asm-2.2.3,这个版本比较古老,而像cglib-2.2则依赖asm-3.1,测试时就发现有些类中的方法找不到。类似:
13:00:21,854 INFO main support.ClassPathXmlApplicationContext:508 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@65faba46: startup date [Tue Mar 06 13:00:21 CST 2012]; root of context hierarchy
13:00:21,917 INFO main xml.XmlBeanDefinitionReader:317 - Loading XML bean definitions from class path resource [prospringintegration/ticket-receiver.xml]
13:00:22,207 INFO main annotation.ClassPathBeanDefinitionScanner:209 - JSR-330 'javax.inject.Named' annotation found and supported for component scanning
Exception in thread "main" java.lang.IllegalStateException: Cannot load configuration class: com.apress.prospringintegration.messaging.rabbitmq.jms.adapter.RabbitmqConfiguration
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:313)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:197)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:696)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:635)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:141)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.apress.prospringintegration.messaging.rabbitmq.jms.adapter.TicketReceiverMain.main(TicketReceiverMain.java:26)
Caused by: java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V
at net.sf.cglib.core.DebuggingClassWriter.<init>(DebuggingClassWriter.java:47)
at net.sf.cglib.core.DefaultGeneratorStrategy.getClassWriter(DefaultGeneratorStrategy.java:30)
at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:24)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:144)
at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:116)
at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108)
at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104)
at net.sf.cglib.proxy.Enhancer.<clinit>(Enhancer.java:69)
at org.springframework.context.annotation.ConfigurationClassEnhancer.newEnhancer(ConfigurationClassEnhancer.java:136)
at org.springframework.context.annotation.ConfigurationClassEnhancer.enhance(ConfigurationClassEnhancer.java:109)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:303)
... 7 more
即使我们在classpath下也加入了asm-3.1的依赖,但是在classloader时,先找到的是我们改写的同名类,而不是asm-3.1的jar包中的类,如果是spring自己的重新打包的就没有此问题。
因此SpringSource是考虑到spring-asm给spring-core提供服务的独立性,及容易与asm类名冲突的原因才重新打包的。
另:使用@Configuration时,需要使用javax.inject-1.jar包。
再另:spring中依赖的cglib对应的maven实际上是cglib-nodep,如果我直接依赖cglib的话,org.springframework.scripting.support.ScriptFactoryPostProcessor需要import的net.sf.cglib.asm.Type就会找不到。
再再另:哪些类使用asm?
org.springframework.core.type.classreading.SimpleMetadataReader
org.springframework.scritpting.support.ScriptFactoryPostProcessor
--转自
该贴由koei123转至本版2015-6-2 8:56:15