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

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

Eclipse Maven based Proj
Spring43xMavenStarterProj.zip

2) Project Name: InitDestroyProj

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

com.springclass.app.InitDestroyApp

package com.springclass.app;

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

import com.springclass.service.InitDestroy;


public class InitDestroyApp {
	private static final Logger log = LoggerFactory.getLogger(InitDestroyApp.class);
	public static final String SPRING_BEANS_LOCATION = "Meta-INF/spring/context.xml";
	public static final String MY_BEAN = "initDestroy"; 
	
	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.");
		
		InitDestroy obj = (InitDestroy) context.getBean(MY_BEAN);
		log.trace("Retrieved the bean with id " + MY_BEAN + " from " + SPRING_BEANS_LOCATION);

		obj.getMessage();
		log.trace("Called getMessage on the initDestroy bean.");
		
		context.close();
		log.trace("Closed my Spring context.");
	}
}



com.springclass.service.InitDestroy

package com.springclass.service;

public class InitDestroy {
   
   private String message;

   public void setMessage(String message){
      this.message  = message;
   }
   
   public void getMessage(){
      System.out.println("Your Message : " + message);
   }
   
   public void init(){
      System.out.println("Bean is going through init.");
   }
   
   public void destroy(){
      System.out.println("Bean will be destroyed now.");
   }
}

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

    <!-- default-init-method="init" default-destroy-method="destroy" -->

   <bean id="initDestroy" class="com.springclass.service.InitDestroy">
       <property name="message" value="Hello World!"/>
   </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="initDestroy" class="com.springclass.InitDestroy"
                         init-method="init" destroy-method="destroy">
       <property name="message" value="Hello World!"/>
   </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