1) From our textbook reading in the previous exercises…Chapter 2 Section 2.4

Strong Suggestion: Type all of the code versus copy/paste to learn the syntax and structure needed to create these files yourself. Solving any errors will help you learn faster. Read all errors.

Create/Demo the CollectionsProj Project

This is a Spring Framework Java Project. Rename a copy of our Spring43xMavenStarterProj(preferred) to CollectionsProj . Suggested maven artifact-id: spring-collections.

Eclipse Maven based Proj
Spring43xMavenStarterProj.zip

2) Project Name: CollectionsProj

Read the particular topic in the book in Section 2.4 to re-enforce our in-class lecture.

com.springclass.app.MainApp

package com.springclass.app;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.springclass.model.JavaCollection;



public class MainApp {
	private static final Logger log = LoggerFactory.getLogger(MainApp.class);
	public static final String SPRING_BEANS_LOCATION = "Meta-INF/spring/context.xml";
	public static final String MY_BEAN = "javaCollection";
	
	public static void main(String[] args) {
		// Go here to learn more
		// https://docs.spring.io/spring/docs/4.3.18.RELEASE/spring-framework-reference/htmlsingle/#beans-factory-client

		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(SPRING_BEANS_LOCATION);
		log.trace("Loaded my bean definitions from XML.");
		
		JavaCollection jc = (JavaCollection) context.getBean(MY_BEAN);
		log.trace("Retrieved the bean with id " + MY_BEAN + " from " + SPRING_BEANS_LOCATION);
		
		jc.getAddressList();
		jc.getAddressSet();
		jc.getAddressMap();
		jc.getAddressProp();
		
		
		context.close();
		log.trace("Closed my Spring context.");
	}
}



com.springclass.model.JavaCollection

package com.springclass.model;

import java.util.*;

public class JavaCollection {

	List addressList;
	Set addressSet;
	Map addressMap;
	Properties addressProp;


	public void setAddressList(List addressList) {
		this.addressList = addressList;
	}

	public List getAddressList() {
		System.out.println("List Elements :" + addressList);
		return addressList;
	}


	public void setAddressSet(Set addressSet) {
		this.addressSet = addressSet;
	}

	public Set getAddressSet() {
		System.out.println("Set Elements :" + addressSet);
		return addressSet;
	}


	public void setAddressMap(Map addressMap) {
		this.addressMap = addressMap;
	}

	public Map getAddressMap() {
		System.out.println("Map Elements :" + addressMap);
		return addressMap;
	}


	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;
	}
}


context.xml

Put in resources folder under META-INF/spring folders.

<?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.xsd">

   <!-- Definition for javaCollection -->
   <bean id="javaCollection" class="com.springclass.model.JavaCollection">

      <!-- results in a setAddressList(java.util.List) call -->
      <property name="addressList">
        <list>
           <value>New York</value>
           <value>Washington, D.C.</value>
           <value>Memphis</value>
           <value>Dallas</value>
           <value>Dallas</value>
        </list>
      </property>

     <!-- results in a setAddressSet(java.util.Set) call -->
     <property name="addressSet">
        <set>
           <value>New York</value>
           <value>Washington, D.C.</value>
           <value>Memphis</value>
           <value>Dallas</value>
           <value>Dallas</value>
        </set>
      </property>

     <!-- results in a setAddressMap(java.util.Map) call -->
     <property name="addressMap">
        <map>
           <entry key="1" value="New York"/>
           <entry key="2" value="Washington, D.C."/>
           <entry key="3" value="Memphis"/>
           <entry key="4" value="Dallas"/>
        </map>
      </property>

     <!-- results in a setAddressProp(java.util.Properties) call -->
     <property name="addressProp">
        <props>
           <prop key="one">New York</prop>
           <prop key="two">Washington, D.C.</prop>
           <prop key="three">Memphis</prop>
           <prop key="four">Dallas</prop>
        </props>
      </property>

   </bean>

</beans>


<!--
Passing bean reference  for java.util.List
      <property name="addressList">
        <list>
           <ref bean="address1"/>
           <ref bean="address2"/>
           <value>Dallas</value>
        </list>
      </property>

Passing bean reference  for java.util.Set
     <property name="addressSet">
        <set>
           <ref bean="address1"/>
           <ref bean="address2"/>
           <value>Dallas</value>
        </set>
      </property>

Passing bean reference  for java.util.Map
     <property name="addressMap">
        <map>
           <entry key="one" value="New York"/>
           <entry key ="two" value-ref="address1"/>
           <entry key ="three" value-ref="address2"/>
        </map>
      </property>

-->

log4j2.xml

Edit your log4j2.xml configurations as desired.

Create the logs folder in your target directory of your project.

The app will craete the log file myLogFile.log

3) Demo