Spring4 Basic IOC Project

Click here to download eclipse supported ZIP file



This is Knight.java file having the source code to execute business logic.



 

    
package com.cv.spring.core;

public interface Knight {
  
  void embarkOnQuest();
  
}


This is KnightTest.java file having the source code to execute business logic.



 

    
package com.cv.spring.core;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class KnightTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    String configLocation = "src/main/resources/applicationContext.xml";
    ApplicationContext factory = new FileSystemXmlApplicationContext(configLocation);
    Knight knight = (Knightfactory.getBean("knight");
    knight.embarkOnQuest();
  }
}


This is NQuest.java file having the source code to execute business logic.



 

    
package com.cv.spring.core;

import org.apache.log4j.Logger;

public class NQuest implements Quest {
  
  private final static Logger LOGGER = Logger.getLogger(NQuest.class);
  
  @Override
  public void embark() {
    LOGGER.info("embark() called : : NQuest");
  }
}


This is Quest.java file having the source code to execute business logic.



 

    
package com.cv.spring.core;

public interface Quest {

  void embark();

}




This is SKnight.java file having the source code to execute business logic.



 

    
package com.cv.spring.core;

import org.apache.log4j.Logger;

public class SKnight implements Knight {
  
  private final static Logger LOGGER = Logger.getLogger(SKnight.class);
  
  private Quest quest;

  public SKnight() {

  }

  public SKnight(Quest quest) {
    this.quest = quest;
  }

  @Override
  public void embarkOnQuest() {
    LOGGER.info("embarkOnQuest() called : : SKnight");
    quest.embark();

  }

}


This is pom.xml file having the entries of dependency jars and information to build the application .



	
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">.0" encoding="UTF-8"?> <modelVersion>4.0.0</modelVersion> <groupId>com.cv.spring.core</groupId> <artifactId>Spring4BasicIOC</artifactId> <name>Spring4BasicIOC</name> <packaging>jar</packaging> <version>1.0</version> <properties> <java-version>1.8</java-version> <log4j-version>1.2.16</log4j-version> <org.springframework-version>4.2.0.RELEASE</org.springframework-version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </pluginManagement> <finalName>Spring4BasicIOC</finalName> </build> </project>


This is applicationContext.xml file having the spring configuration properties.



 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">

<bean id="knight" class="com.cv.spring.core.SKnight">
<constructor-arg ref="quest" />
</bean>
<bean id="quest" class="com.cv.spring.core.NQuest" />
</beans>


This is log4j.properties file having the entries for logging the information into the console/file.




#By default enabling Console appender
# Root logger option
log4j.rootLogger=INFO, stdout

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%c]:%L -->> %m%n

# Redirect log messages to a log file
#log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=C:\servlet-application.log
#log4j.appender.file.MaxFileSize=5MB
#log4j.appender.file.MaxBackupIndex=10
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

No comments:

Post a Comment