@Component
public class BankServicesImpl implements BankServices {
@Autowired
private DataRepoImpl db;
}
----------------------------------------------------------------------------
@Component
public class DataRepoImpl implements DataRepo{
private Map<Integer, Account> repo = new HashMap<Integer,Account>();
}
----------------------------------------------------------------------------
@Component
public class Account {
private Integer accountID;
private int balance;
}
The codes do the job, the repo HashMap object is created ( {} ). Yet i am trying to get the repo Map object to be generated by Spring. so i changed the DataRepoImpl into:
@Component
public class DataRepoImpl implements DataRepo{
@Autowired
private Map<Integer, Account> repo;
}
Error:
Nov 08, 2021 11:37:06 PM org.springframework.context.support.AbstractApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataRepoImpl': Unsatisfied dependency expressed through field 'repo'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.Map<java.lang.Integer, com.doubleliu.model.Account>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataRepoImpl': Unsatisfied dependency expressed through field 'repo'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.Map<java.lang.Integer, com.doubleliu.model.Account>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.Map<java.lang.Integer, com.doubleliu.model.Account>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
However, when i refactor accountID and Map key into String type, i able to produce the HashMap containing a 'dummy' variable which is weird:
{account=com.doubleliu.model.Account@397fbdb}
Back to Integer, I couldnt fix the error, I then tried to move the Autowired to the empty constructor of the DataRepoImpl class:
@Autowired
public DataRepoImpl() {
}
However i get null value from repo, since (my assumption) the object hasnt been created or assigned to the repo Map variable. Again, i then move the Autowired onto the constructor with map as the parameter:
@Autowired
public DataRepoImpl(Map<Integer, Account> repo) {
this.repo = repo;
}
This time the repo object is created ( {} ) and not null, i assume that the object is created through the parameter, is this the right way to create it utilizing spring? also my IDE flag me with an error of
Could not autowire. No beans of 'Map<Integer, Account>' type found.
but i can still compile it. i wonder how to fix it.. fyi, I am using IntelliJ IDEA
I just started learning spring for a bit, trying to completely understand before i go to the next level. so any response, solutions, comments, advice to the code, fix, and issue is very pleased especially with the autowired. Thank you.
EDIT: i am using xml annotation based configuration