Spring AOP XML Config Passing Param Project

Click here to download eclipse supported ZIP file



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


 

    
package com.cv.spring.aop.config.passparam;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Before;
/**
 @author Chandra Vardhan
 *
 */
public class Audience implements MindReader {
  
  private final static Logger LOGGER = Logger.getLogger(Audience.class);
  
  public void takeSeats() {
    LOGGER.info("The audience is taking their seats.");
  }

  public void turnOffCellPhones() {
    LOGGER.info("The audience is turning off their cellphones");
  }

  public void applaud() {
    LOGGER.info("CLAP CLAP CLAP CLAP CLAP");
  }

  public void demandRefund() {
    LOGGER.info("Boo! We want our money back!!!!!");
  }

  public void watchPerformance(ProceedingJoinPoint joinpoint) {
    try {
      LOGGER.info("The audience is taking their seats.");
      LOGGER.info("The audience is turning off their cellphones");
      long start = System.currentTimeMillis();
      joinpoint.proceed();
      long end = System.currentTimeMillis();
      LOGGER.info("CLAP CLAP CLAP CLAP CLAP");
      LOGGER.info("The performance took" (end - start)
          "milliseconds.");
    catch (Throwable t) {
      LOGGER.info("Boo! We want our money back!!!");
    }
  }

  
  private String thoughts;
  
  public void thinking(String thoughts){    
    LOGGER.info("thinking() executing...");
  }
  
  
  @Before("thinking(thoughts)")
  public void interceptThoughts(String thoughts) {
    LOGGER.info("Intercepting volunteer' s thoughts===" + thoughts);
    this.thoughts = thoughts;
  }
  public String getThoughts() {
    return thoughts;
  }
}


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


 

    
package com.cv.spring.aop.config.passparam;

/**
 @author Chandra Vardhan
 *
 */
public class City {
  
  private String name;


  private String state;
  
  private int population;
  public String getName() {
    return name;
  }
  
  public void setName(String name) {
    this.name = name;
  }
  
  public String getState() {
    return state;
  }
  
  public void setState(String state) {
    this.state = state;
  }
  
  public int getPopulation() {
    return population;
  }
  
  public void setPopulation(int population) {
    this.population = population;
  }
}


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


 

    
package com.cv.spring.aop.config.passparam;
/**
 @author Chandra Vardhan
 *
 */
public interface Contestant extends Performer {
  void receiveAward();
}


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


 

    
package com.cv.spring.aop.config.passparam;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;
/**
 @author Chandra Vardhan
 *
 */
@Aspect
public class ContestantIntroducer {
  @DeclareParents(value = "com.cv.aop.config.passparam.Performer+", defaultImpl = GraciousContestant.class)
  public static Contestant contestant;
}


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


 

    
package com.cv.spring.aop.config.passparam;

import org.apache.log4j.Logger;

/**
 @author Chandra Vardhan
 *
 */
public class GraciousContestant implements Contestant {
  private final static Logger LOGGER = Logger.getLogger(GraciousContestant.class);
  @Override
  public void receiveAward() {
    LOGGER.info("receiveAward from GraciousContestant class...");
  }

  @Override
  public void perform() {
    LOGGER.info("perform");
  }

}


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


 

    
package com.cv.spring.aop.config.passparam;

/**
 @author Chandra Vardhan
 *
 */
public interface Instrument {

  void play();

}


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


 

    
package com.cv.spring.aop.config.passparam;

import org.apache.log4j.Logger;

/**
 @author Chandra Vardhan
 *
 */
public class Instrumentalist implements Performer {

  private final static Logger LOGGER = Logger.getLogger(Instrumentalist.class);
  
  private String song;

  private City city;
  
  private Instrument instrument;

  public Instrumentalist() {
  }

  public Instrumentalist(Instrument instrument) {

    this.instrument = instrument;
  }

  public Instrument getInstrument() {
    return instrument;
  }

  public void setInstrument(Instrument instrument) {
    this.instrument = instrument;
  }

  public String getSong() {
    return song;
  }

  public void setSong(String song) {
    this.song = song;
  }

  public City getCity() {
    return city;
  }

  public void setCity(City city) {
    this.city = city;
  }

  public void perform() {
    LOGGER.info("Instrument is");
    instrument.play();
    LOGGER.info("song==" + song);
  }

}


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


 

    
package com.cv.spring.aop.config.passparam;

import org.apache.log4j.Logger;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/**
 @author Chandra Vardhan
 *
 */
@Aspect
public class Magician implements MindReader {
  
  private final static Logger LOGGER = Logger.getLogger(Magician.class);
  
