Dependency injection with Spring not working when using nested beans - spring

I'm trying to use Spring and have a trouble using it for a case.
I have the following code:
<util:map id="someMap" value-type="java.util.Set">
<entry key="a" value-ref="setA"/>
<entry key="b" value-ref="setB"/>
</util:map>
<util:set id="setA">
<value>A</value>
</util:set>
<util:set id="setB">
<value>B</value>
</util:set>
with the following Java code (using #Qualifier to get "someMap"):
package a.b.c;
public class SomeClass {
private final Map<String, Set<String>> someMap;
#Autowired
public SomeClass(#Qualifier("someMap") final Map<String, Set<String>> someMap) {
this.someMap = someMap;
}
}
and it's giving me the following error:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'a.b.c.SomeClass': Unsatisfied dependency expressed through constructor argument with index 2 of type [java.util.Map]: : No matching bean of type [java.util.Set] found for dependency [map with value type java.util.Set]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:{#org.springframework.beans.factory.annotation.Qualifier(value=someMap)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.util.Set] found for dependency [map with value type java.util.Set]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Qualifier(value=someMap)}
Looks like it's having trouble finding the Set but I don't know why it's happening. How would you solve this problem?

I found how to fix this issue. I had to use #Value("#{#someMap}") instead of #Qualifier("someMap").

I hope you are using spring 4.3 or above version
Try with adding mapclass,key-type
<util:map id="AdditionalParams" map-class="java.util.HashMap"
key-type="java.lang.String" value-type="java.lang.String">

Related

No qualifying bean of type 'org.springframework.data.redis.support.atomic.RedisAtomicLong' available: expected at least 1 bean as autowire candidate

I have a common library and common library is using redis jar:
package com.service.dao.impl;
RedisImpl.class
#Profile
#Log
#Named
public class RedisImpl implements RedisDao {
#Autowired
#Qualifier("ECID")
private RedisAtomicLong ECID;
// this field will be errored out
}
after I create a common jar and importing this common library in another project:
I get an error as:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type'org.springframework.data.redis.support.atomic.RedisAtomicLong'
available: expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true), #org.springframework.beans.factory.annotation.Qualifier(value=ECID)}
I tried several options with
#EnableRedisRepositories(basePackageClasses = { SequenceNumberRedisDaoImpl.class })
#ComponentScan
no luck.... How do I resolve this issue . How can I autowired the third party library field through commons library?

No qualifying bean of type [com.atlassian.jira.issue.TemporaryAttachmentsMonitorLocator]

I started to develop a Jira External System Importer Plugin and I should upload a JSON File using its Setup Page but when I add "TemporaryAttachmentsMonitorLocator" to my SetupPage class constructor and I click on my importer's button, it fails with the error: "No qualifying bean of type [com.atlassian.jira.issue.TemporaryAttachmentsMonitorLocator]"
[INFO] [talledLocalContainer] org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dev.addax.jira.plugins.trello2jira.web.TrelloSetupPage': Unsatisfied dependency expressed through constructor argument with index 4 of type [com.atlassian.jira.issue.TemporaryAttachmentsMonitorLocator]: No qualifying bean of type [com.atlassian.jira.issue.TemporaryAttachmentsMonitorLocator] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport(value=)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: **No qualifying bean of type [com.atlassian.jira.issue.TemporaryAttachmentsMonitorLocator] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency**. Dependency annotations: {#com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport(value=)}
[INFO] [talledLocalContainer] at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
public class TrelloSetupPage extends AbstractSetupPage {
private final TemporaryAttachmentsMonitorLocator locator;
public TrelloSetupPage(#ComponentImport UsageTrackingService usageTrackingService, #ComponentImport WebInterfaceManager webInterfaceManager,
#ComponentImport PluginAccessor pluginAccessor, #ComponentImport EventPublisher eventPublisher,
#ComponentImport TemporaryAttachmentsMonitorLocator locator) {
super(usageTrackingService, webInterfaceManager, pluginAccessor, eventPublisher);
this.locator = locator;
}
// ... other content omitted.
}
The other params in my constructor are resolved right. Only this one is what it fails. If I remove that class I can navigate to my SetupPage as is expected.
Finally, the class is exported by the OSGi system bundle and I checked it is even still used as a service by the Jira native JIM Plugin.
Any idea what might be the reason why it is not finding this class?
I removed #ComponentImport TemporaryAttachmentsMonitorLocator locator and I included
ComponentAccessor.getOSGiComponentInstanceOfType(TemporaryAttachmentsMonitorLocator.class) in the constructor body.
I don't know why #ComponentImport does not work for this case but the workaround works like a charm.

