0
@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

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
DoubleLiu
  • 52
  • 1
  • 6
  • Here's what you need https://stackoverflow.com/a/31584390/2599899 – Arthur Klezovich Nov 08 '21 at 17:31
  • XML and Java stuff is equivalent. For this Java config you can create an equivalent XML config – Arthur Klezovich Nov 08 '21 at 17:42
  • Unrelated, but one important point to think about: From your example, i think the Account itself should not be a Spring bean, but rather an entity (which you usually would save in a database). – dunni Nov 08 '21 at 17:51
  • @dunni hey there, i am not really sure but Account is actually a java pojo class for oop. i also refer it to the tutorials in youtube.. am i missing something? – DoubleLiu Nov 08 '21 at 18:20
  • Program to interfaces not classes instead of `DataRepoImpl` you should inject `DataRepo`. Regarding your `Map` you need to define a bean of that type, spring will not automatically create beans for `@Autowired` fields they need to be present int he application context. But injecting a map here doesn't add much. – M. Deinum Nov 08 '21 at 19:35
  • @M.Deinum Hey, thank you for your advice! i truly appreciate it! i just found out that i need to define the map variable itself with the key and value type in the xml. thanks again! – DoubleLiu Nov 08 '21 at 20:14

1 Answers1

1

Related to here

I just found out that i need to define the properties of the defined bean in the xml. everytime i define something like HashMap, add following to the xml and it enables me to inject the just defined Map bean by defining @Resource on the Map variable inside the class.

<util:map id="repo" scope="prototype" map-class="java.util.HashMap"
                  key-type="java.lang.String" value-type="com.doubleliu.model.Account"/>

Thank you for the help and comments, any more comments and advices are welcomed.

DoubleLiu
  • 52
  • 1
  • 6