Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Spring tutorial - tutorialspoint Injecting Bean references WTF

Options
  • 23-03-2020 6:38pm
    #1
    Registered Users Posts: 1,551 ✭✭✭


    I'm doing tutorials to try and get back into programming and hopefully get a job in programming some day.
    Anyway I've been looking at tutorialspoint.com and this particular page here.
    https://www.tutorialspoint.com/spring/spring_injecting_collection.htm

    I'm stuck on Injecting Bean References.
    I was trying to recreate something for that but seem to be getting nowhere.
    You see it doesn't explain the programming code and only gives you some pseudo code xml basically and doesn't explain how to get you to do it.

    It basically wants you to create a collection of Addresses.
    So I tried creating an Address class and making a Bean out of it and creating a list that would accept Address objects and it does but when I try and print it, it prints the actual object not the string which is gobbledygook.
    I'll post the code I have.

    setAddressList2 in JavaCollection.java, Address.java, jc.getAddressList2() in MainApp.java, and the property called addressList2 and the bean called address1 in Beans.xml is the relevant code that I'm having trouble with.
    Hope someone can help and has some kind of idea what I'm on about.

    Here's the code:

    JavaCollection.java
    package com.tutorialspoint;
    import java.util.*;
    
    public class JavaCollection {
       List addressList;
       Set  addressSet;
       Map  addressMap;
       Properties addressProp;
       List  addressList2;
    
       // a setter method to set List
       public void setAddressList(List addressList) {
          this.addressList = addressList;
       }
       
       // prints and returns all the elements of the list.
       public List getAddressList() {
          System.out.println("List Elements :"  + addressList);
          return addressList;
       }
       
    // a setter method to set List of Addresses
       public void setAddressList2(List  addressList2) {
          this.addressList2 = addressList2;
       }
       
       // prints and returns all the elements of the list.
       public List getAddressList2() {
          System.out.println("List Elements :"  + addressList2);
          return addressList2;
       }
       
       
       // a setter method to set Set
       public void setAddressSet(Set addressSet) {
          this.addressSet = addressSet;
       }
       
       // prints and returns all the elements of the Set.
       public Set getAddressSet() {
          System.out.println("Set Elements :"  + addressSet);
          return addressSet;
       }
       
       // a setter method to set Map
       public void setAddressMap(Map addressMap) {
          this.addressMap = addressMap;
       }
       
       // prints and returns all the elements of the Map.
       public Map getAddressMap() {
          System.out.println("Map Elements :"  + addressMap);
          return addressMap;
       }
       
       // a setter method to set Property
       public void setAddressProp(Properties addressProp) {
          this.addressProp = addressProp;
       }
       
       // prints and returns all the elements of the Property.
       public Properties getAddressProp() {
          System.out.println("Property Elements :"  + addressProp);
          return addressProp;
       }
    }
    

    MainApp.java
    package com.tutorialspoint;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MainApp {
       public static void main(String[] args) {
          ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
          JavaCollection jc=(JavaCollection)context.getBean("javaCollection");
    
          jc.getAddressList();
          jc.getAddressList2();
          jc.getAddressSet();
          jc.getAddressMap();
          jc.getAddressProp();
       }
    }
    

    Beans.xml
    <?xml version = "1.0" encoding = "UTF-8"?>
    
    <beans xmlns = "http://www.springframework.org/schema/beans"
       xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation = "http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
     
       <!-- Definition for javaCollection -->
       <bean id = "javaCollection" class = "com.tutorialspoint.JavaCollection">
          
          <!-- results in a setAddressList(java.util.List) call -->
          <property name = "addressList">
             <list>
                <value>INDIA</value>
                <value>Pakistan</value>
                <value>USA</value>
                <value>USA</value>
             </list>
          </property>
         
          <!-- results in a setAddressList(java.util.List) of Addresses call -->
          <property name = "addressList2">
             <list>
                 <ref bean = "address1"/>
                 <value>INDIA</value>
                 <value>Pakistan</value>
             </list>
          </property>
    
          <!-- results in a setAddressSet(java.util.Set) call -->
          <property name = "addressSet">
             <set>
                <value>INDIA</value>
                <value>Pakistan</value>
                <value>USA</value>
                <value>USA</value>
             </set>
          </property>
    
          <!-- results in a setAddressMap(java.util.Map) call -->
          <property name = "addressMap">
             <map>
                <entry key = "1" value = "INDIA"/>
                <entry key = "2" value = "Pakistan"/>
                <entry key = "3" value = "USA"/>
                <entry key = "4" value = "USA"/>
             </map>
          </property>
          
          <!-- results in a setAddressProp(java.util.Properties) call -->
          <property name = "addressProp">
             <props>
                <prop key = "one">INDIA</prop>
                <prop key = "one">INDIA</prop>
                <prop key = "two">Pakistan</prop>
                <prop key = "three">USA</prop>
                <prop key = "four">USA</prop>
             </props>
          </property>
       </bean>
       
       <bean id = "address1" class = "com.tutorialspoint.Address"></bean>
    	 
    	
    	
    </beans>
    

    Address.java
    package com.tutorialspoint;
    
    public class Address {
    	private String address;
    	
    	public String getAddress()
    	{
    		return address;
    	}
    	
    	public void setAddress(String address)
    	{
    		
    		this.address = "hyde park";
    		System.out.println("Reference type list "+ this.address);
    	}
    }
    

    Oh yikes I can see what I'm trying to do in Address.java makes no sense I'm just trying to get it to print out something other than gobbledygook.
    Anyway please help me if you can somebody.


Comments

  • Registered Users Posts: 6,259 ✭✭✭Buford T Justice


    Why are you bothering with antiquated configurations like xml? Spring has long since moved to annotation based injection and is much cleaner to use.

    Unless you have a very specific use case don't waste your time this. Better spent learning the anontations


  • Registered Users Posts: 27,163 ✭✭✭✭GreeBo


    ^ times a million
    its all @Annotations these days (and for a couple of years tbh!)


    @Override toString() method in your address class and you should get what you want.
    Right now its printing out the object references within your List as you havent told it how to deal with a toString call on your Address class.


  • Registered Users Posts: 1,551 ✭✭✭quinnd6


    Good man GreeBo that's exactly right.
    I actually just saw it somewhere else aswell yesterday after and that works very well.
    I found that tutorial kind of confusing because it doesn't explain things very well at that point at all.
    I presume the rest of the code I came up with is ok.
    I was going to use some generics but I don't think that's necessary anyway as it works fine without that.
    Yep the reason I was doing XML was because I thought it's no harm to know both really.


  • Registered Users Posts: 6,259 ✭✭✭Buford T Justice


    quinnd6 wrote: »
    Yep the reason I was doing XML was because I thought it's no harm to know both really.

    I thought that too, but in my last two + years since moving to Spring I've never once had to look at xml, including dealing with legacy code


  • Registered Users Posts: 27,163 ✭✭✭✭GreeBo


    quinnd6 wrote: »
    Good man GreeBo that's exactly right.
    I actually just saw it somewhere else aswell yesterday after and that works very well.
    I found that tutorial kind of confusing because it doesn't explain things very well at that point at all.
    I presume the rest of the code I came up with is ok.
    I was going to use some generics but I don't think that's necessary anyway as it works fine without that.
    Yep the reason I was doing XML was because I thought it's no harm to know both really.

    To be fair to the tutorial, overriding toString is nothing to do with Spring, its basic OO.

    What were you going to do with generics?


  • Advertisement
Advertisement