bean的初始化失败;嵌套异常是org.springframework.beans.ConversionNotSupportedException

时间:2016-01-18 09:56:18

标签: java spring aspectj

我写的日志机制不起作用。我用:

  • aspectjrt 1.7.3
  • aspectjweaver 1.7.3
  • spring 3.0.6

我对bean使用XML spring定义。 someModuleTarget是一个无法初始化的bean。我的应用程序中的一些bean被提供给org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator,其中interceptorNames具有不同的事务传播(REQUIRE,NEW,ETC)

在应用程序部署期间出现问题,但仅当我在某些方法中使用新注释@TimeLog时才会出现问题。下面是一些代码。

注释记录机制:

@Aspect
public class TimeLogger {
    @Around("execution(* *(..)) && @annotation(mypackage.TimeLog)")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        Method method = MethodSignature.class.cast(point.getSignature()).getMethod(); 
        //Why this returns TRUE only when I annotate both API and Impl of myAnnotatedComponent?
        method.isAnnotationPresent(LogExecutionTime.class); 
        Object result = point.proceed();
        LOG.info("some info");
        return result;
    }
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface TimeLog {
    LogLevels logLevel() default LogLevels.DEBUG;
}

Spring AOP配置:

<aop:aspectj-autoproxy/>

<bean id="execTimeLogger" class="mypackage2.TimeLogger">
</bean>

这是我得到的例外:

org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:352)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:153)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1325)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
    at myPackage.someComponent.afterPropertiesSet(ActionExecutor.java:184)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:283)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:282)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:204)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    at myPackage.CustomLoaderListener.contextInitialized(ExionContextLoaderListener.java:20)
    at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:775)
    at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:424)
    at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:767)
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:249)
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1250)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:710)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:492)
    at org.mortbay.jetty.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:293)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:229)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:172)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:229)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95)
    at org.eclipse.jetty.server.Server.doStart(Server.java:280)
    at org.mortbay.jetty.plugin.JettyServer.doStart(JettyServer.java:65)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at org.mortbay.jetty.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:520)
    at org.mortbay.jetty.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:365)
    at org.mortbay.jetty.plugin.JettyRunMojo.execute(JettyRunMojo.java:523)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:133)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:108)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:76)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:116)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:361)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:213)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:157)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)

Caused by: org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'someModuleTarget' defined in some.xml
    Initialization of bean failed; nested exception is
        org.springframework.beans.ConversionNotSupportedException:
        Failed to convert property value of type 'com.sun.proxy.$Proxy191 implementing
        mypackage.commons.LocaleConfigurationAware,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised'
        to required type 'mypackage.core.MyAnnotatedComponent' for property 'myAnnotatedComponent';
        nested exception is
            java.lang.IllegalStateException: Cannot convert value of type
            [com.sun.proxy.$Proxy191 implementing mypackage.commons.LocaleConfigurationAware
            org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised]
            to required type [mypackage.core.MyAnnotatedComponent] for property
            'myAnnotatedComponent': no matching editors or conversion strategy found

我发布了如何注入带注释的组件,作为一些注释的请求:

public class MyModuleImpl implements MyModuleApi {
    private MyAnnotatedComponentApi myAnnotatedComponent;
    //I also tried MyAnnotatedComponentImpl myAnnotatedComponent;

    public MyAnnotatedComponentApi getMyAnnotatedComponent() {
        return myAnnotatedComponent;
    }

    public void setMyAnnotatedComponent(MyAnnotatedComponentApi myAnnotatedComponent) {
        this.myAnnotatedComponent = myAnnotatedComponent;
    }
}

Spring bean定义:

<bean id="myModule" class="myPackage.MyModuleImpl" lazy-init="false">
<property name="myAnnotatedComponent" ref="myAnnotatedComponent"/>

<bean id="myAnnotatedComponent" class="myPackage.MyAnnotatedComponentImpl"

0 个答案:

没有答案