通过JMX使Spring Service类可管理

时间:2012-08-27 08:40:53

标签: java spring jmx

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

我正在扩展使用Spring和OpenJPA的Apache James 3邮件服务器。我现在想要访问一个通过JMX使用JPA Repository的Service类。

服务是

@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不起作用,但在代码中留下任何这些语句都会导致上述异常。

1 个答案:

答案 0 :(得分: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>