Spring beans GeoModule bean injection to RepositoryRestMvcConfiguration

I'm currently testing spring data rest, and I want to expose the primary keys (ids) of my entities through the REST interface.
I have found that the proper (?) way to do this is:
public class IdExporterConfiguration extends RepositoryRestMvcConfiguration {
#Override
protected void configureRepositoryRestConfiguration(
RepositoryRestConfiguration config) {
super.configureRepositoryRestConfiguration(config);
config.exposeIdsFor(User.class);
}
}
The problem is, that if I change my bean definition to this:
<bean class="test.project.util.IdExporterConfiguration"/>
From this:
<bean class="org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration"/>
my application fails to start...
The error is:
Could not autowire field: org.springframework.data.geo.GeoModule org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration.geoModule;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.data.geo.GeoModule] 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)}
Basically it says that it does not find a GeoModule bean, so it can't autowire it for the RepositoryRestMvcConfiguration base...
Now the fun part is, that is I define the bean:
<bean class="org.springframework.data.geo.GeoModule"/>
The error changes to:
Could not autowire field: org.springframework.data.geo.GeoModule org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration.geoModule;
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.data.geo.GeoModule] is defined:
expected single matching bean but found 2: jacksonGeoModule,org.springframework.data.geo.GeoModule#0
So if I don't define a bean, there is 0, but if I define one, there is 2?
I still don't know why, but if I use the #Configuration annotation, then it works...
No GeoModule bean needed, but how can it be, that with the original config as XML bean definition it works, but with the subclass, it does not?

Could not autowire jaxrs client

I have this in my applicationContext.xml (I am using http://cxf.apache.org/jaxrs)
<context:annotation-config />
<context:component-scan base-package="br.com.test" />
<bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" />
<jaxrs:client id="testClient"
serviceClass="br.com.test.ws.InterfaceServiceTest"
address="http://localhost:8080/ocs-teste-ws-web/services/myservice">
<jaxrs:providers>
<ref bean="jsonProvider" />
</jaxrs:providers>
</jaxrs:client>
I am including the jar with interface in this project but I am getting the following exception:
1318 [localhost-startStop-2] ERROR org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someBeanImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: br.com.test.ws.InterfaceServiceTest br.com.test.impl.SomeBeanImpl.interfaceServiceTest; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [br.com.test.ws.InterfaceServiceTest] 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)}
What am I doing wrong?
Thanks.
Don't use #Autowired, use #Resource instead, in the class where you are trying to inject your client.
Because #Autowired and #Inject
1) Matches by Type
2) Restricts by Qualifiers
3) Matches by Name
And #Resource
1) Matches by Name
2) Matches by Type
3) Restricts by Qualifiers (ignored if match is found by name)
In your case you use a proxy, So the class type isn't what you want. With #Resource you start by find by name.

Spring: how to ignore #Autowired property if bean is not defined

Situation: I have I class with property annotated with #Autowired:
public class MyClass {
#Autowired
protected MyAutoWiredBean myAutowiredBean;
}
Is there any possibility to made wiring this bean optional, i.e. if such bean is defined in some configuration file - to wire it, but if such bean is not defined - just continue working without throwing:
org.springframework.beans.factory.BeanCreationException:
Could not autowire field: protected MyAutoWiredBean...;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No matching bean of type [com.mypackage.MyAutoWiredBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
Have you tried:
#Autowired(required=false)
Javadoc:
Declares whether the annotated dependency is required. Defaults to true
you can set required attribute like :
#Autowired(required=false)
http://static.springsource.org/spring/docs/2.5.5/api/org/springframework/beans/factory/annotation/Autowired.html

Resources