Spring Kights IOC Project

Click here to download eclipse supported ZIP file



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


 

    
package com.cv.spring.core;

/**
@author Chandra Vardhan
*
*/
public class BraveKnight implements Knight {
  private Quest quest;
  
  public BraveKnight(Quest quest) {
    this.quest = quest;       //<co id="co_injectedQuest"/>
  }
  
  public void embarkOnQuest() throws QuestException {
    quest.embark();
  }
}


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


 

    
package com.cv.spring.core;

/**
@author Chandra Vardhan
*
*/
public class DamselRescuingKnight implements Knight {
  private RescueDamselQuest quest;
  
  public DamselRescuingKnight() {
    quest = new RescueDamselQuest()//<co id="co_coupledToQuest"/>
  }
  
  public void embarkOnQuest() throws QuestException {
    quest.embark();
  }
}


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


 

    
package com.cv.spring.core;

/**
@author Chandra Vardhan
*
*/
public interface Knight {
  void embarkOnQuest() throws QuestException;
}


This is KnightAopMain.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.ClassPathXmlApplicationContext;

/**
 @author Chandra Vardhan
 *
 */
public class KnightAopMain {
  public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext(
        "knights-aop.xml");

    Knight knight = (Knightcontext.getBean("knight");

    knight.embarkOnQuest();
  }
}


This is KnightMain.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.ClassPathXmlApplicationContext;

/**
 @author Chandra Vardhan
 *
 */
public class KnightMain {
  public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext(
        "knights.xml")// <co id="co_loadKnightContext"/>

    Knight knight = (Knightcontext.getBean("knight")// <co
                              // id="co_getBeanKnight"/>

    knight.embarkOnQuest();// <co id="co_useKnight"/>
  }
}


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


 

    
package com.cv.spring.core;

import org.apache.log4j.Logger;

/**
 @author Chandra Vardhan
 *
 */
public class MakeRoundTableRounderQuest implements Quest {

  private final static Logger LOGGER = Logger
      .getLogger(MakeRoundTableRounderQuest.class);

  public void embark() throws QuestException {
    LOGGER.info("Making round table rounder");
  }

}


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


 

    
package com.cv.spring.core;

import org.apache.log4j.Logger;

/**
 @author Chandra Vardhan
 *
 */
public class Minstrel {

  private final static Logger LOGGER = Logger.getLogger(Minstrel.class);

  public void singBeforeQuest() { // <co id="co_singBefore"/>
    LOGGER.info("Fa la la; The knight is so brave!");
  }

  public void singAfterQuest() { // <co id="co_singAfter"/>
    LOGGER.info("Tee hee he; The brave knight did embark on a quest!");
  }
}


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


 

    
package com.cv.spring.core;

/**
@author Chandra Vardhan
*
*/
public interface Quest {
  void embark() throws QuestException;
}


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


 

    
package com.cv.spring.core;

/**
@author Chandra Vardhan
*
*/
public class QuestException extends RuntimeException {
  private static final long serialVersionUID = 1L;
}


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


 

    
package com.cv.spring.core;

import org.apache.log4j.Logger;

/**
 @author Chandra Vardhan
 *
 */
public class RescueDamselQuest implements Quest {
  private final static Logger LOGGER = Logger
      .getLogger(RescueDamselQuest.class);

  public void embark() throws QuestException {
    LOGGER.info("Rescuing damsel in distress");
  }
}


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


 

    
package com.cv.spring.core;

import org.apache.log4j.Logger;

/**
@author Chandra Vardhan
*
*/
public class SlayDragonQuest implements Quest {
  
  private final static Logger LOGGER = Logger.getLogger(SlayDragonQuest.class);
  

  public void embark() throws QuestException {
    LOGGER.info("Slaying Dragon!");
  }

}




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"> <modelVersion>4.0.0</modelVersion> <groupId>SpringKinghtsIOC</groupId> <artifactId>com.cv.spring.core</artifactId> <version>1.0</version> <name>SpringKinghtsIOC</name> <packaging>jar</packaging> <properties> <java-version>1.8</java-version> <log4j-version>1.2.16</log4j-version> <org.springframework-version>4.2.0.RELEASE</org.springframework-version> <jackson.version>2.5.3</jackson.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>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${org.springframework-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>SpringKinghtsIOC</finalName> </build> </project>


This is knights-aop.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="knight" class="com.cv.spring.core.BraveKnight">
<constructor-arg ref="quest" />
</bean>
<bean id="quest"
class="com.cv.spring.core.SlayDragonQuest" />

<bean id="minstrel"
class="com.cv.spring.core.Minstrel" /> <!--<co id="co_minstrel_bean"/>-->

<aop:config>
<aop:aspect ref="minstrel">

<aop:pointcut id="embark"
expression="execution(* *.embarkOnQuest(..))" /> <!--<co id="co_define_pointcut"/>-->
<aop:before pointcut-ref="embark"
method="singBeforeQuest"/> <!--<co id="co_minstrel_before_advice"/>-->
<aop:after pointcut-ref="embark"
method="singAfterQuest"/> <!--<co id="co_minstrel_after_advice"/>-->
</aop:aspect>
</aop:config>
</beans>


This is knights.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="knight" class="com.cv.spring.core.BraveKnight">
<constructor-arg ref="quest" /> <!--<co id="co_inject_quest_bean"/>-->
</bean>
<bean id="quest"
class="com.cv.spring.core.SlayDragonQuest" /><!--<co id="co_quest_bean"/>-->

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