3

这可能是微不足道的,但我似乎不断地在墙上跑,找不到我的错误。

我正在扩展使用 Spring 和 OpenJPA 的 Apache James 3 邮件服务器。我现在想通过 JMX 访问使用 JPA 存储库的服务类。

该服务是

@Component
@ManagedResource(objectName = "bean:name=BlacklistServiceImpl", description = "The service providing access to the blacklisted recipients in the DB.")
public class BlacklistServiceImpl implements BlacklistService {

    @Autowired
    private BlackListRepository blackListRepository;
    @Autowired
    private ConversationService conversationService;

    @Override
    @ManagedOperation(description = "Add address to blacklist")
    @ManagedOperationParameter(name = "address", description = "Address to add to blacklist.")
    public void addAddress(String address) {
        //Stuff
    }

    @Override
    @ManagedOperation(description = "Remove address from blacklist")
    @ManagedOperationParameter(name = "address", description = "Address to remove from blacklist.")
    public void removeAddress(String address) {
        //Stuff
    }
}

我在我的上下文中添加了以下内容:

<bean id="mailserverBeansExporter" class="org.springframework.jmx.export.annotation.AnnotationMBeanExporter">
    <property name="beans">
        <map>
            <entry key="bean:name=BlacklistServiceImpl">
                <bean class="<snip>.services.blacklist.impl.BlacklistServiceImpl"/>
            </entry>
        </map>
    </property>
</bean>

虽然我设法构建了这个,但 James 在启动时崩溃了,但出现以下异常:

    [INFO] Starting Apache James Server...
    [INFO] ------------------------------------------------------------------------
    [ERROR] FATAL ERROR
    [INFO] ------------------------------------------------------------------------
    [INFO] Error creating bean with name 'blacklistServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.B
    eanCreationException: Could not autowire field: private <snip>.core.repositories.blacklist.BlackListRepository <snip>.service
    s.blacklist.impl.BlacklistServiceImpl.blackListRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean
     of type [<snip>.repositories.blacklist.BlackListRepository] found for dependency: expected at least 1 bean which qualifies as autowire cand
    idate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    [INFO] ------------------------------------------------------------------------
    [INFO] Trace
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'blacklistServiceImpl': Injection of autowired dependencies failed; neste
    d exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private <snip>.repositories.blacklist.Blac
    kListRepository <snip>.services.blacklist.impl.BlacklistServiceImpl.blackListRepository; nested exception is org.springframework.beans.facto
    ry.NoSuchBeanDefinitionException: No matching bean of type [<snip>.repositories.blacklist.BlackListRepository] found for dependency: expecte
    d at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(re
    quired=true)}
            at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java
    :285)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
            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: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.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
            at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
            at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
            at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
            at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
            at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
            at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
            at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:556)
            at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:535)
            at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
            at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
            at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
            at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
            at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
            at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
            at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            at java.lang.reflect.Method.invoke(Method.java:597)
            at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
            at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
            at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
            at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private <snip>.repositories.blacklist.BlackLis
    tRepository <snip>.services.blacklist.impl.BlacklistServiceImpl.blackListRepository; nested exception is org.springframework.beans.factory.N
    oSuchBeanDefinitionException: No matching bean of type [<snip>.repositories.blacklist.BlackListRepository] found for dependency: expected at
     least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(requir
    ed=true)}
            at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.j
    ava:502)
            at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
            at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java
    :282)
            ... 33 more
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [<snip>.repositories.blacklist.BlackLis
    tRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframe
    work.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.j
    ava:474)
            ... 35 more

BlackListRepository 有两个实现:JPABlackListRepository 和 InMemoryBlackListRepository,用于单元测试。我试图通过添加来解决问题

<bean id="BlackListRepository" class="<snip>.JPABlackListRepository" />

到通用上下文,但这导致单元测试失败,因为现在不再使用内存实现。

我将不胜感激任何帮助。

更新:

在调试时(感谢下面的评论),我意识到只删除 @Component 和 @Managed* 注释以及该部分

<bean id="mailserverBeansExporter" class="org.springframework.jmx.export.annotation.AnnotationMBeanExporter">
    <property name="beans">
        <map>
            <entry key="bean:name=BlacklistServiceImpl">
                <bean class="at.willhaben.safe.sms.core.services.blacklist.impl.BlacklistServiceImpl"/>
            </entry>
        </map>
    </property>
</bean>

导致干净的启动。显然 JMX 不起作用,但是在代码中留下任何这些语句都会导致上述异常。

4

1 回答 1

2

就像其他评论指出的那样,该异常与 JMX 并没有真正的关系——问题似乎是没有找到依赖项。

您已经提到,当您以这种方式显式传入存储库时:

<bean id="BlackListRepository" class="<snip>.JPABlackListRepository" />

然后它可以工作,如果是这种情况,您可以有选择地单独覆盖它进行测试,创建一个测试应用程序上下文:

test-applicationcontext.xml

 <import resource="prod-context.xml"/>
 <bean id="BlackListRepository" class="..InMemoryBlackListRepository"/>

这将确保 InMemoryBlackLIstRespository 是注入的。

还要通过 Jmx 公开您的服务 bean:

<bean name="blacklistService" class="..BlackListServiceImpl"/>

<bean id="mailserverBeansExporter" class="org.springframework.jmx.export.annotation.AnnotationMBeanExporter">
<property name="beans">
    <map>
        <entry key="bean:name=BlacklistServiceImpl" value-ref="blacklistService">

        </entry>
    </map>
</property>

于 2012-08-27T11:59:07.427 回答