  private String thoughts;
  @Pointcut("execution(* com.cv.aop.config.passparam.Thinker.thinkOfSomething(String) && args(thoughts)")
  public void thinking(String thoughts){    
    LOGGER.info("thinking() executing...");
  }
  
  
  @Before("thinking(thoughts)")
  public void interceptThoughts(String thoughts) {
    LOGGER.info("Intercepting volunteer' s thoughts===" + thoughts);
    this.thoughts = thoughts;
  }
  public String getThoughts() {
    return thoughts;
  }
}


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


 

    
package com.cv.spring.aop.config.passparam;
/**
 @author Chandra Vardhan
 *
 */
public interface MindReader {
  
  void interceptThoughts(String thoughts);

  String getThoughts();
}


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


 

    
package com.cv.spring.aop.config.passparam;
/**
 @author Chandra Vardhan
 *
 */
public interface Performer {
     public void perform();
}


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


 

    
package com.cv.spring.aop.config.passparam;

import org.apache.log4j.Logger;

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

  public void play() {
    LOGGER.info("PLINK PLINK");
  }
}


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


 

    
package com.cv.spring.aop.config.passparam;

import org.apache.log4j.Logger;

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

  public void play() {
    LOGGER.info("TOOT TOOT TOOT");
  }
}


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


 

    
package com.cv.spring.aop.config.passparam;


import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

  
  /**
   @param args
   */
  public static void main(String[] args) {
    
    ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext(
        "spring-config.xml");

    Performer performer = (PerformerapplicationContext.getBean("hello");

    performer.perform();
    
    Thinker performer2 = (ThinkerapplicationContext.getBean("thinker");

    performer2.thinkOfSomething("thoughts");
    
    applicationContext.close();
  }

}


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


 

    
package com.cv.spring.aop.config.passparam;
/**
 @author Chandra Vardhan
 *
 */
public interface Thinker {
  void thinkOfSomething(String thoughts);
}


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


 

    
package com.cv.spring.aop.config.passparam;

import org.apache.log4j.Logger;

/**
 @author Chandra Vardhan
 *
 */
public class Volunteer implements Thinker {
  
  private final static Logger LOGGER = Logger.getLogger(Volunteer.class);
  
  private String thoughts;

  public void thinkOfSomething(String thoughts) {
    LOGGER.info("thinkOfSomething==========="+thoughts);
    this.thoughts = thoughts;
  }

  public String getThoughts() {
    return thoughts;
  }
}




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.annotation</groupId> <artifactId>SpringAOPXMLConfigPassingParam</artifactId> <name>SpringAOPXMLConfigPassingParam</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.1.6.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>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>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.6</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.6</version> </dependency> </dependencies> <build> <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> </build> </project>


This is spring-config.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="hello" class="com.cv.spring.aop.config.passparam.Instrumentalist">
<property name="song" value="Jingle Bells" />
<property name="instrument" ref="instrument" />
</bean>
<bean id="instrument" class="com.cv.spring.aop.config.passparam.Piano" />
<bean id="saxophone" class="com.cv.spring.aop.config.passparam.Saxophone" />
<bean id="thinker" class="com.cv.spring.aop.config.passparam.Volunteer"></bean>
<bean id="magician" class="com.cv.spring.aop.config.passparam.Magician"></bean>
<bean id="audience" class="com.cv.spring.aop.config.passparam.Audience"></bean>
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut id="performance"
expression="execution(* com.cv.spring.aop.config.passparam.Performer.perform(..))" />
<aop:before pointcut-ref="performance" method="takeSeats" />
<aop:before pointcut-ref="performance" method="turnOffCellPhones" />
<aop:after-returning pointcut-ref="performance"
method="applaud" />
<aop:after-throwing pointcut-ref="performance"
method="demandRefund" />
<aop:around pointcut-ref="performance" method="watchPerformance" />
</aop:aspect>
</aop:config>
<aop:config>
<aop:aspect ref="magician">
<aop:pointcut id="thinking"
expression="execution(* com.cv.spring.aop.config.passparam.Thinker.thinkOfSomething(String))
and args(thoughts)" />
<aop:before pointcut-ref="thinking" method="interceptThoughts"
arg-names="thoughts" />
</aop:aspect>
</aop:config>
<aop:config>
<aop:aspect>
<aop:declare-parents types-matching="com.cv.spring.aop.config.passparam.Performer+"
implement-interface="com.cv.spring.aop.config.passparam.Contestant"
delegate-ref="contestantDelegate" />
</aop:aspect>
</aop:config>
<bean id="contestantDelegate" class="com.cv.spring.aop.config.passparam.GraciousContestant" />
</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