Reputation: 193
This may be trivial, but I seem to continuously run at a wall and can't find my mistake.
I am extending the Apache James 3 mailserver which used Spring and OpenJPA. I would now like to access a Service class which used a JPA Repository via JMX.
The Service is
@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
}
}
and I have added to following to my context:
<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>
While I have managed to build this, James crashes at startup with the following exception:
[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
The BlackListRepository has two implementations: the JPABlackListRepository and the InMemoryBlackListRepository, which is used for unit testing. I tried to solve the problem by adding
<bean id="BlackListRepository" class="<snip>.JPABlackListRepository" />
to the common-context, but this lead to the unit tests failing as now the in memory implementation wasn't used anymore.
I would appreciate any help.
Update:
While debugging (and thanks to the comment below) I realized that only removing BOTH the @Component and @Managed* annotations as well as the section
<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>
leads to a clean startup. Obviously then JMX doesn't work, but leaving any of those statements in the code leads to above exception.
Upvotes: 3
Views: 1780
Reputation: 49935
Like the other comments have noted, the exception is not really related to JMX then - the issue seems to be that a dependency is not being found.
You have mentioned that when you explicitly pass in a repository this way:
<bean id="BlackListRepository" class="<snip>.JPABlackListRepository" />
then it works, if that is the case, you can selectively override it for tests alone, create a test application context:
test-applicationcontext.xml
<import resource="prod-context.xml"/>
<bean id="BlackListRepository" class="..InMemoryBlackListRepository"/>
This will ensure that InMemoryBlackLIstRespository is the one injected.
Also to expose your service bean over Jmx:
<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>
Upvotes: 2