BAEL-1240 An introduction to Spring AOP (#2733)

* BAL-36 File size api in java and apache commons IO

* BAEL-282 grep in java - fixes after code review

* BAEL-519 Added support for disruptor library

* BAEL-519 Added support for disruptor library

* BAEL-519 Added support for disruptor library

* BAEL-519 Added support for disruptor library

* BAEL-519 Added support for disruptor library

* BAEL-519 Added support for disruptor library

* BAEL-519 Added support for disruptor

* BAEL-519 Moved all supporting classes to main source

* BAEL-519 Moved all supporting classes to main source

* BAEL-519 Moved asserts and test classes in test folder.

* BAEL-519 moved test related producer and consumer to src.

* BAEL-586 Guide to Guava BiMap.

* BAEL-587 formatted code.

* BAEL-519 LMAX Disruptor

* BAEL-587 resolved merge

* BAEL-587 Resolved merge

* BAEL-519 Removed disruptor link.

* BAEL-519 Reverted Guava changes

* RFQ-587 Added disruptor as a separate module.

* BAEL-519 Disruptor changes.

* BAEL-519 Removed disruptor from core-java module.

* BAEL-729 Expose additional information programmatically in /info
endpoint of actuator.

* BAEL-1240 Introduction to Spring AOP

* BAEL-1240 - Spring AOP using configuration in XML.
This commit is contained in:
Muhammed Almas
2017-10-15 23:53:20 +05:30
committed by Grzegorz Piwowarek
parent eece3d02ba
commit 4aaaf4cea5
17 changed files with 408 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
package org.baeldung.logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AdderAfterAspect {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
public void afterAdvice() throws Throwable {
logger.info("I'm done calling the method");
}
}

View File

@@ -0,0 +1,13 @@
package org.baeldung.logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AdderAfterReturnAspect {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
public void afterReturn(final Object returnValue) throws Throwable {
logger.info("value return was {}", returnValue);
}
}

View File

@@ -0,0 +1,13 @@
package org.baeldung.logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AdderAfterThrowAspect {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
public void afterThrow(final Exception exception) throws Throwable {
logger.info("Exception thrown was {}", exception.getMessage());
}
}

View File

@@ -0,0 +1,18 @@
package org.baeldung.logger;
import java.util.Arrays;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AdderAroundAspect {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
public Object aroundAdvice(final ProceedingJoinPoint joinPoint) throws Throwable {
logger.info("Arguments passed to method are: " + Arrays.toString(joinPoint.getArgs()));
final Object result = joinPoint.proceed();
logger.info("Result from method is: " + result);
return result;
}
}

View File

@@ -0,0 +1,13 @@
package org.baeldung.logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AdderBeforeAspect {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
public void beforeAdvice() throws Throwable {
logger.info("I would be executed just before method starts");
}
}

View File

@@ -0,0 +1,12 @@
package org.baeldung.logger;
public class SampleAdder {
public int add(int a, int b) {
if (a < 0 || b < 0) {
throw new IllegalArgumentException("Make sure all the arguments are greater than zero.");
}
return a + b;
}
}

View File

@@ -0,0 +1,54 @@
<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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<bean id="sampleAdder"
class="org.baeldung.logger.SampleAdder" />
<bean id="doBeforeAspect" class="org.baeldung.logger.AdderBeforeAspect" />
<bean id="doAfterAspect" class="org.baeldung.logger.AdderAfterAspect" />
<bean id="doAfterThrowingAspect" class="org.baeldung.logger.AdderAfterThrowAspect" />
<bean id="doAfterReturningAspect" class="org.baeldung.logger.AdderAfterReturnAspect" />
<bean id="doAroundAspect" class="org.baeldung.logger.AdderAroundAspect" />
<aop:config>
<aop:aspect id="aspects" ref="doBeforeAspect">
<aop:pointcut id="pointCutBefore"
expression="execution(* org.baeldung.logger.SampleAdder+.*(..))" />
<aop:before method="beforeAdvice" pointcut-ref="pointCutBefore" />
</aop:aspect>
<aop:aspect id="aspects" ref="doAfterAspect">
<aop:pointcut id="pointCutAfter"
expression="execution(* org.baeldung.logger.SampleAdder+.*(..))" />
<aop:after method="afterAdvice" pointcut-ref="pointCutAfter" />
</aop:aspect>
<aop:aspect id="aspects" ref="doAfterReturningAspect">
<aop:pointcut id="pointCutAfterReturning"
expression="execution(* org.baeldung.logger.SampleAdder+.*(..))" />
<aop:after-returning method="afterReturn"
returning="returnValue" pointcut-ref="pointCutAfterReturning" />
</aop:aspect>
<aop:aspect id="aspects" ref="doAfterThrowingAspect">
<aop:pointcut id="pointCutAfterThrowing"
expression="execution(* org.baeldung.logger.SampleAdder+.*(..))" />
<aop:after-throwing method="afterThrow"
throwing="error" pointcut-ref="pointCutAfterThrowing" />
</aop:aspect>
<aop:aspect id="aspects" ref="doAroundAspect">
<aop:pointcut id="pointCutAround"
expression="execution(* org.baeldung.logger.SampleAdder+.*(..))" />
<aop:around method="aroundAdvice" pointcut-ref="pointCutAround" />
</aop:aspect>
</aop:config>
</beans>

View File

@@ -0,0 +1,29 @@
package org.baeldung.logger;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = {"classpath:springAop-applicationContext.xml"})
public class CalculatorTest {
@Autowired
private SampleAdder sampleAdder;
@Test
public void whenAddValidValues_returnsSucessfully() {
final int addedValue = sampleAdder.add(12, 12);
assertEquals(24, addedValue);
}
@Test (expected = IllegalArgumentException.class)
public void whenAddInValidValues_throwsException() {
sampleAdder.add(12, -12);
}
}

