From 3dd8df2fd2ed6e7be978e7f01f8c2b1e17b4d705 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Mon, 11 Apr 2016 02:43:19 +0200 Subject: [PATCH] Spring AOP and maven plugin --- SpringJava/AOP/SpringAOP20/pom.xml | 124 ++++++++++ .../main/java/de/spring/example/GeneralAccess.java | 21 ++ .../main/java/de/spring/example/SpringAdvice.java | 23 ++ .../de/spring/example/SpringContextLocator.java | 53 +++++ .../main/java/de/spring/example/SpringStart.java | 25 ++ .../java/de/spring/example/SystemArchitecture.java | 43 ++++ .../main/java/de/spring/example/service/TestB.java | 16 ++ .../src/main/java/de/spring/example/web/TestA.java | 16 ++ .../AOP/SpringAOP20/src/main/resources/log4j2.xml | 43 ++++ .../src/main/resources/spring-config.xml | 80 +++++++ SpringJava/AOP/SpringAspectJ/pom.xml | 124 ++++++++++ .../src/main/java/de/spring/example/MyAdvice.java | 44 ++++ .../main/java/de/spring/example/SpringAdvice.java | 22 ++ .../de/spring/example/SpringContextLocator.java | 57 +++++ .../main/java/de/spring/example/SpringStart.java | 22 ++ .../java/de/spring/example/SystemArchitecture.java | 53 +++++ .../src/main/java/de/spring/example/web/Test.java | 30 +++ .../SpringAspectJ/src/main/resources/log4j2.xml | 43 ++++ .../src/main/resources/spring-config.xml | 59 +++++ SpringJava/AOP/SpringLTW/README | 3 + SpringJava/AOP/SpringLTW/pom.xml | 190 +++++++++++++++ .../src/main/java/de/spring/example/MyAdvice.java | 28 +++ .../de/spring/example/SpringContextLocator.java | 57 +++++ .../main/java/de/spring/example/SpringStart.java | 22 ++ .../java/de/spring/example/TransactionManager.java | 48 ++++ .../example/annotation/commitTransactional.java | 14 ++ .../example/annotation/initTransactional.java | 14 ++ .../src/main/java/de/spring/example/web/Test.java | 40 ++++ .../SpringLTW/src/main/resources/META-INF/aop.xml | 23 ++ .../AOP/SpringLTW/src/main/resources/log4j2.xml | 43 ++++ .../SpringLTW/src/main/resources/spring-config.xml | 68 ++++++ SpringJava/AOP/SpringMavenWeaver/pom.xml | 256 +++++++++++++++++++++ .../src/main/java/de/spring/example/MyAdvice.java | 28 +++ .../de/spring/example/SpringContextLocator.java | 57 +++++ .../main/java/de/spring/example/SpringStart.java | 22 ++ .../java/de/spring/example/TransactionManager.java | 48 ++++ .../example/annotation/commitTransactional.java | 14 ++ .../example/annotation/initTransactional.java | 14 ++ .../src/main/java/de/spring/example/web/Test.java | 40 ++++ .../src/main/resources/log4j2.xml | 43 ++++ .../src/main/resources/spring-config.xml | 68 ++++++ SpringJava/SpringAOP20/pom.xml | 124 ---------- .../main/java/de/spring/example/GeneralAccess.java | 21 -- .../main/java/de/spring/example/SpringAdvice.java | 23 -- .../de/spring/example/SpringContextLocator.java | 53 ----- .../main/java/de/spring/example/SpringStart.java | 25 -- .../java/de/spring/example/SystemArchitecture.java | 43 ---- .../main/java/de/spring/example/service/TestB.java | 16 -- .../src/main/java/de/spring/example/web/TestA.java | 16 -- .../SpringAOP20/src/main/resources/log4j2.xml | 43 ---- .../src/main/resources/spring-config.xml | 80 ------- SpringJava/SpringAspectJ/pom.xml | 124 ---------- .../src/main/java/de/spring/example/MyAdvice.java | 44 ---- .../main/java/de/spring/example/SpringAdvice.java | 22 -- .../de/spring/example/SpringContextLocator.java | 57 ----- .../main/java/de/spring/example/SpringStart.java | 22 -- .../java/de/spring/example/SystemArchitecture.java | 53 ----- .../src/main/java/de/spring/example/web/Test.java | 30 --- .../SpringAspectJ/src/main/resources/log4j2.xml | 43 ---- .../src/main/resources/spring-config.xml | 59 ----- SpringJava/SpringLTW/README | 3 - SpringJava/SpringLTW/pom.xml | 190 --------------- .../src/main/java/de/spring/example/MyAdvice.java | 28 --- .../de/spring/example/SpringContextLocator.java | 57 ----- .../main/java/de/spring/example/SpringStart.java | 22 -- .../java/de/spring/example/TransactionManager.java | 48 ---- .../example/annotation/commitTransactional.java | 14 -- .../example/annotation/initTransactional.java | 14 -- .../src/main/java/de/spring/example/web/Test.java | 40 ---- .../SpringLTW/src/main/resources/META-INF/aop.xml | 23 -- SpringJava/SpringLTW/src/main/resources/log4j2.xml | 43 ---- .../SpringLTW/src/main/resources/spring-config.xml | 68 ------ 72 files changed, 2038 insertions(+), 1448 deletions(-) create mode 100644 SpringJava/AOP/SpringAOP20/pom.xml create mode 100644 SpringJava/AOP/SpringAOP20/src/main/java/de/spring/example/GeneralAccess.java create mode 100644 SpringJava/AOP/SpringAOP20/src/main/java/de/spring/example/SpringAdvice.java create mode 100644 SpringJava/AOP/SpringAOP20/src/main/java/de/spring/example/SpringContextLocator.java create mode 100644 SpringJava/AOP/SpringAOP20/src/main/java/de/spring/example/SpringStart.java create mode 100644 SpringJava/AOP/SpringAOP20/src/main/java/de/spring/example/SystemArchitecture.java create mode 100644 SpringJava/AOP/SpringAOP20/src/main/java/de/spring/example/service/TestB.java create mode 100644 SpringJava/AOP/SpringAOP20/src/main/java/de/spring/example/web/TestA.java create mode 100644 SpringJava/AOP/SpringAOP20/src/main/resources/log4j2.xml create mode 100644 SpringJava/AOP/SpringAOP20/src/main/resources/spring-config.xml create mode 100644 SpringJava/AOP/SpringAspectJ/pom.xml create mode 100644 SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/MyAdvice.java create mode 100644 SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/SpringAdvice.java create mode 100644 SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/SpringContextLocator.java create mode 100644 SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/SpringStart.java create mode 100644 SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/SystemArchitecture.java create mode 100644 SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/web/Test.java create mode 100644 SpringJava/AOP/SpringAspectJ/src/main/resources/log4j2.xml create mode 100644 SpringJava/AOP/SpringAspectJ/src/main/resources/spring-config.xml create mode 100644 SpringJava/AOP/SpringLTW/README create mode 100644 SpringJava/AOP/SpringLTW/pom.xml create mode 100644 SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/MyAdvice.java create mode 100644 SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/SpringContextLocator.java create mode 100644 SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/SpringStart.java create mode 100644 SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/TransactionManager.java create mode 100644 SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/annotation/commitTransactional.java create mode 100644 SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/annotation/initTransactional.java create mode 100644 SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/web/Test.java create mode 100644 SpringJava/AOP/SpringLTW/src/main/resources/META-INF/aop.xml create mode 100644 SpringJava/AOP/SpringLTW/src/main/resources/log4j2.xml create mode 100644 SpringJava/AOP/SpringLTW/src/main/resources/spring-config.xml create mode 100644 SpringJava/AOP/SpringMavenWeaver/pom.xml create mode 100644 SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/MyAdvice.java create mode 100644 SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/SpringContextLocator.java create mode 100644 SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/SpringStart.java create mode 100644 SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/TransactionManager.java create mode 100644 SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/annotation/commitTransactional.java create mode 100644 SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/annotation/initTransactional.java create mode 100644 SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/web/Test.java create mode 100644 SpringJava/AOP/SpringMavenWeaver/src/main/resources/log4j2.xml create mode 100644 SpringJava/AOP/SpringMavenWeaver/src/main/resources/spring-config.xml delete mode 100644 SpringJava/SpringAOP20/pom.xml delete mode 100644 SpringJava/SpringAOP20/src/main/java/de/spring/example/GeneralAccess.java delete mode 100644 SpringJava/SpringAOP20/src/main/java/de/spring/example/SpringAdvice.java delete mode 100644 SpringJava/SpringAOP20/src/main/java/de/spring/example/SpringContextLocator.java delete mode 100644 SpringJava/SpringAOP20/src/main/java/de/spring/example/SpringStart.java delete mode 100644 SpringJava/SpringAOP20/src/main/java/de/spring/example/SystemArchitecture.java delete mode 100644 SpringJava/SpringAOP20/src/main/java/de/spring/example/service/TestB.java delete mode 100644 SpringJava/SpringAOP20/src/main/java/de/spring/example/web/TestA.java delete mode 100644 SpringJava/SpringAOP20/src/main/resources/log4j2.xml delete mode 100644 SpringJava/SpringAOP20/src/main/resources/spring-config.xml delete mode 100644 SpringJava/SpringAspectJ/pom.xml delete mode 100644 SpringJava/SpringAspectJ/src/main/java/de/spring/example/MyAdvice.java delete mode 100644 SpringJava/SpringAspectJ/src/main/java/de/spring/example/SpringAdvice.java delete mode 100644 SpringJava/SpringAspectJ/src/main/java/de/spring/example/SpringContextLocator.java delete mode 100644 SpringJava/SpringAspectJ/src/main/java/de/spring/example/SpringStart.java delete mode 100644 SpringJava/SpringAspectJ/src/main/java/de/spring/example/SystemArchitecture.java delete mode 100644 SpringJava/SpringAspectJ/src/main/java/de/spring/example/web/Test.java delete mode 100644 SpringJava/SpringAspectJ/src/main/resources/log4j2.xml delete mode 100644 SpringJava/SpringAspectJ/src/main/resources/spring-config.xml delete mode 100644 SpringJava/SpringLTW/README delete mode 100644 SpringJava/SpringLTW/pom.xml delete mode 100644 SpringJava/SpringLTW/src/main/java/de/spring/example/MyAdvice.java delete mode 100644 SpringJava/SpringLTW/src/main/java/de/spring/example/SpringContextLocator.java delete mode 100644 SpringJava/SpringLTW/src/main/java/de/spring/example/SpringStart.java delete mode 100644 SpringJava/SpringLTW/src/main/java/de/spring/example/TransactionManager.java delete mode 100644 SpringJava/SpringLTW/src/main/java/de/spring/example/annotation/commitTransactional.java delete mode 100644 SpringJava/SpringLTW/src/main/java/de/spring/example/annotation/initTransactional.java delete mode 100644 SpringJava/SpringLTW/src/main/java/de/spring/example/web/Test.java delete mode 100644 SpringJava/SpringLTW/src/main/resources/META-INF/aop.xml delete mode 100644 SpringJava/SpringLTW/src/main/resources/log4j2.xml delete mode 100644 SpringJava/SpringLTW/src/main/resources/spring-config.xml diff --git a/SpringJava/AOP/SpringAOP20/pom.xml b/SpringJava/AOP/SpringAOP20/pom.xml new file mode 100644 index 0000000..b43ac81 --- /dev/null +++ b/SpringJava/AOP/SpringAOP20/pom.xml @@ -0,0 +1,124 @@ + + + + + 4.0.0 + de.spring.example + spring-aop-example + 2.0-SNAPSHOT + spring-aop-example + http://gumartinm.name + + UTF-8 + + + + + org.springframework + spring-context + 4.2.5.RELEASE + + + + commons-logging + commons-logging + + + + + org.springframework + spring-aspects + 4.2.5.RELEASE + + + + commons-logging + commons-logging + + + + + org.springframework + spring-aop + 4.2.5.RELEASE + + + + commons-logging + commons-logging + + + + + + cglib + cglib + 3.2.1 + + + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.3 + + + + org.apache.logging.log4j + log4j-core + 2.3 + + + + org.slf4j + jcl-over-slf4j + 1.7.12 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + 1.8 + 1.8 + ${project.build.sourceEncoding} + + + + org.apache.maven.plugins + maven-resources-plugin + 2.7 + + ${project.build.sourceEncoding} + + + + + diff --git a/SpringJava/AOP/SpringAOP20/src/main/java/de/spring/example/GeneralAccess.java b/SpringJava/AOP/SpringAOP20/src/main/java/de/spring/example/GeneralAccess.java new file mode 100644 index 0000000..ce30630 --- /dev/null +++ b/SpringJava/AOP/SpringAOP20/src/main/java/de/spring/example/GeneralAccess.java @@ -0,0 +1,21 @@ +package de.spring.example; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * This class is not using the AspectJ annotations, so we could use it on JDK 1.4 and below. + * If we want to use it as an Aspect we may JUST do it using a schema based declaration. + * What means, you can use this class as an Aspect JUST using an xml Spring declaration. + * + */ +public class GeneralAccess { + private static final Logger LOGGER = LoggerFactory.getLogger(GeneralAccess.class); + + + public void monitor() + { + LOGGER.info("I am the Advice monitor for TestB and I will be run before."); + } +} diff --git a/SpringJava/AOP/SpringAOP20/src/main/java/de/spring/example/SpringAdvice.java b/SpringJava/AOP/SpringAOP20/src/main/java/de/spring/example/SpringAdvice.java new file mode 100644 index 0000000..1555575 --- /dev/null +++ b/SpringJava/AOP/SpringAOP20/src/main/java/de/spring/example/SpringAdvice.java @@ -0,0 +1,23 @@ +package de.spring.example; + +import java.lang.reflect.Method; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.aop.MethodBeforeAdvice; + +/** + * + * We are using here an Advice of Spring 1.2 + * See: http://static.springsource.org/spring/docs/3.1.0.RC1/spring-framework-reference/html/aop-api.html#aop-api-advice-types + * + */ +public class SpringAdvice implements MethodBeforeAdvice { + private static final Logger LOGGER = LoggerFactory.getLogger(SpringAdvice.class); + + + public void before(Method m, Object[] args, Object target) throws Throwable { + LOGGER.info("I am the SpringAdvice and I will be run before."); + } + +} diff --git a/SpringJava/AOP/SpringAOP20/src/main/java/de/spring/example/SpringContextLocator.java b/SpringJava/AOP/SpringAOP20/src/main/java/de/spring/example/SpringContextLocator.java new file mode 100644 index 0000000..d599c8f --- /dev/null +++ b/SpringJava/AOP/SpringAOP20/src/main/java/de/spring/example/SpringContextLocator.java @@ -0,0 +1,53 @@ +package de.spring.example; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + + +/** + * + */ +public final class SpringContextLocator { + + // Singleton Pattern + private static SpringContextLocator instance; + + // Spring ApplicationContext + private static ApplicationContext context; + + // Spring Context + private static final String SPRING_CONFIG_CONTEXT="spring-config.xml"; + + + /** + * Private constructor. Singleton pattern. + */ + private SpringContextLocator() { + String[] factoryFiles = null; + System.out.println("Loading context files: " + SpringContextLocator.SPRING_CONFIG_CONTEXT); + + factoryFiles = new String[] { SPRING_CONFIG_CONTEXT }; + + SpringContextLocator.context = new ClassPathXmlApplicationContext(factoryFiles); + + System.out.println("The context has been loaded successfully!! "); + } + + /** + * Singleton pattern not thread safety. To use SingletoHolder pattern as the best approximation + * otherwise to use an Enum class (see Effective Java Second Edition and ) if we need serialization. + */ + public static SpringContextLocator getInstance() { + if (SpringContextLocator.instance == null) { + SpringContextLocator.instance = new SpringContextLocator(); + } + return SpringContextLocator.instance; + } + + /** + * Return bean from application context. + */ + public Object getBean(final String name) { + return SpringContextLocator.context.getBean(name); + } +} \ No newline at end of file diff --git a/SpringJava/AOP/SpringAOP20/src/main/java/de/spring/example/SpringStart.java b/SpringJava/AOP/SpringAOP20/src/main/java/de/spring/example/SpringStart.java new file mode 100644 index 0000000..c6ed090 --- /dev/null +++ b/SpringJava/AOP/SpringAOP20/src/main/java/de/spring/example/SpringStart.java @@ -0,0 +1,25 @@ +package de.spring.example; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import de.spring.example.service.TestB; +import de.spring.example.web.TestA; + +public class SpringStart { + private static final Logger LOGGER = LoggerFactory.getLogger(SpringStart.class); + + /** + * @param args + */ + public static void main(String[] args) { + LOGGER.info("Starting application"); + SpringContextLocator.getInstance(); + + TestA testA = (TestA) SpringContextLocator.getInstance().getBean("testA"); + testA.myMethod(); + + TestB testB = (TestB) SpringContextLocator.getInstance().getBean("testB"); + testB.myMethod(); + } +} diff --git a/SpringJava/AOP/SpringAOP20/src/main/java/de/spring/example/SystemArchitecture.java b/SpringJava/AOP/SpringAOP20/src/main/java/de/spring/example/SystemArchitecture.java new file mode 100644 index 0000000..822f240 --- /dev/null +++ b/SpringJava/AOP/SpringAOP20/src/main/java/de/spring/example/SystemArchitecture.java @@ -0,0 +1,43 @@ +package de.spring.example; + +import org.aspectj.lang.annotation.Pointcut; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * Using the @Aspect annotation you could use this class as an Aspect without + * using a schema based declaration (without using + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SpringJava/AOP/SpringAOP20/src/main/resources/spring-config.xml b/SpringJava/AOP/SpringAOP20/src/main/resources/spring-config.xml new file mode 100644 index 0000000..bda1080 --- /dev/null +++ b/SpringJava/AOP/SpringAOP20/src/main/resources/spring-config.xml @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SpringJava/AOP/SpringAspectJ/pom.xml b/SpringJava/AOP/SpringAspectJ/pom.xml new file mode 100644 index 0000000..be48cff --- /dev/null +++ b/SpringJava/AOP/SpringAspectJ/pom.xml @@ -0,0 +1,124 @@ + + + + + 4.0.0 + de.spring.example + spring-aspectj + 2.0-SNAPSHOT + spring-aspectj + http://gumartinm.name + + UTF-8 + + + + + org.springframework + spring-context + 4.2.5.RELEASE + + + + commons-logging + commons-logging + + + + + org.springframework + spring-aspects + 4.2.5.RELEASE + + + + commons-logging + commons-logging + + + + + org.springframework + spring-aop + 4.2.5.RELEASE + + + + commons-logging + commons-logging + + + + + + cglib + cglib + 3.2.1 + + + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.3 + + + + org.apache.logging.log4j + log4j-core + 2.3 + + + + org.slf4j + jcl-over-slf4j + 1.7.12 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + 1.8 + 1.8 + ${project.build.sourceEncoding} + + + + org.apache.maven.plugins + maven-resources-plugin + 2.7 + + ${project.build.sourceEncoding} + + + + + diff --git a/SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/MyAdvice.java b/SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/MyAdvice.java new file mode 100644 index 0000000..5121bba --- /dev/null +++ b/SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/MyAdvice.java @@ -0,0 +1,44 @@ +package de.spring.example; + +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Aspect +public class MyAdvice { + private static final Logger LOGGER = LoggerFactory.getLogger(MyAdvice.class); + + + //This advice is connected with the Pointcut defined in SystemArchitecture. + //So, every method connected to that Pointcut will be run after the + //method defined in this Advice. + @Before("de.spring.example.SystemArchitecture.pointCutMethod())") + public void doAccessCheck() { + LOGGER.info("I am the Advice and I will be run before."); + } + + + //NOTICE: YOU DO NOT NEED TO CREATE A SPECIAL CLASS FOR POINTCUTS + // YOU COULD DEFINE AN ADVICE WITHOUT A POINTCUT + //This advice has a PointCut defined like execution(* de.spring.example.web.Test.anotherExample()) + //right here without a special PointCut method. This advice has itself the PointCut + @Around("execution(* de.spring.example.web.Test.anotherExample())") + public Object doAround(ProceedingJoinPoint pjp) { + LOGGER.info("I am the Advice and I will be run before and after. BEFORE"); + // start stopwatch + // This local variable will store the returned value from the method anotherExample() + Object retVal=null; + try { + //Calling the real method + retVal = pjp.proceed(); + } catch (Throwable e) { + LOGGER.info("Error", e); + } + // stop stopwatch + LOGGER.info("I am the Advice and I will be run before and after. AFTER " + retVal); + return retVal; + } +} diff --git a/SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/SpringAdvice.java b/SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/SpringAdvice.java new file mode 100644 index 0000000..e5bdf66 --- /dev/null +++ b/SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/SpringAdvice.java @@ -0,0 +1,22 @@ +package de.spring.example; + +import java.lang.reflect.Method; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.aop.MethodBeforeAdvice; + +/** + * + * We are using here an Advice of Spring 1.2 + * See: http://static.springsource.org/spring/docs/3.1.0.RC1/spring-framework-reference/html/aop-api.html#aop-api-advice-types + * + */ +public class SpringAdvice implements MethodBeforeAdvice { + private static final Logger LOGGER = LoggerFactory.getLogger(SpringAdvice.class); + + public void before(Method m, Object[] args, Object target) throws Throwable { + LOGGER.info("I am the SpringAdvice and I will be run before."); + } + +} diff --git a/SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/SpringContextLocator.java b/SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/SpringContextLocator.java new file mode 100644 index 0000000..ff58276 --- /dev/null +++ b/SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/SpringContextLocator.java @@ -0,0 +1,57 @@ +package de.spring.example; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + + +/** + * + */ +public final class SpringContextLocator { + private static final Logger LOGGER = LoggerFactory.getLogger(SpringAdvice.class); + + + // Singleton Pattern + private static SpringContextLocator instance; + + // Spring ApplicationContext + private static ApplicationContext context; + + // Spring Context + private static final String SPRING_CONFIG_CONTEXT="spring-config.xml"; + + + /** + * Private constructor. Singleton pattern. + */ + private SpringContextLocator() { + String[] factoryFiles = null; + LOGGER.info("Loading context files: " + SpringContextLocator.SPRING_CONFIG_CONTEXT); + + factoryFiles = new String[] { SPRING_CONFIG_CONTEXT }; + + SpringContextLocator.context = new ClassPathXmlApplicationContext(factoryFiles); + + LOGGER.info("The context has been loaded successfully!! "); + } + + /** + * Singleton pattern not thread safety. To use SingletoHolder pattern as the best approximation + * otherwise to use an Enum class (see Effective Java Second Edition and ) if we need serialization. + */ + public static SpringContextLocator getInstance() { + if (SpringContextLocator.instance == null) { + SpringContextLocator.instance = new SpringContextLocator(); + } + return SpringContextLocator.instance; + } + + /** + * Return bean from application context. + */ + public Object getBean(final String name) { + return SpringContextLocator.context.getBean(name); + } +} \ No newline at end of file diff --git a/SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/SpringStart.java b/SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/SpringStart.java new file mode 100644 index 0000000..f553341 --- /dev/null +++ b/SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/SpringStart.java @@ -0,0 +1,22 @@ +package de.spring.example; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import de.spring.example.web.Test; + +public class SpringStart { + private static final Logger LOGGER = LoggerFactory.getLogger(SpringStart.class); + + /** + * @param args + */ + public static void main(String[] args) { + LOGGER.info("Starting application"); + SpringContextLocator.getInstance(); + + Test test = (Test) SpringContextLocator.getInstance().getBean("test"); + test.myMethod(); + test.anotherExample(); + } +} diff --git a/SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/SystemArchitecture.java b/SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/SystemArchitecture.java new file mode 100644 index 0000000..ef83b3d --- /dev/null +++ b/SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/SystemArchitecture.java @@ -0,0 +1,53 @@ +package de.spring.example; + +import org.aspectj.lang.annotation.AfterReturning; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * We are using here the @AspectJ annotations to declare + * Proxies. If we want to use these kinds of proxies on the Spring framework + * we have to use the annotation on the Spring xml file + * (the Spring context file) + */ +@Aspect +public class SystemArchitecture { + private static final Logger LOGGER = LoggerFactory.getLogger(SpringStart.class); + + + //Coonecting to the execution of any method defined in the + //package: de.spring.example.web + //We are connecting the methods defined in that package with this + //Pointcut. So, when executing any of those methods defined in that + //package we will run the Advice related to this Pointcut (if there is an Advice) + @Pointcut("execution(* de.spring.example.web.*.*(..))") + public void pointCutMethod() + { + LOGGER.info("I am the Pointcut and you will never see me."); + //This is the PointCut. + //You can write code here, but it will be useless because while running + //the methods connected to the Pointcut, this code will not be executed. + //Just the advice will run!!!! :/ + //Is not this weird? We are here defining a method whose code + //will never be run. When the hell should we write code here? + //This is a waste of time and code IMHO. Isn't it? + } + + //NOTICE: YOU DO NOT NEED TO CREATE A SPECIAL CLASS FOR THE ADVICE + // YOU COULD USE THE SAME CLASS FOR THE POINTCUTS AND FOR THE + // ADVICES. IN THIS CASE FOR EXAMPLE WE HAVE THE @AfterReturning + // ADVICE IN THIS CLASS AND THE @Before ADVICE IN THE CLASS CALLED + // MyAdvice + //This advice is connected with the another Pointcut. + //The returning value of every method connected to that Pointcut + //will be caught by this method. + @AfterReturning(pointcut="de.spring.example.SystemArchitecture.pointCutMethod())", + returning="retVal") + public void doAccessCheck(final Object retVal) { + LOGGER.info("The returned value by the method " + + "connected to the Pointcut: " + retVal); + } +} \ No newline at end of file diff --git a/SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/web/Test.java b/SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/web/Test.java new file mode 100644 index 0000000..d01bd5d --- /dev/null +++ b/SpringJava/AOP/SpringAspectJ/src/main/java/de/spring/example/web/Test.java @@ -0,0 +1,30 @@ +package de.spring.example.web; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Test { + private static final Logger LOGGER = LoggerFactory.getLogger(Test.class); + + public int myMethod() + { + LOGGER.info("The Advice should be run before."); + + //This value will be caught by the Advice with the @AfterReturning annotation. + return 666; + } + + public int anotherExample() + { + LOGGER.info("The Advice should be run before and after."); + return 666; + + } + + public class InnerTest { + public void innerMethod() { + LOGGER.info("I am the inner class. The Advice should be run after." + + " NO NEED OF DECLARING Spring BEANS WHEN WEAVING!!!!"); + } + } +} diff --git a/SpringJava/AOP/SpringAspectJ/src/main/resources/log4j2.xml b/SpringJava/AOP/SpringAspectJ/src/main/resources/log4j2.xml new file mode 100644 index 0000000..8b3fc3f --- /dev/null +++ b/SpringJava/AOP/SpringAspectJ/src/main/resources/log4j2.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SpringJava/AOP/SpringAspectJ/src/main/resources/spring-config.xml b/SpringJava/AOP/SpringAspectJ/src/main/resources/spring-config.xml new file mode 100644 index 0000000..6aa0317 --- /dev/null +++ b/SpringJava/AOP/SpringAspectJ/src/main/resources/spring-config.xml @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SpringJava/AOP/SpringLTW/README b/SpringJava/AOP/SpringLTW/README new file mode 100644 index 0000000..9091fb8 --- /dev/null +++ b/SpringJava/AOP/SpringLTW/README @@ -0,0 +1,3 @@ +VM arguments: + +-javaagent:$HOME/.m2/repository/org/springframework/spring-instrument/4.2.5.RELEASE/spring-instrument-4.2.5.RELEASE.jar diff --git a/SpringJava/AOP/SpringLTW/pom.xml b/SpringJava/AOP/SpringLTW/pom.xml new file mode 100644 index 0000000..5ebc4a0 --- /dev/null +++ b/SpringJava/AOP/SpringLTW/pom.xml @@ -0,0 +1,190 @@ + + + + + 4.0.0 + de.spring.example + spring-ltw + 2.0-SNAPSHOT + spring-ltw + http://gumartinm.name + + UTF-8 + + + + + org.springframework + spring-context + 4.2.5.RELEASE + + + + commons-logging + commons-logging + + + + + org.springframework + spring-aspects + 4.2.5.RELEASE + + + + commons-logging + commons-logging + + + + + org.springframework + spring-aop + 4.2.5.RELEASE + + + + commons-logging + commons-logging + + + + + org.springframework + spring-instrument + 4.2.5.RELEASE + + + + commons-logging + commons-logging + + + + + javax.cache + cache-api + 1.0.0-PFD + + + javax.transaction + javax.transaction-api + 1.2 + + + org.springframework + spring-context-support + 4.2.5.RELEASE + + + + commons-logging + commons-logging + + + + + org.springframework + spring-jdbc + 4.2.5.RELEASE + + + + commons-logging + commons-logging + + + + + + cglib + cglib + 3.2.1 + + + + com.mchange + c3p0 + 0.9.5.2 + + + mysql + mysql-connector-java + 5.1.38 + + + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.3 + + + + org.apache.logging.log4j + log4j-core + 2.3 + + + + org.slf4j + jcl-over-slf4j + 1.7.12 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + 1.8 + 1.8 + ${project.build.sourceEncoding} + + + + org.apache.maven.plugins + maven-resources-plugin + 2.7 + + ${project.build.sourceEncoding} + + + + + diff --git a/SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/MyAdvice.java b/SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/MyAdvice.java new file mode 100644 index 0000000..6e25974 --- /dev/null +++ b/SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/MyAdvice.java @@ -0,0 +1,28 @@ +package de.spring.example; + +import org.aspectj.lang.annotation.After; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Aspect +public class MyAdvice { + private static final Logger LOGGER = LoggerFactory.getLogger(MyAdvice.class); + + // With execution we avoid double weaving (when call and when execution) + @Before("@annotation(de.spring.example.annotation.initTransactional) && execution(* *(..))") + public void initTransactional() + { + LOGGER.info("I am the Advice initTransaction."); + TransactionManager.getInstance().initTransaction(); + } + + + // With execution we avoid double weaving (when call and when execution) + @After("@annotation(de.spring.example.annotation.commitTransactional) && execution(* *(..))") + public void commitTransactional() { + LOGGER.info("I am the Advice commitTransaction."); + TransactionManager.getInstance().commitTransaction(); + } +} diff --git a/SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/SpringContextLocator.java b/SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/SpringContextLocator.java new file mode 100644 index 0000000..8a82a3a --- /dev/null +++ b/SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/SpringContextLocator.java @@ -0,0 +1,57 @@ +package de.spring.example; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + + +/** + * + */ +public final class SpringContextLocator { + private static final Logger LOGGER = LoggerFactory.getLogger(SpringContextLocator.class); + + + // Singleton Pattern + private static SpringContextLocator instance; + + // Spring ApplicationContext + private static ApplicationContext context; + + // Spring Context + private static final String SPRING_CONFIG_CONTEXT="/spring-config.xml"; + + + /** + * Private constructor. Singleton pattern. + */ + private SpringContextLocator() { + String[] factoryFiles = null; + LOGGER.info("Loading context files: " + SpringContextLocator.SPRING_CONFIG_CONTEXT); + + factoryFiles = new String[] { SPRING_CONFIG_CONTEXT }; + + SpringContextLocator.context = new ClassPathXmlApplicationContext(factoryFiles); + + LOGGER.info("The context has been loaded successfully!! "); + } + + /** + * Singleton pattern not thread safety. To use SingletoHolder pattern as the best approximation + * otherwise to use an Enum class (see Effective Java Second Edition and ) if we need serialization. + */ + public static SpringContextLocator getInstance() { + if (SpringContextLocator.instance == null) { + SpringContextLocator.instance = new SpringContextLocator(); + } + return SpringContextLocator.instance; + } + + /** + * Return bean from application context. + */ + public Object getBean(final String name) { + return SpringContextLocator.context.getBean(name); + } +} \ No newline at end of file diff --git a/SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/SpringStart.java b/SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/SpringStart.java new file mode 100644 index 0000000..2aba4c2 --- /dev/null +++ b/SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/SpringStart.java @@ -0,0 +1,22 @@ +package de.spring.example; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import de.spring.example.web.Test; + +public class SpringStart { + private static final Logger LOGGER = LoggerFactory.getLogger(SpringStart.class); + + + /** + * @param args + */ + public static void main(String[] args) { + LOGGER.info("Starting application"); + SpringContextLocator.getInstance(); + + Test test = (Test) SpringContextLocator.getInstance().getBean("testOuter"); + test.myMethod(); + } +} diff --git a/SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/TransactionManager.java b/SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/TransactionManager.java new file mode 100644 index 0000000..53f3c59 --- /dev/null +++ b/SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/TransactionManager.java @@ -0,0 +1,48 @@ +package de.spring.example; + +import org.aspectj.lang.annotation.Aspect; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.transaction.TransactionStatus; + +@Aspect +public class TransactionManager { + private static final Logger LOGGER = LoggerFactory.getLogger(TransactionManager.class); + private static TransactionManager instance = new TransactionManager(); + private DataSourceTransactionManager transactionManager; + private TransactionStatus transactionStatus; + + //Why could you want to extend this class? + private TransactionManager() { + } + + public static TransactionManager getInstance() { + return instance; + } + + public void initTransaction() + { + LOGGER.info("initTRANSACTION"); + // transactionStatus = this.transactionManager.getTransaction(null); + } + + public void rollbackTransaction() + { + this.transactionManager.rollback(this.transactionStatus); + } + + + public void commitTransaction() + { + LOGGER.info("commitTRANSACTION"); + // this.transactionManager.commit(this.transactionStatus); + } + + + /************************* Setters and getters *******************************************/ + public void setTransactionManager(final DataSourceTransactionManager transactionManager) + { + this.transactionManager = transactionManager; + } +} diff --git a/SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/annotation/commitTransactional.java b/SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/annotation/commitTransactional.java new file mode 100644 index 0000000..1fda03f --- /dev/null +++ b/SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/annotation/commitTransactional.java @@ -0,0 +1,14 @@ +package de.spring.example.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.stereotype.Component; + +@Target({ElementType.METHOD, ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Component +public @interface commitTransactional { +} diff --git a/SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/annotation/initTransactional.java b/SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/annotation/initTransactional.java new file mode 100644 index 0000000..dad0b3e --- /dev/null +++ b/SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/annotation/initTransactional.java @@ -0,0 +1,14 @@ +package de.spring.example.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.stereotype.Component; + +@Target({ElementType.METHOD, ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Component +public @interface initTransactional { +} diff --git a/SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/web/Test.java b/SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/web/Test.java new file mode 100644 index 0000000..5cd7d2a --- /dev/null +++ b/SpringJava/AOP/SpringLTW/src/main/java/de/spring/example/web/Test.java @@ -0,0 +1,40 @@ +package de.spring.example.web; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import de.spring.example.annotation.commitTransactional; +import de.spring.example.annotation.initTransactional; + + +public class Test { + private static final Logger LOGGER = LoggerFactory.getLogger(Test.class); + + @initTransactional + public int myMethod() + { + LOGGER.info("The Advice should be run before."); + + annotatedPrivateMethod(); + + InnerTest innerTest = new InnerTest(); + innerTest.innerMethod(); + + return 666; + } + + public class InnerTest { + @commitTransactional + public void innerMethod() { + LOGGER.info("I am the inner class. The Advice should be run after." + + " NO NEED OF DECLARING Spring BEANS WHEN WEAVING!!!!"); + } + } + + // IT WORKS WHEN WEAVING!!! + @initTransactional + private void annotatedPrivateMethod() { + LOGGER.info("The Advice should be run before even with private methods because I AM WEAVING." + + " IT WORKS EVEN CALLING FROM METHOD OF THE SAME CLASS. It doesn't when using proxies AOP."); + } +} diff --git a/SpringJava/AOP/SpringLTW/src/main/resources/META-INF/aop.xml b/SpringJava/AOP/SpringLTW/src/main/resources/META-INF/aop.xml new file mode 100644 index 0000000..2345268 --- /dev/null +++ b/SpringJava/AOP/SpringLTW/src/main/resources/META-INF/aop.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + diff --git a/SpringJava/AOP/SpringLTW/src/main/resources/log4j2.xml b/SpringJava/AOP/SpringLTW/src/main/resources/log4j2.xml new file mode 100644 index 0000000..8b3fc3f --- /dev/null +++ b/SpringJava/AOP/SpringLTW/src/main/resources/log4j2.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SpringJava/AOP/SpringLTW/src/main/resources/spring-config.xml b/SpringJava/AOP/SpringLTW/src/main/resources/spring-config.xml new file mode 100644 index 0000000..ffb978c --- /dev/null +++ b/SpringJava/AOP/SpringLTW/src/main/resources/spring-config.xml @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SpringJava/AOP/SpringMavenWeaver/pom.xml b/SpringJava/AOP/SpringMavenWeaver/pom.xml new file mode 100644 index 0000000..79f8576 --- /dev/null +++ b/SpringJava/AOP/SpringMavenWeaver/pom.xml @@ -0,0 +1,256 @@ + + + + + 4.0.0 + de.spring.example + spring-maven-weaver + 2.0-SNAPSHOT + spring-maven-weaver + http://gumartinm.name + + UTF-8 + + + + + org.springframework + spring-context + 4.2.5.RELEASE + + + + commons-logging + commons-logging + + + + + org.springframework + spring-aspects + 4.2.5.RELEASE + + + + commons-logging + commons-logging + + + + + org.springframework + spring-aop + 4.2.5.RELEASE + + + + commons-logging + commons-logging + + + + + + javax.cache + cache-api + 1.0.0-PFD + + + javax.transaction + javax.transaction-api + 1.2 + + + org.springframework + spring-context-support + 4.2.5.RELEASE + + + + commons-logging + commons-logging + + + + + org.springframework + spring-jdbc + 4.2.5.RELEASE + + + + commons-logging + commons-logging + + + + + + + org.aspectj + aspectjrt + 1.8.9 + compile + + + + cglib + cglib + 3.2.1 + + + + com.mchange + c3p0 + 0.9.5.2 + + + mysql + mysql-connector-java + 5.1.38 + + + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.3 + + + + org.apache.logging.log4j + log4j-core + 2.3 + + + + org.slf4j + jcl-over-slf4j + 1.7.12 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + 1.8 + 1.8 + ${project.build.sourceEncoding} + + + + + default-compile + + + ${project.build.directory}/unwoven-classes + + + + + + + org.apache.maven.plugins + maven-resources-plugin + 2.7 + + ${project.build.sourceEncoding} + + + + + org.codehaus.mojo + aspectj-maven-plugin + 1.8 + + build.ajproperties + + ${project.build.directory}/unwoven-classes + + true + + + src/main/java + + de.spring.example.MyAdvice.java + + + **/logging/*.aj + + + + + + + + process-classes + + compile + + + + + + + + + + + + org.codehaus.mojo + aspectj-maven-plugin + 1.8 + + true + true + 1.5 + build.ajproperties + + + + + aspectj-report + + + + + + + + diff --git a/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/MyAdvice.java b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/MyAdvice.java new file mode 100644 index 0000000..6e25974 --- /dev/null +++ b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/MyAdvice.java @@ -0,0 +1,28 @@ +package de.spring.example; + +import org.aspectj.lang.annotation.After; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Aspect +public class MyAdvice { + private static final Logger LOGGER = LoggerFactory.getLogger(MyAdvice.class); + + // With execution we avoid double weaving (when call and when execution) + @Before("@annotation(de.spring.example.annotation.initTransactional) && execution(* *(..))") + public void initTransactional() + { + LOGGER.info("I am the Advice initTransaction."); + TransactionManager.getInstance().initTransaction(); + } + + + // With execution we avoid double weaving (when call and when execution) + @After("@annotation(de.spring.example.annotation.commitTransactional) && execution(* *(..))") + public void commitTransactional() { + LOGGER.info("I am the Advice commitTransaction."); + TransactionManager.getInstance().commitTransaction(); + } +} diff --git a/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/SpringContextLocator.java b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/SpringContextLocator.java new file mode 100644 index 0000000..8a82a3a --- /dev/null +++ b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/SpringContextLocator.java @@ -0,0 +1,57 @@ +package de.spring.example; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + + +/** + * + */ +public final class SpringContextLocator { + private static final Logger LOGGER = LoggerFactory.getLogger(SpringContextLocator.class); + + + // Singleton Pattern + private static SpringContextLocator instance; + + // Spring ApplicationContext + private static ApplicationContext context; + + // Spring Context + private static final String SPRING_CONFIG_CONTEXT="/spring-config.xml"; + + + /** + * Private constructor. Singleton pattern. + */ + private SpringContextLocator() { + String[] factoryFiles = null; + LOGGER.info("Loading context files: " + SpringContextLocator.SPRING_CONFIG_CONTEXT); + + factoryFiles = new String[] { SPRING_CONFIG_CONTEXT }; + + SpringContextLocator.context = new ClassPathXmlApplicationContext(factoryFiles); + + LOGGER.info("The context has been loaded successfully!! "); + } + + /** + * Singleton pattern not thread safety. To use SingletoHolder pattern as the best approximation + * otherwise to use an Enum class (see Effective Java Second Edition and ) if we need serialization. + */ + public static SpringContextLocator getInstance() { + if (SpringContextLocator.instance == null) { + SpringContextLocator.instance = new SpringContextLocator(); + } + return SpringContextLocator.instance; + } + + /** + * Return bean from application context. + */ + public Object getBean(final String name) { + return SpringContextLocator.context.getBean(name); + } +} \ No newline at end of file diff --git a/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/SpringStart.java b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/SpringStart.java new file mode 100644 index 0000000..2aba4c2 --- /dev/null +++ b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/SpringStart.java @@ -0,0 +1,22 @@ +package de.spring.example; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import de.spring.example.web.Test; + +public class SpringStart { + private static final Logger LOGGER = LoggerFactory.getLogger(SpringStart.class); + + + /** + * @param args + */ + public static void main(String[] args) { + LOGGER.info("Starting application"); + SpringContextLocator.getInstance(); + + Test test = (Test) SpringContextLocator.getInstance().getBean("testOuter"); + test.myMethod(); + } +} diff --git a/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/TransactionManager.java b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/TransactionManager.java new file mode 100644 index 0000000..53f3c59 --- /dev/null +++ b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/TransactionManager.java @@ -0,0 +1,48 @@ +package de.spring.example; + +import org.aspectj.lang.annotation.Aspect; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.transaction.TransactionStatus; + +@Aspect +public class TransactionManager { + private static final Logger LOGGER = LoggerFactory.getLogger(TransactionManager.class); + private static TransactionManager instance = new TransactionManager(); + private DataSourceTransactionManager transactionManager; + private TransactionStatus transactionStatus; + + //Why could you want to extend this class? + private TransactionManager() { + } + + public static TransactionManager getInstance() { + return instance; + } + + public void initTransaction() + { + LOGGER.info("initTRANSACTION"); + // transactionStatus = this.transactionManager.getTransaction(null); + } + + public void rollbackTransaction() + { + this.transactionManager.rollback(this.transactionStatus); + } + + + public void commitTransaction() + { + LOGGER.info("commitTRANSACTION"); + // this.transactionManager.commit(this.transactionStatus); + } + + + /************************* Setters and getters *******************************************/ + public void setTransactionManager(final DataSourceTransactionManager transactionManager) + { + this.transactionManager = transactionManager; + } +} diff --git a/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/annotation/commitTransactional.java b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/annotation/commitTransactional.java new file mode 100644 index 0000000..1fda03f --- /dev/null +++ b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/annotation/commitTransactional.java @@ -0,0 +1,14 @@ +package de.spring.example.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.stereotype.Component; + +@Target({ElementType.METHOD, ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Component +public @interface commitTransactional { +} diff --git a/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/annotation/initTransactional.java b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/annotation/initTransactional.java new file mode 100644 index 0000000..dad0b3e --- /dev/null +++ b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/annotation/initTransactional.java @@ -0,0 +1,14 @@ +package de.spring.example.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.stereotype.Component; + +@Target({ElementType.METHOD, ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Component +public @interface initTransactional { +} diff --git a/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/web/Test.java b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/web/Test.java new file mode 100644 index 0000000..5cd7d2a --- /dev/null +++ b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/web/Test.java @@ -0,0 +1,40 @@ +package de.spring.example.web; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import de.spring.example.annotation.commitTransactional; +import de.spring.example.annotation.initTransactional; + + +public class Test { + private static final Logger LOGGER = LoggerFactory.getLogger(Test.class); + + @initTransactional + public int myMethod() + { + LOGGER.info("The Advice should be run before."); + + annotatedPrivateMethod(); + + InnerTest innerTest = new InnerTest(); + innerTest.innerMethod(); + + return 666; + } + + public class InnerTest { + @commitTransactional + public void innerMethod() { + LOGGER.info("I am the inner class. The Advice should be run after." + + " NO NEED OF DECLARING Spring BEANS WHEN WEAVING!!!!"); + } + } + + // IT WORKS WHEN WEAVING!!! + @initTransactional + private void annotatedPrivateMethod() { + LOGGER.info("The Advice should be run before even with private methods because I AM WEAVING." + + " IT WORKS EVEN CALLING FROM METHOD OF THE SAME CLASS. It doesn't when using proxies AOP."); + } +} diff --git a/SpringJava/AOP/SpringMavenWeaver/src/main/resources/log4j2.xml b/SpringJava/AOP/SpringMavenWeaver/src/main/resources/log4j2.xml new file mode 100644 index 0000000..8b3fc3f --- /dev/null +++ b/SpringJava/AOP/SpringMavenWeaver/src/main/resources/log4j2.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SpringJava/AOP/SpringMavenWeaver/src/main/resources/spring-config.xml b/SpringJava/AOP/SpringMavenWeaver/src/main/resources/spring-config.xml new file mode 100644 index 0000000..ffb978c --- /dev/null +++ b/SpringJava/AOP/SpringMavenWeaver/src/main/resources/spring-config.xml @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SpringJava/SpringAOP20/pom.xml b/SpringJava/SpringAOP20/pom.xml deleted file mode 100644 index b43ac81..0000000 --- a/SpringJava/SpringAOP20/pom.xml +++ /dev/null @@ -1,124 +0,0 @@ - - - - - 4.0.0 - de.spring.example - spring-aop-example - 2.0-SNAPSHOT - spring-aop-example - http://gumartinm.name - - UTF-8 - - - - - org.springframework - spring-context - 4.2.5.RELEASE - - - - commons-logging - commons-logging - - - - - org.springframework - spring-aspects - 4.2.5.RELEASE - - - - commons-logging - commons-logging - - - - - org.springframework - spring-aop - 4.2.5.RELEASE - - - - commons-logging - commons-logging - - - - - - cglib - cglib - 3.2.1 - - - - - org.apache.logging.log4j - log4j-slf4j-impl - 2.3 - - - - org.apache.logging.log4j - log4j-core - 2.3 - - - - org.slf4j - jcl-over-slf4j - 1.7.12 - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.3 - - 1.8 - 1.8 - ${project.build.sourceEncoding} - - - - org.apache.maven.plugins - maven-resources-plugin - 2.7 - - ${project.build.sourceEncoding} - - - - - diff --git a/SpringJava/SpringAOP20/src/main/java/de/spring/example/GeneralAccess.java b/SpringJava/SpringAOP20/src/main/java/de/spring/example/GeneralAccess.java deleted file mode 100644 index ce30630..0000000 --- a/SpringJava/SpringAOP20/src/main/java/de/spring/example/GeneralAccess.java +++ /dev/null @@ -1,21 +0,0 @@ -package de.spring.example; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * - * This class is not using the AspectJ annotations, so we could use it on JDK 1.4 and below. - * If we want to use it as an Aspect we may JUST do it using a schema based declaration. - * What means, you can use this class as an Aspect JUST using an xml Spring declaration. - * - */ -public class GeneralAccess { - private static final Logger LOGGER = LoggerFactory.getLogger(GeneralAccess.class); - - - public void monitor() - { - LOGGER.info("I am the Advice monitor for TestB and I will be run before."); - } -} diff --git a/SpringJava/SpringAOP20/src/main/java/de/spring/example/SpringAdvice.java b/SpringJava/SpringAOP20/src/main/java/de/spring/example/SpringAdvice.java deleted file mode 100644 index 1555575..0000000 --- a/SpringJava/SpringAOP20/src/main/java/de/spring/example/SpringAdvice.java +++ /dev/null @@ -1,23 +0,0 @@ -package de.spring.example; - -import java.lang.reflect.Method; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.aop.MethodBeforeAdvice; - -/** - * - * We are using here an Advice of Spring 1.2 - * See: http://static.springsource.org/spring/docs/3.1.0.RC1/spring-framework-reference/html/aop-api.html#aop-api-advice-types - * - */ -public class SpringAdvice implements MethodBeforeAdvice { - private static final Logger LOGGER = LoggerFactory.getLogger(SpringAdvice.class); - - - public void before(Method m, Object[] args, Object target) throws Throwable { - LOGGER.info("I am the SpringAdvice and I will be run before."); - } - -} diff --git a/SpringJava/SpringAOP20/src/main/java/de/spring/example/SpringContextLocator.java b/SpringJava/SpringAOP20/src/main/java/de/spring/example/SpringContextLocator.java deleted file mode 100644 index d599c8f..0000000 --- a/SpringJava/SpringAOP20/src/main/java/de/spring/example/SpringContextLocator.java +++ /dev/null @@ -1,53 +0,0 @@ -package de.spring.example; - -import org.springframework.context.ApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - - -/** - * - */ -public final class SpringContextLocator { - - // Singleton Pattern - private static SpringContextLocator instance; - - // Spring ApplicationContext - private static ApplicationContext context; - - // Spring Context - private static final String SPRING_CONFIG_CONTEXT="spring-config.xml"; - - - /** - * Private constructor. Singleton pattern. - */ - private SpringContextLocator() { - String[] factoryFiles = null; - System.out.println("Loading context files: " + SpringContextLocator.SPRING_CONFIG_CONTEXT); - - factoryFiles = new String[] { SPRING_CONFIG_CONTEXT }; - - SpringContextLocator.context = new ClassPathXmlApplicationContext(factoryFiles); - - System.out.println("The context has been loaded successfully!! "); - } - - /** - * Singleton pattern not thread safety. To use SingletoHolder pattern as the best approximation - * otherwise to use an Enum class (see Effective Java Second Edition and ) if we need serialization. - */ - public static SpringContextLocator getInstance() { - if (SpringContextLocator.instance == null) { - SpringContextLocator.instance = new SpringContextLocator(); - } - return SpringContextLocator.instance; - } - - /** - * Return bean from application context. - */ - public Object getBean(final String name) { - return SpringContextLocator.context.getBean(name); - } -} \ No newline at end of file diff --git a/SpringJava/SpringAOP20/src/main/java/de/spring/example/SpringStart.java b/SpringJava/SpringAOP20/src/main/java/de/spring/example/SpringStart.java deleted file mode 100644 index c6ed090..0000000 --- a/SpringJava/SpringAOP20/src/main/java/de/spring/example/SpringStart.java +++ /dev/null @@ -1,25 +0,0 @@ -package de.spring.example; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import de.spring.example.service.TestB; -import de.spring.example.web.TestA; - -public class SpringStart { - private static final Logger LOGGER = LoggerFactory.getLogger(SpringStart.class); - - /** - * @param args - */ - public static void main(String[] args) { - LOGGER.info("Starting application"); - SpringContextLocator.getInstance(); - - TestA testA = (TestA) SpringContextLocator.getInstance().getBean("testA"); - testA.myMethod(); - - TestB testB = (TestB) SpringContextLocator.getInstance().getBean("testB"); - testB.myMethod(); - } -} diff --git a/SpringJava/SpringAOP20/src/main/java/de/spring/example/SystemArchitecture.java b/SpringJava/SpringAOP20/src/main/java/de/spring/example/SystemArchitecture.java deleted file mode 100644 index 822f240..0000000 --- a/SpringJava/SpringAOP20/src/main/java/de/spring/example/SystemArchitecture.java +++ /dev/null @@ -1,43 +0,0 @@ -package de.spring.example; - -import org.aspectj.lang.annotation.Pointcut; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * - * Using the @Aspect annotation you could use this class as an Aspect without - * using a schema based declaration (without using - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/SpringJava/SpringAOP20/src/main/resources/spring-config.xml b/SpringJava/SpringAOP20/src/main/resources/spring-config.xml deleted file mode 100644 index bda1080..0000000 --- a/SpringJava/SpringAOP20/src/main/resources/spring-config.xml +++ /dev/null @@ -1,80 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/SpringJava/SpringAspectJ/pom.xml b/SpringJava/SpringAspectJ/pom.xml deleted file mode 100644 index be48cff..0000000 --- a/SpringJava/SpringAspectJ/pom.xml +++ /dev/null @@ -1,124 +0,0 @@ - - - - - 4.0.0 - de.spring.example - spring-aspectj - 2.0-SNAPSHOT - spring-aspectj - http://gumartinm.name - - UTF-8 - - - - - org.springframework - spring-context - 4.2.5.RELEASE - - - - commons-logging - commons-logging - - - - - org.springframework - spring-aspects - 4.2.5.RELEASE - - - - commons-logging - commons-logging - - - - - org.springframework - spring-aop - 4.2.5.RELEASE - - - - commons-logging - commons-logging - - - - - - cglib - cglib - 3.2.1 - - - - - org.apache.logging.log4j - log4j-slf4j-impl - 2.3 - - - - org.apache.logging.log4j - log4j-core - 2.3 - - - - org.slf4j - jcl-over-slf4j - 1.7.12 - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.3 - - 1.8 - 1.8 - ${project.build.sourceEncoding} - - - - org.apache.maven.plugins - maven-resources-plugin - 2.7 - - ${project.build.sourceEncoding} - - - - - diff --git a/SpringJava/SpringAspectJ/src/main/java/de/spring/example/MyAdvice.java b/SpringJava/SpringAspectJ/src/main/java/de/spring/example/MyAdvice.java deleted file mode 100644 index 5121bba..0000000 --- a/SpringJava/SpringAspectJ/src/main/java/de/spring/example/MyAdvice.java +++ /dev/null @@ -1,44 +0,0 @@ -package de.spring.example; - -import org.aspectj.lang.ProceedingJoinPoint; -import org.aspectj.lang.annotation.Around; -import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.Before; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -@Aspect -public class MyAdvice { - private static final Logger LOGGER = LoggerFactory.getLogger(MyAdvice.class); - - - //This advice is connected with the Pointcut defined in SystemArchitecture. - //So, every method connected to that Pointcut will be run after the - //method defined in this Advice. - @Before("de.spring.example.SystemArchitecture.pointCutMethod())") - public void doAccessCheck() { - LOGGER.info("I am the Advice and I will be run before."); - } - - - //NOTICE: YOU DO NOT NEED TO CREATE A SPECIAL CLASS FOR POINTCUTS - // YOU COULD DEFINE AN ADVICE WITHOUT A POINTCUT - //This advice has a PointCut defined like execution(* de.spring.example.web.Test.anotherExample()) - //right here without a special PointCut method. This advice has itself the PointCut - @Around("execution(* de.spring.example.web.Test.anotherExample())") - public Object doAround(ProceedingJoinPoint pjp) { - LOGGER.info("I am the Advice and I will be run before and after. BEFORE"); - // start stopwatch - // This local variable will store the returned value from the method anotherExample() - Object retVal=null; - try { - //Calling the real method - retVal = pjp.proceed(); - } catch (Throwable e) { - LOGGER.info("Error", e); - } - // stop stopwatch - LOGGER.info("I am the Advice and I will be run before and after. AFTER " + retVal); - return retVal; - } -} diff --git a/SpringJava/SpringAspectJ/src/main/java/de/spring/example/SpringAdvice.java b/SpringJava/SpringAspectJ/src/main/java/de/spring/example/SpringAdvice.java deleted file mode 100644 index e5bdf66..0000000 --- a/SpringJava/SpringAspectJ/src/main/java/de/spring/example/SpringAdvice.java +++ /dev/null @@ -1,22 +0,0 @@ -package de.spring.example; - -import java.lang.reflect.Method; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.aop.MethodBeforeAdvice; - -/** - * - * We are using here an Advice of Spring 1.2 - * See: http://static.springsource.org/spring/docs/3.1.0.RC1/spring-framework-reference/html/aop-api.html#aop-api-advice-types - * - */ -public class SpringAdvice implements MethodBeforeAdvice { - private static final Logger LOGGER = LoggerFactory.getLogger(SpringAdvice.class); - - public void before(Method m, Object[] args, Object target) throws Throwable { - LOGGER.info("I am the SpringAdvice and I will be run before."); - } - -} diff --git a/SpringJava/SpringAspectJ/src/main/java/de/spring/example/SpringContextLocator.java b/SpringJava/SpringAspectJ/src/main/java/de/spring/example/SpringContextLocator.java deleted file mode 100644 index ff58276..0000000 --- a/SpringJava/SpringAspectJ/src/main/java/de/spring/example/SpringContextLocator.java +++ /dev/null @@ -1,57 +0,0 @@ -package de.spring.example; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.context.ApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - - -/** - * - */ -public final class SpringContextLocator { - private static final Logger LOGGER = LoggerFactory.getLogger(SpringAdvice.class); - - - // Singleton Pattern - private static SpringContextLocator instance; - - // Spring ApplicationContext - private static ApplicationContext context; - - // Spring Context - private static final String SPRING_CONFIG_CONTEXT="spring-config.xml"; - - - /** - * Private constructor. Singleton pattern. - */ - private SpringContextLocator() { - String[] factoryFiles = null; - LOGGER.info("Loading context files: " + SpringContextLocator.SPRING_CONFIG_CONTEXT); - - factoryFiles = new String[] { SPRING_CONFIG_CONTEXT }; - - SpringContextLocator.context = new ClassPathXmlApplicationContext(factoryFiles); - - LOGGER.info("The context has been loaded successfully!! "); - } - - /** - * Singleton pattern not thread safety. To use SingletoHolder pattern as the best approximation - * otherwise to use an Enum class (see Effective Java Second Edition and ) if we need serialization. - */ - public static SpringContextLocator getInstance() { - if (SpringContextLocator.instance == null) { - SpringContextLocator.instance = new SpringContextLocator(); - } - return SpringContextLocator.instance; - } - - /** - * Return bean from application context. - */ - public Object getBean(final String name) { - return SpringContextLocator.context.getBean(name); - } -} \ No newline at end of file diff --git a/SpringJava/SpringAspectJ/src/main/java/de/spring/example/SpringStart.java b/SpringJava/SpringAspectJ/src/main/java/de/spring/example/SpringStart.java deleted file mode 100644 index f553341..0000000 --- a/SpringJava/SpringAspectJ/src/main/java/de/spring/example/SpringStart.java +++ /dev/null @@ -1,22 +0,0 @@ -package de.spring.example; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import de.spring.example.web.Test; - -public class SpringStart { - private static final Logger LOGGER = LoggerFactory.getLogger(SpringStart.class); - - /** - * @param args - */ - public static void main(String[] args) { - LOGGER.info("Starting application"); - SpringContextLocator.getInstance(); - - Test test = (Test) SpringContextLocator.getInstance().getBean("test"); - test.myMethod(); - test.anotherExample(); - } -} diff --git a/SpringJava/SpringAspectJ/src/main/java/de/spring/example/SystemArchitecture.java b/SpringJava/SpringAspectJ/src/main/java/de/spring/example/SystemArchitecture.java deleted file mode 100644 index ef83b3d..0000000 --- a/SpringJava/SpringAspectJ/src/main/java/de/spring/example/SystemArchitecture.java +++ /dev/null @@ -1,53 +0,0 @@ -package de.spring.example; - -import org.aspectj.lang.annotation.AfterReturning; -import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.Pointcut; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * - * We are using here the @AspectJ annotations to declare - * Proxies. If we want to use these kinds of proxies on the Spring framework - * we have to use the annotation on the Spring xml file - * (the Spring context file) - */ -@Aspect -public class SystemArchitecture { - private static final Logger LOGGER = LoggerFactory.getLogger(SpringStart.class); - - - //Coonecting to the execution of any method defined in the - //package: de.spring.example.web - //We are connecting the methods defined in that package with this - //Pointcut. So, when executing any of those methods defined in that - //package we will run the Advice related to this Pointcut (if there is an Advice) - @Pointcut("execution(* de.spring.example.web.*.*(..))") - public void pointCutMethod() - { - LOGGER.info("I am the Pointcut and you will never see me."); - //This is the PointCut. - //You can write code here, but it will be useless because while running - //the methods connected to the Pointcut, this code will not be executed. - //Just the advice will run!!!! :/ - //Is not this weird? We are here defining a method whose code - //will never be run. When the hell should we write code here? - //This is a waste of time and code IMHO. Isn't it? - } - - //NOTICE: YOU DO NOT NEED TO CREATE A SPECIAL CLASS FOR THE ADVICE - // YOU COULD USE THE SAME CLASS FOR THE POINTCUTS AND FOR THE - // ADVICES. IN THIS CASE FOR EXAMPLE WE HAVE THE @AfterReturning - // ADVICE IN THIS CLASS AND THE @Before ADVICE IN THE CLASS CALLED - // MyAdvice - //This advice is connected with the another Pointcut. - //The returning value of every method connected to that Pointcut - //will be caught by this method. - @AfterReturning(pointcut="de.spring.example.SystemArchitecture.pointCutMethod())", - returning="retVal") - public void doAccessCheck(final Object retVal) { - LOGGER.info("The returned value by the method " + - "connected to the Pointcut: " + retVal); - } -} \ No newline at end of file diff --git a/SpringJava/SpringAspectJ/src/main/java/de/spring/example/web/Test.java b/SpringJava/SpringAspectJ/src/main/java/de/spring/example/web/Test.java deleted file mode 100644 index d01bd5d..0000000 --- a/SpringJava/SpringAspectJ/src/main/java/de/spring/example/web/Test.java +++ /dev/null @@ -1,30 +0,0 @@ -package de.spring.example.web; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class Test { - private static final Logger LOGGER = LoggerFactory.getLogger(Test.class); - - public int myMethod() - { - LOGGER.info("The Advice should be run before."); - - //This value will be caught by the Advice with the @AfterReturning annotation. - return 666; - } - - public int anotherExample() - { - LOGGER.info("The Advice should be run before and after."); - return 666; - - } - - public class InnerTest { - public void innerMethod() { - LOGGER.info("I am the inner class. The Advice should be run after." - + " NO NEED OF DECLARING Spring BEANS WHEN WEAVING!!!!"); - } - } -} diff --git a/SpringJava/SpringAspectJ/src/main/resources/log4j2.xml b/SpringJava/SpringAspectJ/src/main/resources/log4j2.xml deleted file mode 100644 index 8b3fc3f..0000000 --- a/SpringJava/SpringAspectJ/src/main/resources/log4j2.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/SpringJava/SpringAspectJ/src/main/resources/spring-config.xml b/SpringJava/SpringAspectJ/src/main/resources/spring-config.xml deleted file mode 100644 index 6aa0317..0000000 --- a/SpringJava/SpringAspectJ/src/main/resources/spring-config.xml +++ /dev/null @@ -1,59 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/SpringJava/SpringLTW/README b/SpringJava/SpringLTW/README deleted file mode 100644 index 9091fb8..0000000 --- a/SpringJava/SpringLTW/README +++ /dev/null @@ -1,3 +0,0 @@ -VM arguments: - --javaagent:$HOME/.m2/repository/org/springframework/spring-instrument/4.2.5.RELEASE/spring-instrument-4.2.5.RELEASE.jar diff --git a/SpringJava/SpringLTW/pom.xml b/SpringJava/SpringLTW/pom.xml deleted file mode 100644 index 5ebc4a0..0000000 --- a/SpringJava/SpringLTW/pom.xml +++ /dev/null @@ -1,190 +0,0 @@ - - - - - 4.0.0 - de.spring.example - spring-ltw - 2.0-SNAPSHOT - spring-ltw - http://gumartinm.name - - UTF-8 - - - - - org.springframework - spring-context - 4.2.5.RELEASE - - - - commons-logging - commons-logging - - - - - org.springframework - spring-aspects - 4.2.5.RELEASE - - - - commons-logging - commons-logging - - - - - org.springframework - spring-aop - 4.2.5.RELEASE - - - - commons-logging - commons-logging - - - - - org.springframework - spring-instrument - 4.2.5.RELEASE - - - - commons-logging - commons-logging - - - - - javax.cache - cache-api - 1.0.0-PFD - - - javax.transaction - javax.transaction-api - 1.2 - - - org.springframework - spring-context-support - 4.2.5.RELEASE - - - - commons-logging - commons-logging - - - - - org.springframework - spring-jdbc - 4.2.5.RELEASE - - - - commons-logging - commons-logging - - - - - - cglib - cglib - 3.2.1 - - - - com.mchange - c3p0 - 0.9.5.2 - - - mysql - mysql-connector-java - 5.1.38 - - - - - org.apache.logging.log4j - log4j-slf4j-impl - 2.3 - - - - org.apache.logging.log4j - log4j-core - 2.3 - - - - org.slf4j - jcl-over-slf4j - 1.7.12 - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.3 - - 1.8 - 1.8 - ${project.build.sourceEncoding} - - - - org.apache.maven.plugins - maven-resources-plugin - 2.7 - - ${project.build.sourceEncoding} - - - - - diff --git a/SpringJava/SpringLTW/src/main/java/de/spring/example/MyAdvice.java b/SpringJava/SpringLTW/src/main/java/de/spring/example/MyAdvice.java deleted file mode 100644 index 6e25974..0000000 --- a/SpringJava/SpringLTW/src/main/java/de/spring/example/MyAdvice.java +++ /dev/null @@ -1,28 +0,0 @@ -package de.spring.example; - -import org.aspectj.lang.annotation.After; -import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.Before; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -@Aspect -public class MyAdvice { - private static final Logger LOGGER = LoggerFactory.getLogger(MyAdvice.class); - - // With execution we avoid double weaving (when call and when execution) - @Before("@annotation(de.spring.example.annotation.initTransactional) && execution(* *(..))") - public void initTransactional() - { - LOGGER.info("I am the Advice initTransaction."); - TransactionManager.getInstance().initTransaction(); - } - - - // With execution we avoid double weaving (when call and when execution) - @After("@annotation(de.spring.example.annotation.commitTransactional) && execution(* *(..))") - public void commitTransactional() { - LOGGER.info("I am the Advice commitTransaction."); - TransactionManager.getInstance().commitTransaction(); - } -} diff --git a/SpringJava/SpringLTW/src/main/java/de/spring/example/SpringContextLocator.java b/SpringJava/SpringLTW/src/main/java/de/spring/example/SpringContextLocator.java deleted file mode 100644 index 8a82a3a..0000000 --- a/SpringJava/SpringLTW/src/main/java/de/spring/example/SpringContextLocator.java +++ /dev/null @@ -1,57 +0,0 @@ -package de.spring.example; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.context.ApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - - -/** - * - */ -public final class SpringContextLocator { - private static final Logger LOGGER = LoggerFactory.getLogger(SpringContextLocator.class); - - - // Singleton Pattern - private static SpringContextLocator instance; - - // Spring ApplicationContext - private static ApplicationContext context; - - // Spring Context - private static final String SPRING_CONFIG_CONTEXT="/spring-config.xml"; - - - /** - * Private constructor. Singleton pattern. - */ - private SpringContextLocator() { - String[] factoryFiles = null; - LOGGER.info("Loading context files: " + SpringContextLocator.SPRING_CONFIG_CONTEXT); - - factoryFiles = new String[] { SPRING_CONFIG_CONTEXT }; - - SpringContextLocator.context = new ClassPathXmlApplicationContext(factoryFiles); - - LOGGER.info("The context has been loaded successfully!! "); - } - - /** - * Singleton pattern not thread safety. To use SingletoHolder pattern as the best approximation - * otherwise to use an Enum class (see Effective Java Second Edition and ) if we need serialization. - */ - public static SpringContextLocator getInstance() { - if (SpringContextLocator.instance == null) { - SpringContextLocator.instance = new SpringContextLocator(); - } - return SpringContextLocator.instance; - } - - /** - * Return bean from application context. - */ - public Object getBean(final String name) { - return SpringContextLocator.context.getBean(name); - } -} \ No newline at end of file diff --git a/SpringJava/SpringLTW/src/main/java/de/spring/example/SpringStart.java b/SpringJava/SpringLTW/src/main/java/de/spring/example/SpringStart.java deleted file mode 100644 index 2aba4c2..0000000 --- a/SpringJava/SpringLTW/src/main/java/de/spring/example/SpringStart.java +++ /dev/null @@ -1,22 +0,0 @@ -package de.spring.example; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import de.spring.example.web.Test; - -public class SpringStart { - private static final Logger LOGGER = LoggerFactory.getLogger(SpringStart.class); - - - /** - * @param args - */ - public static void main(String[] args) { - LOGGER.info("Starting application"); - SpringContextLocator.getInstance(); - - Test test = (Test) SpringContextLocator.getInstance().getBean("testOuter"); - test.myMethod(); - } -} diff --git a/SpringJava/SpringLTW/src/main/java/de/spring/example/TransactionManager.java b/SpringJava/SpringLTW/src/main/java/de/spring/example/TransactionManager.java deleted file mode 100644 index 53f3c59..0000000 --- a/SpringJava/SpringLTW/src/main/java/de/spring/example/TransactionManager.java +++ /dev/null @@ -1,48 +0,0 @@ -package de.spring.example; - -import org.aspectj.lang.annotation.Aspect; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.jdbc.datasource.DataSourceTransactionManager; -import org.springframework.transaction.TransactionStatus; - -@Aspect -public class TransactionManager { - private static final Logger LOGGER = LoggerFactory.getLogger(TransactionManager.class); - private static TransactionManager instance = new TransactionManager(); - private DataSourceTransactionManager transactionManager; - private TransactionStatus transactionStatus; - - //Why could you want to extend this class? - private TransactionManager() { - } - - public static TransactionManager getInstance() { - return instance; - } - - public void initTransaction() - { - LOGGER.info("initTRANSACTION"); - // transactionStatus = this.transactionManager.getTransaction(null); - } - - public void rollbackTransaction() - { - this.transactionManager.rollback(this.transactionStatus); - } - - - public void commitTransaction() - { - LOGGER.info("commitTRANSACTION"); - // this.transactionManager.commit(this.transactionStatus); - } - - - /************************* Setters and getters *******************************************/ - public void setTransactionManager(final DataSourceTransactionManager transactionManager) - { - this.transactionManager = transactionManager; - } -} diff --git a/SpringJava/SpringLTW/src/main/java/de/spring/example/annotation/commitTransactional.java b/SpringJava/SpringLTW/src/main/java/de/spring/example/annotation/commitTransactional.java deleted file mode 100644 index 1fda03f..0000000 --- a/SpringJava/SpringLTW/src/main/java/de/spring/example/annotation/commitTransactional.java +++ /dev/null @@ -1,14 +0,0 @@ -package de.spring.example.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import org.springframework.stereotype.Component; - -@Target({ElementType.METHOD, ElementType.TYPE}) -@Retention(RetentionPolicy.RUNTIME) -@Component -public @interface commitTransactional { -} diff --git a/SpringJava/SpringLTW/src/main/java/de/spring/example/annotation/initTransactional.java b/SpringJava/SpringLTW/src/main/java/de/spring/example/annotation/initTransactional.java deleted file mode 100644 index dad0b3e..0000000 --- a/SpringJava/SpringLTW/src/main/java/de/spring/example/annotation/initTransactional.java +++ /dev/null @@ -1,14 +0,0 @@ -package de.spring.example.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import org.springframework.stereotype.Component; - -@Target({ElementType.METHOD, ElementType.TYPE}) -@Retention(RetentionPolicy.RUNTIME) -@Component -public @interface initTransactional { -} diff --git a/SpringJava/SpringLTW/src/main/java/de/spring/example/web/Test.java b/SpringJava/SpringLTW/src/main/java/de/spring/example/web/Test.java deleted file mode 100644 index 5cd7d2a..0000000 --- a/SpringJava/SpringLTW/src/main/java/de/spring/example/web/Test.java +++ /dev/null @@ -1,40 +0,0 @@ -package de.spring.example.web; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import de.spring.example.annotation.commitTransactional; -import de.spring.example.annotation.initTransactional; - - -public class Test { - private static final Logger LOGGER = LoggerFactory.getLogger(Test.class); - - @initTransactional - public int myMethod() - { - LOGGER.info("The Advice should be run before."); - - annotatedPrivateMethod(); - - InnerTest innerTest = new InnerTest(); - innerTest.innerMethod(); - - return 666; - } - - public class InnerTest { - @commitTransactional - public void innerMethod() { - LOGGER.info("I am the inner class. The Advice should be run after." - + " NO NEED OF DECLARING Spring BEANS WHEN WEAVING!!!!"); - } - } - - // IT WORKS WHEN WEAVING!!! - @initTransactional - private void annotatedPrivateMethod() { - LOGGER.info("The Advice should be run before even with private methods because I AM WEAVING." - + " IT WORKS EVEN CALLING FROM METHOD OF THE SAME CLASS. It doesn't when using proxies AOP."); - } -} diff --git a/SpringJava/SpringLTW/src/main/resources/META-INF/aop.xml b/SpringJava/SpringLTW/src/main/resources/META-INF/aop.xml deleted file mode 100644 index 2345268..0000000 --- a/SpringJava/SpringLTW/src/main/resources/META-INF/aop.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - - - - diff --git a/SpringJava/SpringLTW/src/main/resources/log4j2.xml b/SpringJava/SpringLTW/src/main/resources/log4j2.xml deleted file mode 100644 index 8b3fc3f..0000000 --- a/SpringJava/SpringLTW/src/main/resources/log4j2.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/SpringJava/SpringLTW/src/main/resources/spring-config.xml b/SpringJava/SpringLTW/src/main/resources/spring-config.xml deleted file mode 100644 index ffb978c..0000000 --- a/SpringJava/SpringLTW/src/main/resources/spring-config.xml +++ /dev/null @@ -1,68 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- 2.1.4