Skip to content

Commit

Permalink
add annotion
Browse files Browse the repository at this point in the history
  • Loading branch information
wangweiqing committed Jul 29, 2014
1 parent 15bad07 commit 437ecea
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public double add(double a, double b) {
return result;
}

@LoggingRequired
public double sub(double a, double b) {
// TODO Auto-generated method stub
double result = a - b;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.Ordered;

@Aspect
public class CalculatorLoggingAspect {
public class CalculatorLoggingAspect implements Ordered{
/**
* Logger for this class
*/
Expand Down Expand Up @@ -94,12 +95,22 @@ public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
throw e;
}
}

@After(value = "@annotation (com.wwq.calculator.LoggingRequired)")
public void loggingRequired(){
logger.info("JUST TEST!!!JUST TEST!!!JUST TEST!!!");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

}
@Override
public int getOrder() {
// TODO Auto-generated method stub
return 0;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.wwq.calculator;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.Ordered;

@Aspect
public class CalculatorValidationAspect implements Ordered{
@Before("execution(* *.*(double,double))")
public void validateBefore(JoinPoint joinPoint) {
for (Object arg : joinPoint.getArgs()) {
validate((Double) arg);
}
}

private void validate(double a) {
if (a < 0) {
throw new IllegalArgumentException("Positive number only");
}
}

@Override
public int getOrder() {
// TODO Auto-generated method stub
return 1;
}
}
14 changes: 14 additions & 0 deletions src/main/resources/java/com/wwq/calculator/LoggingRequired.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.wwq.calculator;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LoggingRequired {

}
10 changes: 5 additions & 5 deletions src/main/resources/java/com/wwq/calculator/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public static void main(String[] args) {
"src/main/webapp/WEB-INF/applicationContext.xml");
ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) context
.getBean("arithmeticCalculator");
arithmeticCalculator.add(1, 2);
/* arithmeticCalculator.add(1, 2);*/
arithmeticCalculator.sub(4, 3);
arithmeticCalculator.mul(2, 3);
arithmeticCalculator.div(4, 2);
/* arithmeticCalculator.mul(2, 3);
arithmeticCalculator.div(4, -1);*/

UnitCalculator unitCalculator = (UnitCalculator) context
/* UnitCalculator unitCalculator = (UnitCalculator) context
.getBean("unitCalculator");
unitCalculator.kilogramToMile(5);
unitCalculator.kilogramToPound(10);
unitCalculator.kilogramToPound(10);*/
}

}
1 change: 1 addition & 0 deletions src/main/webapp/WEB-INF/applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<bean id="unitCalculator"
class="com.wwq.calculator.UnitCalculatorImpl" />
<bean class="com.wwq.calculator.CalculatorLoggingAspect" />
<bean class="com.wwq.calculator.CalculatorValidationAspect"/>



Expand Down

0 comments on commit 437ecea

Please sign in to comment.