View File

@@ -0,0 +1,54 @@
<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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<bean id="sampleAdder"
class="org.baeldung.logger.SampleAdder" />
<bean id="doBeforeAspect" class="org.baeldung.logger.AdderBeforeAspect" />
<bean id="doAfterAspect" class="org.baeldung.logger.AdderAfterAspect" />
<bean id="doAfterThrowingAspect" class="org.baeldung.logger.AdderAfterThrowAspect" />
<bean id="doAfterReturningAspect" class="org.baeldung.logger.AdderAfterReturnAspect" />
<bean id="doAroundAspect" class="org.baeldung.logger.AdderAroundAspect" />
<aop:config>
<aop:aspect id="aspects" ref="doBeforeAspect">
<aop:pointcut id="pointCutBefore"
expression="execution(* org.baeldung.logger.SampleAdder+.*(..))" />
<aop:before method="beforeAdvice" pointcut-ref="pointCutBefore" />
</aop:aspect>
<aop:aspect id="aspects" ref="doAfterAspect">
<aop:pointcut id="pointCutAfter"
expression="execution(* org.baeldung.logger.SampleAdder+.*(..))" />
<aop:after method="afterAdvice" pointcut-ref="pointCutAfter" />
</aop:aspect>
<aop:aspect id="aspects" ref="doAfterReturningAspect">
<aop:pointcut id="pointCutAfterReturning"
expression="execution(* org.baeldung.logger.SampleAdder+.*(..))" />
<aop:after-returning method="afterReturn"
returning="returnValue" pointcut-ref="pointCutAfterReturning" />
</aop:aspect>
<aop:aspect id="aspects" ref="doAfterThrowingAspect">
<aop:pointcut id="pointCutAfterThrowing"
expression="execution(* org.baeldung.logger.SampleAdder+.*(..))" />
<aop:after-throwing method="afterThrow"
throwing="exception" pointcut-ref="pointCutAfterThrowing" />
</aop:aspect>
<aop:aspect id="aspects" ref="doAroundAspect">
<aop:pointcut id="pointCutAround"
expression="execution(* org.baeldung.logger.SampleAdder+.*(..))" />
<aop:around method="aroundAdvice" pointcut-ref="pointCutAround" />
</aop:aspect>
</aop:config>
</beans>