I am creating a bean with annotations.
#Component
public class MyClass
{
#Autowired
private ArrayList<String> myFriends= new ArrayList<String>();
//Getters and setters
}
I am getting the following exception
Could not autowire field: private java.util.ArrayList com.mypackage.MyClass.myFriends; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.util.ArrayList] 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)}
I also tried with this
#Resource
private ArrayList<String> myFriends= new ArrayList<String>();
I am getting the following exception
No matching bean of type [java.util.ArrayList] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER)}
Please let me know how to fix this.
In the XML file you would need to define a list.
In the XML file include the util namespace and add the following bean definition.
<util:list id="myFriends">
<value>string1</value>
<value>string2</value>
<value>string3</value>
</util:list>
You need to change the type of the variable to List<String> instead of ArrayList<String>. That will make it easier to inject and is also a better coding practice to follow. You need to add a Qualifier annotation to specify the id of the bean that needs to be injected, qualifier might not be required if you have only one such list.
#Component
public class MyClass {
#Autowired
#Qualifier("myFriends")
private List<String> myFriends= new ArrayList<String>();
//Getters and setters
}
Link to spring reference documentation for util:list
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/xsd-config.html#xsd-config-body-schemas-util-list
Related
I am using mapstruct to transform a DTO into an object and vice versus and I am getting the following exception:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.rppjs.customer.online.portal.dtos.mapper.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1506)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:819)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:725)
I can see that UserMapper.impl is being generated but still the above exception. My code is on github on this branch 42_RenameCodeBaseToCustomerOnlinePortal. The code is pretty simple and not many lines of code. The exception is generated as part of the RegistrationEndpointIT.java.
Please could you take a look where I am going wrong? It is using a gradle wrapper.
Additionally, I get the following exception when running Application.java:
Description:
Parameter 0 of constructor in
com.rppjs.customer.online.portal.endpoints.RegistrationEndpoint
required a bean of type
'com.rppjs.customer.online.portal.dtos.mapper.UserMapper' that could
not be found.
Action:
Consider defining a bean of type
'com.rppjs.customer.online.portal.dtos.mapper.UserMapper' in your
configuration.
Please note, Application.java is a Spring boot application.
The problem is that RegistrationEndpoint uses the mapper as constructor argument. Since it is a component Spring wants to autowire it. But neither UserMapper nor UserMapperImpl are spring beans, therefore the exceptions.
You have two options:
Remove the UserMapper constructor argument and get your mapper with Mappers.getMapper(UserMapper.class). Best practive would be to also public MAPPER instance inside your mapper (see the example here)
If you need autowired dependencies inside your mapper you can define your mapper as as spring bean as follows:
#Mapper(componentModel = "spring")
#Component
public interface UserMapper() {
//...
}
I have an interface:
public interface IExternalCommandExecutor {
String[] getCommandNames();
void process(String command) throws FailedOperationException;
}
And two implementations(second is similar):
#Service
public class ExecutorImpl implements IExternalCommandExecutor {
//some code...
}
And service, in which I'm using #Autowired annotation on the list of IExternalCommandExecutor:
#Service
public class ExternalCommandsService {
#Autowired
List<IExternalCommandExecutor> commandExecutors;
// other code
}
When I'm trying to run this, I'm getting this error:
NoSuchBeanDefinitionException: No qualifying bean of type
'java.util.List<com.mpcomp.entity.system.IExternalCommandExecutor>' available:
expected at least 1 bean which qualifies as autowire candidate
Which is weird behavior for Spring. As it was mentioned in this article and in the javadoc, Spring should try to autowire all elements of IExternalCommandExecutor instead of trying to find bean of type java.util.List.
If I would change List<IExternalCommandExecutor> to IExternalCommandExecutor[], error is quite similar:
NoSuchBeanDefinitionException: No qualifying bean of type
'com.mpcomp.entity.system.IExternalCommandExecutor[]' available:
expected at least 1 bean which qualifies as autowire candidate
I'm using Spring of version 4.3.8 RELEASE. Any help will be appreciated!
Edit
Interface and service are in the core module, and implementations of interface are in 'children' module which is depends on core module. When I moved service to this 'children' module, everything works fine!
Following https://developer.atlassian.com/bamboodev/bamboo-tasks-api/executing-external-processes-using-processservice I would like to invoke some command using ProcessService bean. The injection as described in the link, does not work.
I checked the source of several other plugins at Bitbucket, but each is using the concept as described in the link.
My class:
import com.atlassian.bamboo.process.ProcessService;
public class CheckTask implements TaskType {
private final ProcessService processService;
public CheckTask(#NotNull final ProcessService processService) {
this.processService = processService;
}
However Bamboo does not find the ProcessService bean and fail with following:
(org.springframework.beans.factory.UnsatisfiedDependencyException :
Error creating bean with name 'bamboo.tasks.CheckTask': Unsatisfied
dependency expressed through constructor argument with index 0 of type
[com.atlassian.bamboo.process.ProcessService]: : No qualifying bean of
type [com.atlassian.bamboo.process.ProcessService] found for
dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations: {}; nested
exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type [com.atlassian.bamboo.process.ProcessService]
found for dependency: expected at least 1 bean which qualifies as
autowire candidate for this dependency. Dependency annotations: {})
Am I missing something ?
Bamboo version: 5.13.0
AMPS version: 6.2.6
The solution in the end was quite simple, no oficial docs discuss the solution though. Hope this helps you a bit.
Finally thanks to this post I made it work: https://answers.atlassian.com/questions/33141765/testcollationservice-not-injected-into-tasktype-constructor-on-sdk-bamboo
import com.atlassian.bamboo.process.ProcessService;
import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
#Scanned
public class CheckTask implements TaskType {
#ComponentImport
private final ProcessService processService;
public CheckTask(#NotNull final ProcessService processService) {
this.processService = processService;
}
The rest of the project was basicaly default, as generated by atlas-create-bamboo-plugin.
Try to add in your atlassian-plugin.xml next line
<component-import key="processService"
interface="com.atlassian.bamboo.process.ProcessService"/>
That should help you
Trying to create a map bean with prototype scope in config class
#Configuration
public class SpringConfig {
public SpringConfig() {
}
#Bean
#Scope("prototype")
public Map<String, Composite> getCompositesMap() {
return new LinkedHashMap<String, Composite>();
}
}
But spring complains
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.eclipse.swt.widgets.Composite] found for dependency [map with value type org.eclipse.swt.widgets.Composite]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER, lookup=)}
How does one define a prototype map bean using annotations only (no xml)?
The error is occurring because Spring is trying to inject Composite into your method, but there is no such bean that corresponds to that class.
You can add a prototype scoped bean inside your SpringConfig class - see here.
I'm trying to run a web app and I'm having some issues. Basically I have a controller and a process and they both share a queue.
The controller manages the files that are uploaded to the server and it puts them in the queue. In the other side, the process takes the files in the queue and uses them for other things.
I've defined the queue as a LinkedBlockingQueue and the annotation #Resource on both of them, but when I run the app, the following exception appears:
Error creating bean with name 'csvQueueConsumerBean': Injection of resource
dependencies failed; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [java.util.concurrent.LinkedBlockingQueue] found for
dependency: expected at least 1 bean which qualifies as autowire candidate for this
dependency.
The code of both clases is the following:
#RestController
#RequestMapping("/upload")
public class FileUploadControllerW {
#Resource
protected LinkedBlockingQueue<QueueObject> csvQueue;
...
}
#Component
public class CsvQueueConsumerBean{
#Resource
protected LinkedBlockingQueue<QueueObject> csvQueue;
...
}
Just for the record, both classes are not on the same package.
The reason for this is because Spring context cannot wire the Bean called
csvQueueConsumerBean
You will need to initialize its LinkedBlockingQueue dependency in the Spring config file like this:
#Bean
public LinkedBlockingQueue<QueueObject> linkedBlockingQueue(){
LinkedBlockingQueue<QueueObject> blockingQueue = new LinkedBlockingQueue<QueueObject>();
// do what you need here...
return blockingQueue;
}