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 InnerBeanProj Project

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

Eclipse Maven based Proj
Spring43xMavenStarterProj.zip

2) Project Name: InnerBeanProj

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.service.TextEditor;

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 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.");
		
		TextEditor editor = (TextEditor) context.getBean("textEditor");
		log.trace("Retrieved the bean with id textEditor from " + SPRING_BEANS_LOCATION);

		editor.spellCheck();
		log.trace("Running spellCheck on my editor TextEditor");
		
		context.close();
		log.trace("Closed my Spring context.");
	}
}


com.springclass.service.TextEditor

package com.springclass.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TextEditor {
	private static final Logger log = LoggerFactory.getLogger(TextEditor.class);
	private SpellChecker spellChecker;

	public TextEditor() {	
	}

	public TextEditor(SpellChecker spellChecker) {
		System.out.println("Inside TextEditor constructor.");
		this.spellChecker = spellChecker;
		log.debug("Inside TextEditor constructor that sets the spellChecker.");
	}

	public void spellCheck() {
		spellChecker.checkSpelling();
		log.trace("Running checkSpelling from TextEditor spellCheck.");
	}
}


com.springclass.service.SpellChecker

package com.springclass.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SpellChecker {
	private static final Logger log = LoggerFactory.getLogger(SpellChecker.class);
	
	public SpellChecker(){
		System.out.println("Inside SpellChecker constructor." );
		log.trace("Inside SpellChecker constructor.");
	}

	public void checkSpelling() {
		System.out.println("I'm checking spelling." );
		log.debug("I'm checking spelling.");
	}

} 


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">
 
 	<!--  Go here to learn more -->
 	<!--	https://docs.spring.io/spring/docs/4.3.18.RELEASE/spring-framework-reference/htmlsingle/#beans  -->

   <!-- Definition for textEditor bean -->
   <bean id="textEditor" class="com.springclass.service.TextEditor">
      <constructor-arg ref="spellChecker"/>
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.springclass.service.SpellChecker">
   </bean>

</beans>

context2.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">
 
 	<!--  Go here to learn more -->
 	<!--	https://docs.spring.io/spring/docs/4.3.18.RELEASE/spring-framework-reference/htmlsingle/#beans  -->

   <bean id="textEditor" class="com.springclass.service.TextEditor">
      <property name="spellChecker">
         <bean class="com.springclass.SpellChecker"/>
       </property>
   </bean>

</beans>

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