me0wster / javamelody

Automatically exported from code.google.com/p/javamelody
0 stars 0 forks source link

Problem when mixing monitoring-spring.xml with use of Spring aop namespace and autowiring. #172

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. add classpath:net/bull/javamelody/monitoring-spring.xml to web.xml
2. Create advisor for a bean X (eg. for transactional advice)
3. Try to make X @Autowired in some bean Y.

What is the expected output? What do you see instead?
Without 1. the application starts, with it there is an exception:
org.springframework.beans.factory.BeanCreationException: Error creating bean 
with name '<Y>': Injection of autowired dependencies failed; nested exception 
is org.springframework.beans.factory.BeanCreationException: Could not autowire 
field: private <class of X> <field name>; nested exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching 
bean of type [<class of X>] found for dependency: expected at least 1 bean 
which qualifies as autowire candidate for this dependency. Dependency 
annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
[...cut...]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No 
matching bean of type [<class of X>] found for dependency: expected at least 1 
bean which qualifies as autowire candidate for this dependency. Dependency 
annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:920)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:789)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)
    ... 23 more

What version of the product are you using? On what application server, JDK,
operating system?
JavaMelody 1.33.0, Spring 3.0.5

Please provide any additional information below.

X is instance of a class not implementing any interfaces. This may make things 
worse, I guess.
When looking at the situation in a debugger:
The working case (without monitoring-spring.xml) has X created as a CGLIB proxy 
that is an instance of the class of X.
With monitoring-spring.xml a CGLIB proxy is wrapped in a 
java.lang.reflect.Proxy, which obviously is not an instance of the class of X.

Original issue reported on code.google.com by m...@meep.pl on 9 Jan 2012 at 4:54

GoogleCodeExporter commented 9 years ago
The workaround here is to use only the springDataSourceBeanPostProcessor part 
of monitoring-spring.xml.
Maybe it would be OK to provide not only the one-stop config in 
monitoring-spring.xml but also separate files for its separate parts (or 
documentation for people needing only some of these parts and having problems 
with others).

Original comment by m...@meep.pl on 9 Jan 2012 at 4:57

GoogleCodeExporter commented 9 years ago
Instead of using only the springDataSourceBeanPostProcessor part of 
monitoring-spring.xml, is this issue fixed by just using 
monitoring-spring-aspectj.xml, which is also included in javamelody jar file?

In other words, is it fixed by adding 
classpath:net/bull/javamelody/monitoring-spring-aspectj.xml to web.xml (and not 
monitoring-spring.xml) ?

Original comment by evernat@free.fr on 11 Jan 2012 at 9:09

GoogleCodeExporter commented 9 years ago
To myself: if yes, I may add a note at the end of the JDBC chapter of the user 
guide for this.

Original comment by evernat@free.fr on 11 Jan 2012 at 9:17

GoogleCodeExporter commented 9 years ago
Unfortunately monitoring-spring-aspectj.xml brings another problem here 
(because of <aop:config proxy-target-class="true"> is my first guess):

Caused by: org.springframework.beans.factory.BeanCreationException: Error 
creating bean with name '<X>' defined in class path resource [<Y>]: 
Initialization of bean failed; nested exception is 
org.springframework.aop.framework.AopConfigException: Could not generate CGLIB 
subclass of class [<Z>]: Common causes of this problem include using a final 
class or a non-visible class; nested exception is 
java.lang.IllegalArgumentException: Superclass has no null constructors but no 
arguments were given
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:844)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:786)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)
    ... 44 more
Caused by: org.springframework.aop.framework.AopConfigException: Could not 
generate CGLIB subclass of class [<Z>]: Common causes of this problem include 
using a final class or a non-visible class; nested exception is 
java.lang.IllegalArgumentException: Superclass has no null constructors but no 
arguments were given
    at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:212)
    at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:112)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:476)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:362)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:322)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:407)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1426)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    ... 53 more
Caused by: java.lang.IllegalArgumentException: Superclass has no null 
constructors but no arguments were given
    at net.sf.cglib.proxy.Enhancer.emitConstructors(Enhancer.java:721)
    at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:499)
    at net.sf.cglib.transform.TransformingClassGenerator.generateClass(TransformingClassGenerator.java:33)
    at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
    at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
    at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:285)
    at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:200)
    ... 60 more

Original comment by m...@meep.pl on 16 Jan 2012 at 11:38

GoogleCodeExporter commented 9 years ago
So I have added the monitoring-spring-datasource.xml file (revision 2617) as an 
alternative to the all-in-one monitoring-spring.xml file.

Doc to be added in the user guide:
    If ever, there is a conflict in your application between the all-in-one monitoring-spring.xml and AOP or @Autowired,
    then you can use the monitoring-spring-datasource.xml file, instead of monitoring-spring.xml.
    This file contains only the datasource post-processor and an example of SpringDataSourceFactoryBean.

Thanks

Original comment by evernat@free.fr on 11 Mar 2012 at 6:09

GoogleCodeExporter commented 9 years ago
Isn't this not really a "fix", but a workaround?  I would like to still be able 
to use the @MonitoredWithSpring annotation, but am unable to use it because of 
this issue with Autowiring.  While it is nice to have the datasource monitoring 
now, it seems to me that the underlying issue still remains.

Original comment by kevhen...@gmail.com on 21 Mar 2012 at 2:49

GoogleCodeExporter commented 9 years ago
@kevhender
Certainly.
Do you have a patch in mind? an alternative monitoring-spring-x.xml file?

Original comment by evernat@free.fr on 21 Mar 2012 at 5:13

GoogleCodeExporter commented 9 years ago
I had not really thought about how to patch... I certainly didn't intend to 
sound rude, so I hope that it didn't come across that way.  My only intention 
was to try to ensure that this issue doesn't get lost since it is marked as 
"Fixed," but technically isn't fixed yet.  Thanks.

Original comment by kevhen...@gmail.com on 22 Mar 2012 at 1:17