Javatpoint Logo

91-9990449935

 0120-4256464

Constructor Injection with Non-String Map (having dependent Object) Example

In this example, we are using map as the answer that have Answer and User. Here, we are using key and value pair both as an object. Answer has its own information such as answerId, answer and postedDate, User has its own information such as userId, username, emailId.

Like previous examples, it is the example of forum where one question can have multiple answers.

Question.java

This class contains three properties, two constructors and displayInfo() method to display the information.

Answer.java
User.java
applicationContext.xml

The key-ref and value-ref attributes of entry element is used to define the reference of bean in the map.

Test.java

This class gets the bean from the applicationContext.xml file and calls the displayInfo() method to display the information.