From: Gustavo Martin Morcuende Date: Sun, 10 Apr 2016 23:56:12 +0000 (+0200) Subject: Spring AOP: improves in old examples about AOP Spring old way, AspectJ and LTW. X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=2ad1f7783a1b74acf539d6be84c75ef33d016628;p=JavaForFun Spring AOP: improves in old examples about AOP Spring old way, AspectJ and LTW. --- diff --git a/SpringJava/JPA/pom.xml b/SpringJava/JPA/pom.xml new file mode 100644 index 0000000..d91a5b4 --- /dev/null +++ b/SpringJava/JPA/pom.xml @@ -0,0 +1,239 @@ + + 4.0.0 + de.spring.jpa + spring-jpa + 1.0-SNAPSHOT + spring-jpa + http://gumartinm.name + JPA Spring Framework + + Gustavo Martin Morcuende + http://www.gumartinm.name + + + scm:git:http://git.gumartinm.name/SpringWebServicesForFun + http://git.gumartinm.name/SpringWebServicesForFun + + + + UTF-8 + UTF-8 + 4.2.4.RELEASE + + + + + release + + release + + + true + + + + + + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.5 + + + + org.apache.logging.log4j + log4j-core + 2.5 + + + + org.slf4j + jcl-over-slf4j + 1.7.13 + + + + org.springframework + spring-context + ${spring.version} + + + + commons-logging + commons-logging + + + + + + cglib + cglib + 2.2.2 + + + + com.mchange + c3p0 + 0.9.5.2 + + + + + org.springframework + spring-orm + ${spring.version} + + + + org.springframework.data + spring-data-jpa + 1.9.2.RELEASE + + + org.springframework + spring-beans + + + org.springframework + spring-jdbc + + + org.springframework + spring-orm + + + org.springframework + spring-core + + + org.springframework + spring-aop + + + org.springframework + spring-context + + + + + + org.hibernate + hibernate-entitymanager + 5.0.7.Final + + + + + + junit + junit + 4.12 + test + + + org.springframework + spring-test + ${spring.version} + test + + + org.mockito + mockito-core + 2.0.11-beta + test + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.18.1 + + + org.apache.maven.plugins + maven-failsafe-plugin + 2.18.1 + + + + 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} + + + + org.apache.maven.plugins + maven-jar-plugin + 2.4 + + + + ${project.description} + ${project.version} + ${project.organization.name} + ${project.description} + ${project.version} + ${project.organization.name} + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*IntegrationTest.java + + + + + org.apache.maven.plugins + maven-failsafe-plugin + + + + integration-test + verify + + + + + + **/*IntegrationTest.java + + + + + + diff --git a/SpringJava/JPA/src/main/resources/jpa.properties b/SpringJava/JPA/src/main/resources/jpa.properties new file mode 100644 index 0000000..f0755b8 --- /dev/null +++ b/SpringJava/JPA/src/main/resources/jpa.properties @@ -0,0 +1,2 @@ +jpa.dialect=org.hibernate.dialect.DB2Dialect +jpa.show_sql=true \ No newline at end of file diff --git a/SpringJava/JPA/src/main/resources/log4j2.xml b/SpringJava/JPA/src/main/resources/log4j2.xml new file mode 100644 index 0000000..d684e48 --- /dev/null +++ b/SpringJava/JPA/src/main/resources/log4j2.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SpringJava/JPA/src/main/resources/spring-configuration/datasource-configuration.xml b/SpringJava/JPA/src/main/resources/spring-configuration/datasource-configuration.xml new file mode 100644 index 0000000..0951744 --- /dev/null +++ b/SpringJava/JPA/src/main/resources/spring-configuration/datasource-configuration.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SpringJava/JPA/src/main/resources/spring-configuration/jpa-configuration.xml b/SpringJava/JPA/src/main/resources/spring-configuration/jpa-configuration.xml new file mode 100644 index 0000000..4381156 --- /dev/null +++ b/SpringJava/JPA/src/main/resources/spring-configuration/jpa-configuration.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SpringJava/SpringAOP20/pom.xml b/SpringJava/SpringAOP20/pom.xml index 03f9331..b43ac81 100644 --- a/SpringJava/SpringAOP20/pom.xml +++ b/SpringJava/SpringAOP20/pom.xml @@ -1,663 +1,124 @@ - + + 4.0.0 - de.spring.example - spring-aop - 2.0-SNAPSHOT - spring-aop - http://www.myorganization.de - Spring AspectJ with Spring Libraries - - MyOrganization - http://www.myorganization.de - - pom - - trac - http://noserver/ - - - jenkins - http://noserver/jenkins/ - - - scm:svn:http://noserver - http://noserver - - - - com.sun.jdmk - jmxtools - 1.2.1 - - - javax.activation - activation - 1.1 - - - log4j - log4j - 1.2.15 - - - com.sun.jdmk - jmxtools - - - com.sun.jmx - jmxri - - - javax.mail - mail - - - javax.jms - jms - - - - - junit - junit - 4.4 - test - - - - - - c3p0 - c3p0 - 0.9.1.2 - - - cglib - cglib-nodep - 2.1_3 - - - commons-collections - commons-collections - 3.2.1 - - - commons-configuration - commons-configuration - 1.6 - - - commons-dbcp - commons-dbcp - 1.2.2 - - - commons-io - commons-io - 1.4 - - - commons-lang - commons-lang - 2.4 - - - commons-logging - commons-logging - 1.1.1 - - - commons-net - commons-net - 2.0 - - - commons-pool - commons-pool - 1.3 - - - com.h2database - h2 - 1.2.130 - - - dom4j - dom4j - 1.6.1 - - - xml-apis - xml-apis - - - - - hsqldb - hsqldb - 1.8.0.7 - - - javatar - javatar - 2.5 - - - jpos - jpos - 1.12.2 - provided - - - jpos - jpos-controls - 1.12.2 - provided - - - org.python - jython - 2.5.2b2 - - - urbanophile - java-getopt - 1.0.13 - - - mysql - mysql-connector-java - 5.1.6 - - - org.apache.ibatis - ibatis-sqlmap - 2.3.4.726 - - - org.apache.mina - mina-core - 2.0.0-M6 - - - org.aspectj - aspectjrt - 1.6.5 - - - org.aspectj - aspectjweaver - 1.6.5 - - - org.dbunit - dbunit - 2.4.4 - test - - - org.eclipse.jetty - jetty-continuation - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-http - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-io - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-security - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-server - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-servlet - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-webapp - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-util - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-xml - 7.0.0.v20091005 - - - org.slf4j - slf4j-api - 1.5.2 - - - org.slf4j - slf4j-log4j12 - 1.5.2 - - - log4j - log4j - - - - - org.springframework - spring-aop - ${spring.version} - - - org.springframework - spring-beans - ${spring.version} - - - org.springframework - spring-core - ${spring.version} - - - org.springframework - spring-context - ${spring.version} - - - org.springframework - spring-jdbc - ${spring.version} - - - org.springframework - spring-tx - ${spring.version} - - - org.springframework - spring-web - ${spring.version} - - - org.springframework - spring-webmvc - ${spring.version} - - - org.springframework - spring-context-support - ${spring.version} - - - org.springframework - spring-test - ${spring.version} - test - - - org.springframework.batch - spring-batch-test - 2.0.4.RELEASE - - - org.springframework.batch - spring-batch-core - 2.0.4.RELEASE - - - org.springframework.batch - spring-batch-infrastructure - 2.0.4.RELEASE - - - org.springframework - spring-orm - ${spring.version} - - - net.sf.ehcache - ehcache - 1.6.2 - - - org.springmodules - spring-modules-cache - 0.9 - - - p6spy - p6spy - 1.3 - - - javax.transaction - jta - 1.0.1B - - - javax.servlet - servlet-api - 2.5 - - - com.caucho - hessian - 3.1.6 - - - org.codehaus.jettison - jettison - 1.0 - - - com.thoughtworks.xstream - xstream - 1.3 - - - org.ini4j - ini4j - 0.5.1 - - - org.easymock - easymock - 2.4 - test - - - org.easymock - easymockclassextension - 2.4 - test - + 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 + + + - - org.apache.commons - commons-compress - 1.0 - - - org.apache.commons - commons-math - 2.0 - - - dtgjpos_forms - dtgjpos_forms - 1.4.12 - provided - - - org.codehaus.castor - castor-xml - 1.3.1 - - - org.apache.xmlbeans - xmlbeans - 2.5.0 - - - org.apache.velocity.tools - velocity-tools-generic - 1.4 - - - org.apache.velocity - velocity - 1.6.2 - - - org.tmatesoft.svnkit - svnkit - 1.3.1 - - - - - spring-aop-example - - - - - org.apache.maven.plugins - maven-compiler-plugin - 2.0.2 - - 1.6 - 1.6 - ${project.build.sourceEncoding} - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.6 - - - org.apache.maven.plugins - maven-surefire-plugin - 2.4.2 - - true - - - - org.codehaus.mojo - jdepend-maven-plugin - 2.0-beta-2 - - - org.apache.maven.plugins - maven-jar-plugin - 2.3.1 - - - - ${project.description} - ${project.version} - ${project.organization.name} - ${project.description} - ${project.version} - ${project.organization.name} - ${BUILD_TAG} - ${BUILD_ID} - ${BUILD_NUMBER} - ${prefix.committedRevision} - ${prefix.repository} - ${prefix.path} - - - - - - com.google.code.maven-svn-revision-number-plugin - maven-svn-revision-number-plugin - 1.6 - - - - revision - - - - - - - prefix - - - - - - - - - - releases - releases - http://noserver/artifactory/custom-annotations-libs-releases-local - - - snapshots-releases - snapshots-releases - http://noserver/artifactory/custom-annotations-libs-snapshots-local - - - noserver - file:///mnt/sdb1/data/downloads/jenkins/ - + + cglib + cglib + 3.2.1 + - - - - - org.apache.maven.plugins - maven-project-info-reports-plugin - 2.1.2 - - - - index - dependencies - cim - issue-tracking - scm - summary - project-team - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.6.1 - - - html - - MYPROJECT API for ${project.name} ${project.version} - MYPROJECT API for ${project.name} ${project.version} - - - javadoc - aggregate - - - - - - org.codehaus.mojo - taglist-maven-plugin - 2.3 - - - TODO - @todo - FIXME - XXX - - - - - org.apache.maven.plugins - maven-surefire-report-plugin - 2.4.3 - - - integration-tests - - report-only - - - failsafe-report - - target/failsafe-reports - - - - - junit-tests - - report-only - - - surefire-report - - target/surefire-reports - - - - - - - - - UTF-8 - 2.5.6 - - - - - - - + + + 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/spring-aop-example/pom.xml b/SpringJava/SpringAOP20/spring-aop-example/pom.xml deleted file mode 100644 index fc21237..0000000 --- a/SpringJava/SpringAOP20/spring-aop-example/pom.xml +++ /dev/null @@ -1,38 +0,0 @@ - - - - - 4.0.0 - - spring-aop - de.spring.example - 2.0-SNAPSHOT - - - spring-aop-example - spring-aop-example - http://maven.apache.org - - - - org.springframework - spring-aspects - 2.5.6 - - - org.springframework - spring-context - - - cglib - cglib-nodep - - - org.springframework - spring-aop - - - diff --git a/SpringJava/SpringAOP20/spring-aop-example/src/main/java/de/spring/example/GeneralAccess.java b/SpringJava/SpringAOP20/spring-aop-example/src/main/java/de/spring/example/GeneralAccess.java deleted file mode 100644 index 3f07587..0000000 --- a/SpringJava/SpringAOP20/spring-aop-example/src/main/java/de/spring/example/GeneralAccess.java +++ /dev/null @@ -1,17 +0,0 @@ -package de.spring.example; - - -/** - * - * 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 { - - public void monitor() - { - System.out.println("I am the Advice monitor for TestB and I will be run before."); - } -} diff --git a/SpringJava/SpringAOP20/spring-aop-example/src/main/java/de/spring/example/SpringAdvice.java b/SpringJava/SpringAOP20/spring-aop-example/src/main/java/de/spring/example/SpringAdvice.java deleted file mode 100644 index 90e345e..0000000 --- a/SpringJava/SpringAOP20/spring-aop-example/src/main/java/de/spring/example/SpringAdvice.java +++ /dev/null @@ -1,18 +0,0 @@ -package de.spring.example; - -import java.lang.reflect.Method; -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 { - - public void before(Method m, Object[] args, Object target) throws Throwable { - System.out.println("I am the SpringAdvice and I will be run before."); - } - -} diff --git a/SpringJava/SpringAOP20/spring-aop-example/src/main/java/de/spring/example/SpringContextLocator.java b/SpringJava/SpringAOP20/spring-aop-example/src/main/java/de/spring/example/SpringContextLocator.java deleted file mode 100644 index d599c8f..0000000 --- a/SpringJava/SpringAOP20/spring-aop-example/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/spring-aop-example/src/main/java/de/spring/example/SpringStart.java b/SpringJava/SpringAOP20/spring-aop-example/src/main/java/de/spring/example/SpringStart.java deleted file mode 100644 index 2cf4a1a..0000000 --- a/SpringJava/SpringAOP20/spring-aop-example/src/main/java/de/spring/example/SpringStart.java +++ /dev/null @@ -1,21 +0,0 @@ -package de.spring.example; - -import de.spring.example.service.TestB; -import de.spring.example.web.TestA; - -public class SpringStart { - - /** - * @param args - */ - public static void main(String[] args) { - System.out.println("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/spring-aop-example/src/main/java/de/spring/example/SystemArchitecture.java b/SpringJava/SpringAOP20/spring-aop-example/src/main/java/de/spring/example/SystemArchitecture.java deleted file mode 100644 index f9511b2..0000000 --- a/SpringJava/SpringAOP20/spring-aop-example/src/main/java/de/spring/example/SystemArchitecture.java +++ /dev/null @@ -1,39 +0,0 @@ -package de.spring.example; - -import org.aspectj.lang.annotation.Pointcut; - -/** - * - * 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/java/de/spring/example/GeneralAccess.java b/SpringJava/SpringAOP20/src/main/java/de/spring/example/GeneralAccess.java new file mode 100644 index 0000000..ce30630 --- /dev/null +++ b/SpringJava/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/SpringAOP20/src/main/java/de/spring/example/SpringAdvice.java b/SpringJava/SpringAOP20/src/main/java/de/spring/example/SpringAdvice.java new file mode 100644 index 0000000..1555575 --- /dev/null +++ b/SpringJava/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/SpringAOP20/src/main/java/de/spring/example/SpringContextLocator.java b/SpringJava/SpringAOP20/src/main/java/de/spring/example/SpringContextLocator.java new file mode 100644 index 0000000..d599c8f --- /dev/null +++ b/SpringJava/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/SpringAOP20/src/main/java/de/spring/example/SpringStart.java b/SpringJava/SpringAOP20/src/main/java/de/spring/example/SpringStart.java new file mode 100644 index 0000000..c6ed090 --- /dev/null +++ b/SpringJava/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/SpringAOP20/src/main/java/de/spring/example/SystemArchitecture.java b/SpringJava/SpringAOP20/src/main/java/de/spring/example/SystemArchitecture.java new file mode 100644 index 0000000..822f240 --- /dev/null +++ b/SpringJava/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/SpringAOP20/src/main/resources/spring-config.xml b/SpringJava/SpringAOP20/src/main/resources/spring-config.xml new file mode 100644 index 0000000..bda1080 --- /dev/null +++ b/SpringJava/SpringAOP20/src/main/resources/spring-config.xml @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SpringJava/SpringAspectJ/pom.xml b/SpringJava/SpringAspectJ/pom.xml index 61dfe24..be48cff 100644 --- a/SpringJava/SpringAspectJ/pom.xml +++ b/SpringJava/SpringAspectJ/pom.xml @@ -1,663 +1,124 @@ - + + 4.0.0 - de.spring.example + de.spring.example spring-aspectj - 2.0-SNAPSHOT - spring-aspect - http://www.myorganization.de - Spring AspectJ with Spring Libraries - - MyOrganization - http://www.myorganization.de - - pom - - trac - http://noserver/ - - - jenkins - http://noserver/jenkins/ - - - scm:svn:http://noserver - http://noserver - - - - com.sun.jdmk - jmxtools - 1.2.1 - - - javax.activation - activation - 1.1 - - - log4j - log4j - 1.2.15 - - - com.sun.jdmk - jmxtools - - - com.sun.jmx - jmxri - - - javax.mail - mail - - - javax.jms - jms - - - - - junit - junit - 4.4 - test - - - - - - c3p0 - c3p0 - 0.9.1.2 - - - cglib - cglib-nodep - 2.1_3 - - - commons-collections - commons-collections - 3.2.1 - - - commons-configuration - commons-configuration - 1.6 - - - commons-dbcp - commons-dbcp - 1.2.2 - - - commons-io - commons-io - 1.4 - - - commons-lang - commons-lang - 2.4 - - - commons-logging - commons-logging - 1.1.1 - - - commons-net - commons-net - 2.0 - - - commons-pool - commons-pool - 1.3 - - - com.h2database - h2 - 1.2.130 - - - dom4j - dom4j - 1.6.1 - - - xml-apis - xml-apis - - - - - hsqldb - hsqldb - 1.8.0.7 - - - javatar - javatar - 2.5 - - - jpos - jpos - 1.12.2 - provided - - - jpos - jpos-controls - 1.12.2 - provided - - - org.python - jython - 2.5.2b2 - - - urbanophile - java-getopt - 1.0.13 - - - mysql - mysql-connector-java - 5.1.6 - - - org.apache.ibatis - ibatis-sqlmap - 2.3.4.726 - - - org.apache.mina - mina-core - 2.0.0-M6 - - - org.aspectj - aspectjrt - 1.6.5 - - - org.aspectj - aspectjweaver - 1.6.5 - - - org.dbunit - dbunit - 2.4.4 - test - - - org.eclipse.jetty - jetty-continuation - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-http - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-io - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-security - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-server - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-servlet - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-webapp - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-util - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-xml - 7.0.0.v20091005 - - - org.slf4j - slf4j-api - 1.5.2 - - - org.slf4j - slf4j-log4j12 - 1.5.2 - - - log4j - log4j - - - - - org.springframework - spring-aop - ${spring.version} - - - org.springframework - spring-beans - ${spring.version} - - - org.springframework - spring-core - ${spring.version} - - - org.springframework - spring-context - ${spring.version} - - - org.springframework - spring-jdbc - ${spring.version} - - - org.springframework - spring-tx - ${spring.version} - - - org.springframework - spring-web - ${spring.version} - - - org.springframework - spring-webmvc - ${spring.version} - - - org.springframework - spring-context-support - ${spring.version} - - - org.springframework - spring-test - ${spring.version} - test - - - org.springframework.batch - spring-batch-test - 2.0.4.RELEASE - - - org.springframework.batch - spring-batch-core - 2.0.4.RELEASE - - - org.springframework.batch - spring-batch-infrastructure - 2.0.4.RELEASE - - - org.springframework - spring-orm - ${spring.version} - - - net.sf.ehcache - ehcache - 1.6.2 - - - org.springmodules - spring-modules-cache - 0.9 - - - p6spy - p6spy - 1.3 - - - javax.transaction - jta - 1.0.1B - - - javax.servlet - servlet-api - 2.5 - - - com.caucho - hessian - 3.1.6 - - - org.codehaus.jettison - jettison - 1.0 - - - com.thoughtworks.xstream - xstream - 1.3 - - - org.ini4j - ini4j - 0.5.1 - - - org.easymock - easymock - 2.4 - test - - - org.easymock - easymockclassextension - 2.4 - test - + 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 + + + - - org.apache.commons - commons-compress - 1.0 - - - org.apache.commons - commons-math - 2.0 - - - dtgjpos_forms - dtgjpos_forms - 1.4.12 - provided - - - org.codehaus.castor - castor-xml - 1.3.1 - - - org.apache.xmlbeans - xmlbeans - 2.5.0 - - - org.apache.velocity.tools - velocity-tools-generic - 1.4 - - - org.apache.velocity - velocity - 1.6.2 - - - org.tmatesoft.svnkit - svnkit - 1.3.1 - - - - - spring-aspectj-example - - - - - org.apache.maven.plugins - maven-compiler-plugin - 2.0.2 - - 1.6 - 1.6 - ${project.build.sourceEncoding} - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.6 - - - org.apache.maven.plugins - maven-surefire-plugin - 2.4.2 - - true - - - - org.codehaus.mojo - jdepend-maven-plugin - 2.0-beta-2 - - - org.apache.maven.plugins - maven-jar-plugin - 2.3.1 - - - - ${project.description} - ${project.version} - ${project.organization.name} - ${project.description} - ${project.version} - ${project.organization.name} - ${BUILD_TAG} - ${BUILD_ID} - ${BUILD_NUMBER} - ${prefix.committedRevision} - ${prefix.repository} - ${prefix.path} - - - - - - com.google.code.maven-svn-revision-number-plugin - maven-svn-revision-number-plugin - 1.6 - - - - revision - - - - - - - prefix - - - - - - - - - - releases - releases - http://noserver/artifactory/custom-annotations-libs-releases-local - - - snapshots-releases - snapshots-releases - http://noserver/artifactory/custom-annotations-libs-snapshots-local - - - noserver - file:///mnt/sdb1/data/downloads/jenkins/ - + + cglib + cglib + 3.2.1 + - - - - - org.apache.maven.plugins - maven-project-info-reports-plugin - 2.1.2 - - - - index - dependencies - cim - issue-tracking - scm - summary - project-team - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.6.1 - - - html - - MYPROJECT API for ${project.name} ${project.version} - MYPROJECT API for ${project.name} ${project.version} - - - javadoc - aggregate - - - - - - org.codehaus.mojo - taglist-maven-plugin - 2.3 - - - TODO - @todo - FIXME - XXX - - - - - org.apache.maven.plugins - maven-surefire-report-plugin - 2.4.3 - - - integration-tests - - report-only - - - failsafe-report - - target/failsafe-reports - - - - - junit-tests - - report-only - - - surefire-report - - target/surefire-reports - - - - - - - - - UTF-8 - 2.5.6 - - - - - - - + + + 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/spring-aspectj-example/pom.xml b/SpringJava/SpringAspectJ/spring-aspectj-example/pom.xml deleted file mode 100644 index 78334dc..0000000 --- a/SpringJava/SpringAspectJ/spring-aspectj-example/pom.xml +++ /dev/null @@ -1,38 +0,0 @@ - - - - - 4.0.0 - - spring-aspectj - de.spring.example - 2.0-SNAPSHOT - - - spring-aspectj-example - spring-aspectj-example - http://maven.apache.org - - - - org.springframework - spring-aspects - 2.5.6 - - - org.springframework - spring-context - - - cglib - cglib-nodep - - - org.springframework - spring-aop - - - diff --git a/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/de/spring/example/MyAdvice.java b/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/de/spring/example/MyAdvice.java deleted file mode 100644 index 728b78b..0000000 --- a/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/de/spring/example/MyAdvice.java +++ /dev/null @@ -1,40 +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; - -@Aspect -public class MyAdvice { - - //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() { - System.out.println("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 wihout a special PointCut method. This advice has itself the PointCut - @Around("execution(* de.spring.example.web.Test.anotherExample())") - public Object doAround(ProceedingJoinPoint pjp) { - System.out.println("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) { - e.printStackTrace(); - } - // stop stopwatch - System.out.println("I am the Advice and I will be run before and after. AFTER " + retVal); - return retVal; - } -} diff --git a/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/de/spring/example/SpringAdvice.java b/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/de/spring/example/SpringAdvice.java deleted file mode 100644 index 90e345e..0000000 --- a/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/de/spring/example/SpringAdvice.java +++ /dev/null @@ -1,18 +0,0 @@ -package de.spring.example; - -import java.lang.reflect.Method; -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 { - - public void before(Method m, Object[] args, Object target) throws Throwable { - System.out.println("I am the SpringAdvice and I will be run before."); - } - -} diff --git a/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/de/spring/example/SpringContextLocator.java b/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/de/spring/example/SpringContextLocator.java deleted file mode 100644 index d599c8f..0000000 --- a/SpringJava/SpringAspectJ/spring-aspectj-example/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/SpringAspectJ/spring-aspectj-example/src/main/java/de/spring/example/SpringStart.java b/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/de/spring/example/SpringStart.java deleted file mode 100644 index 66285c2..0000000 --- a/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/de/spring/example/SpringStart.java +++ /dev/null @@ -1,18 +0,0 @@ -package de.spring.example; - -import de.spring.example.web.Test; - -public class SpringStart { - - /** - * @param args - */ - public static void main(String[] args) { - System.out.println("Starting application"); - SpringContextLocator.getInstance(); - - Test test = (Test) SpringContextLocator.getInstance().getBean("test"); - test.myMethod(); - test.anotherExample(); - } -} diff --git a/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/de/spring/example/SystemArchitecture.java b/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/de/spring/example/SystemArchitecture.java deleted file mode 100644 index a3ec844..0000000 --- a/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/de/spring/example/SystemArchitecture.java +++ /dev/null @@ -1,49 +0,0 @@ -package de.spring.example; - -import org.aspectj.lang.annotation.AfterReturning; -import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.Pointcut; - -/** - * - * 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 { - - //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() - { - System.out.println("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) { - System.out.println("The returned value by the method " + - "connected to the Pointcut: " + retVal); - } -} \ No newline at end of file diff --git a/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/de/spring/example/web/Test.java b/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/de/spring/example/web/Test.java deleted file mode 100644 index 1a8224c..0000000 --- a/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/de/spring/example/web/Test.java +++ /dev/null @@ -1,19 +0,0 @@ -package de.spring.example.web; - - -public class Test { - public int myMethod() - { - System.out.println("The Advice should be run before."); - - //This value will be caught by the Advice with the @AfterReturning annotation. - return 666; - } - - public int anotherExample() - { - System.out.println("The Advice should be run before and after."); - return 666; - - } -} diff --git a/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/resources/spring-config.xml b/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/resources/spring-config.xml deleted file mode 100644 index 9081c6c..0000000 --- a/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/resources/spring-config.xml +++ /dev/null @@ -1,59 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/SpringJava/SpringAspectJ/src/main/java/de/spring/example/MyAdvice.java b/SpringJava/SpringAspectJ/src/main/java/de/spring/example/MyAdvice.java new file mode 100644 index 0000000..5121bba --- /dev/null +++ b/SpringJava/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/SpringAspectJ/src/main/java/de/spring/example/SpringAdvice.java b/SpringJava/SpringAspectJ/src/main/java/de/spring/example/SpringAdvice.java new file mode 100644 index 0000000..e5bdf66 --- /dev/null +++ b/SpringJava/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/SpringAspectJ/src/main/java/de/spring/example/SpringContextLocator.java b/SpringJava/SpringAspectJ/src/main/java/de/spring/example/SpringContextLocator.java new file mode 100644 index 0000000..ff58276 --- /dev/null +++ b/SpringJava/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/SpringAspectJ/src/main/java/de/spring/example/SpringStart.java b/SpringJava/SpringAspectJ/src/main/java/de/spring/example/SpringStart.java new file mode 100644 index 0000000..f553341 --- /dev/null +++ b/SpringJava/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/SpringAspectJ/src/main/java/de/spring/example/SystemArchitecture.java b/SpringJava/SpringAspectJ/src/main/java/de/spring/example/SystemArchitecture.java new file mode 100644 index 0000000..ef83b3d --- /dev/null +++ b/SpringJava/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/SpringAspectJ/src/main/java/de/spring/example/web/Test.java b/SpringJava/SpringAspectJ/src/main/java/de/spring/example/web/Test.java new file mode 100644 index 0000000..d01bd5d --- /dev/null +++ b/SpringJava/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/SpringAspectJ/src/main/resources/log4j2.xml b/SpringJava/SpringAspectJ/src/main/resources/log4j2.xml new file mode 100644 index 0000000..8b3fc3f --- /dev/null +++ b/SpringJava/SpringAspectJ/src/main/resources/log4j2.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SpringJava/SpringAspectJ/src/main/resources/spring-config.xml b/SpringJava/SpringAspectJ/src/main/resources/spring-config.xml new file mode 100644 index 0000000..6aa0317 --- /dev/null +++ b/SpringJava/SpringAspectJ/src/main/resources/spring-config.xml @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SpringJava/SpringLTW/README b/SpringJava/SpringLTW/README new file mode 100644 index 0000000..9091fb8 --- /dev/null +++ b/SpringJava/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/SpringLTW/pom.xml b/SpringJava/SpringLTW/pom.xml index d55d01f..5ebc4a0 100644 --- a/SpringJava/SpringLTW/pom.xml +++ b/SpringJava/SpringLTW/pom.xml @@ -1,658 +1,190 @@ - + + 4.0.0 - de.spring.example + de.spring.example spring-ltw - 2.0-SNAPSHOT + 2.0-SNAPSHOT spring-ltw - http://www.myorganization.de - Spring AspectJ with Spring Libraries - - MyOrganization - http://www.myorganization.de - - pom - - trac - http://noserver/ - - - jenkins - http://noserver/jenkins/ - - - scm:svn:http://noserver - http://noserver - - + 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.activation - activation - 1.1 + javax.cache + cache-api + 1.0.0-PFD - log4j - log4j - 1.2.15 - - - com.sun.jdmk - jmxtools - - - com.sun.jmx - jmxri - - - javax.mail - mail - - - javax.jms - jms - - + javax.transaction + javax.transaction-api + 1.2 - junit - junit - 4.4 - test + 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 - - - - - c3p0 - c3p0 - 0.9.1.2 - - - cglib - cglib-nodep - 2.1_3 - - - commons-collections - commons-collections - 3.2.1 - - - commons-configuration - commons-configuration - 1.6 - - - commons-dbcp - commons-dbcp - 1.2.2 - - - commons-io - commons-io - 1.4 - - - commons-lang - commons-lang - 2.4 - - - commons-logging - commons-logging - 1.1.1 - - - commons-net - commons-net - 2.0 - - - commons-pool - commons-pool - 1.3 - - - com.h2database - h2 - 1.2.130 - - - dom4j - dom4j - 1.6.1 - - - xml-apis - xml-apis - - - - - hsqldb - hsqldb - 1.8.0.7 - - - javatar - javatar - 2.5 - - - jpos - jpos - 1.12.2 - provided - - - jpos - jpos-controls - 1.12.2 - provided - - - org.python - jython - 2.5.2b2 - - - urbanophile - java-getopt - 1.0.13 - - - mysql - mysql-connector-java - 5.1.6 - - - org.apache.ibatis - ibatis-sqlmap - 2.3.4.726 - - - org.apache.mina - mina-core - 2.0.0-M6 - - - org.aspectj - aspectjrt - 1.6.5 - - - org.aspectj - aspectjweaver - 1.6.5 - - - org.dbunit - dbunit - 2.4.4 - test - - - org.eclipse.jetty - jetty-continuation - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-http - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-io - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-security - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-server - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-servlet - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-webapp - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-util - 7.0.0.v20091005 - - - org.eclipse.jetty - jetty-xml - 7.0.0.v20091005 - - - org.slf4j - slf4j-api - 1.5.2 - - - org.slf4j - slf4j-log4j12 - 1.5.2 - - - log4j - log4j - - - - - org.springframework - spring-aop - ${spring.version} - - - org.springframework - spring-beans - ${spring.version} - - - org.springframework - spring-core - ${spring.version} - - - org.springframework - spring-context - ${spring.version} - - - org.springframework - spring-jdbc - ${spring.version} - - - org.springframework - spring-tx - ${spring.version} - - - org.springframework - spring-web - ${spring.version} - - - org.springframework - spring-webmvc - ${spring.version} - - - org.springframework - spring-context-support - ${spring.version} - - - org.springframework - spring-test - ${spring.version} - test - - - org.springframework.batch - spring-batch-test - 2.0.4.RELEASE - - - org.springframework.batch - spring-batch-core - 2.0.4.RELEASE - - - org.springframework.batch - spring-batch-infrastructure - 2.0.4.RELEASE - - - org.springframework - spring-orm - ${spring.version} - - - net.sf.ehcache - ehcache - 1.6.2 - - - org.springmodules - spring-modules-cache - 0.9 - - - p6spy - p6spy - 1.3 - - - javax.transaction - jta - 1.0.1B - - - javax.servlet - servlet-api - 2.5 - - - com.caucho - hessian - 3.1.6 - - - org.codehaus.jettison - jettison - 1.0 - - - com.thoughtworks.xstream - xstream - 1.3 - - - org.ini4j - ini4j - 0.5.1 - - - org.easymock - easymock - 2.4 - test - - - org.easymock - easymockclassextension - 2.4 - test - - - org.apache.commons - commons-compress - 1.0 - - - org.apache.commons - commons-math - 2.0 - - - dtgjpos_forms - dtgjpos_forms - 1.4.12 - provided - - - org.codehaus.castor - castor-xml - 1.3.1 - - - org.apache.xmlbeans - xmlbeans - 2.5.0 - - - org.apache.velocity.tools - velocity-tools-generic - 1.4 - - - org.apache.velocity - velocity - 1.6.2 - - - org.tmatesoft.svnkit - svnkit - 1.3.1 - - - - - spring-ltw-example - - - - - org.apache.maven.plugins - maven-compiler-plugin - 2.0.2 - - 1.6 - 1.6 - ${project.build.sourceEncoding} - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.6 - - - org.apache.maven.plugins - maven-surefire-plugin - 2.4.2 - - true - - - - org.codehaus.mojo - jdepend-maven-plugin - 2.0-beta-2 - - - org.apache.maven.plugins - maven-jar-plugin - 2.3.1 - - - - ${project.description} - ${project.version} - ${project.organization.name} - ${project.description} - ${project.version} - ${project.organization.name} - ${BUILD_TAG} - ${BUILD_ID} - ${BUILD_NUMBER} - ${prefix.committedRevision} - ${prefix.repository} - ${prefix.path} - - - - - - com.google.code.maven-svn-revision-number-plugin - maven-svn-revision-number-plugin - 1.6 - - - - revision - - - - - - - prefix - - - - - - - - - - releases - releases - http://noserver/artifactory/custom-annotations-libs-releases-local - - - snapshots-releases - snapshots-releases - http://noserver/artifactory/custom-annotations-libs-snapshots-local - - - noserver - file:///mnt/sdb1/data/downloads/jenkins/ - + + + 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-project-info-reports-plugin - 2.1.2 - - - - index - dependencies - cim - issue-tracking - scm - summary - project-team - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.6.1 - - - html - - MYPROJECT API for ${project.name} ${project.version} - MYPROJECT API for ${project.name} ${project.version} - - - javadoc - aggregate - - - - - - org.codehaus.mojo - taglist-maven-plugin - 2.3 - - - TODO - @todo - FIXME - XXX - - - - - org.apache.maven.plugins - maven-surefire-report-plugin - 2.4.3 - - - integration-tests - - report-only - - - failsafe-report - - target/failsafe-reports - - - - - junit-tests - - report-only - - - surefire-report - - target/surefire-reports - - - - - - - - - UTF-8 - 2.5.6 - - - - - - - + + + + + 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/spring-ltw-example/pom.xml b/SpringJava/SpringLTW/spring-ltw-example/pom.xml deleted file mode 100644 index 03f917f..0000000 --- a/SpringJava/SpringLTW/spring-ltw-example/pom.xml +++ /dev/null @@ -1,50 +0,0 @@ - - - - - 4.0.0 - - spring-ltw - de.spring.example - 2.0-SNAPSHOT - - - spring-ltw-example - spring-ltw-example - http://maven.apache.org - - - - org.springframework - spring-aspects - 2.5.6 - - - org.springframework - spring-context - - - cglib - cglib-nodep - - - org.springframework - spring-aop - - - org.springframework - spring-jdbc - - - c3p0 - c3p0 - - - mysql - mysql-connector-java - - - diff --git a/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/MyAdvice.java b/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/MyAdvice.java deleted file mode 100644 index bee04c1..0000000 --- a/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/MyAdvice.java +++ /dev/null @@ -1,24 +0,0 @@ -package de.spring.example; - -import org.aspectj.lang.annotation.After; -import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.Before; - -@Aspect -public class MyAdvice { - - - @Before("@annotation(de.spring.example.annotation.initTransactional)") - public void initTransactional() - { - System.out.println("I am the Advice initTransaction."); - TransactionManager.getInstance().initTransaction(); - } - - - @After("@annotation(de.spring.example.annotation.commitTransactional)") - public void commitTransactional() { - System.out.println("I am the Advice commitTransaction."); - TransactionManager.getInstance().commitTransaction(); - } -} diff --git a/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/SpringContextLocator.java b/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/SpringContextLocator.java deleted file mode 100644 index 2ece260..0000000 --- a/SpringJava/SpringLTW/spring-ltw-example/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/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/SpringStart.java b/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/SpringStart.java deleted file mode 100644 index a9da7cb..0000000 --- a/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/SpringStart.java +++ /dev/null @@ -1,20 +0,0 @@ -package de.spring.example; - -import de.spring.example.web.Test; - -public class SpringStart { - - /** - * @param args - */ - public static void main(String[] args) { - System.out.println("Starting application"); - SpringContextLocator.getInstance(); - - Test test = (Test) SpringContextLocator.getInstance().getBean("testOuter"); - test.myMethod(); - - Test.InnerTest testInner = (Test.InnerTest) SpringContextLocator.getInstance().getBean("testInner"); - testInner.innerMethod(); - } -} diff --git a/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/TransactionManager.java b/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/TransactionManager.java deleted file mode 100644 index d8657f2..0000000 --- a/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/TransactionManager.java +++ /dev/null @@ -1,45 +0,0 @@ -package de.spring.example; - -import org.aspectj.lang.annotation.Aspect; -import org.springframework.jdbc.datasource.DataSourceTransactionManager; -import org.springframework.transaction.TransactionStatus; - -@Aspect -public class TransactionManager { - 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() - { - System.out.println("initTRANSACTION"); - // transactionStatus = this.transactionManager.getTransaction(null); - } - - public void rollbackTransaction() - { - this.transactionManager.rollback(this.transactionStatus); - } - - - public void commitTransaction() - { - System.out.println("commitTRANSACTION"); - // this.transactionManager.commit(this.transactionStatus); - } - - - /************************* Setters and getters *******************************************/ - public void setTransactionManager(final DataSourceTransactionManager transactionManager) - { - this.transactionManager = transactionManager; - } -} diff --git a/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/annotation/commitTransactional.java b/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/annotation/commitTransactional.java deleted file mode 100644 index 1fda03f..0000000 --- a/SpringJava/SpringLTW/spring-ltw-example/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/spring-ltw-example/src/main/java/de/spring/example/annotation/initTransactional.java b/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/annotation/initTransactional.java deleted file mode 100644 index dad0b3e..0000000 --- a/SpringJava/SpringLTW/spring-ltw-example/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/spring-ltw-example/src/main/java/de/spring/example/web/Test.java b/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/web/Test.java deleted file mode 100644 index cc21d8f..0000000 --- a/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/web/Test.java +++ /dev/null @@ -1,23 +0,0 @@ -package de.spring.example.web; - -import de.spring.example.annotation.commitTransactional; -import de.spring.example.annotation.initTransactional; - - -public class Test { - @initTransactional - public int myMethod() - { - System.out.println("The Advice should be run before."); - - //This value will be caught by the Advice with the @AfterReturning annotation. - return 666; - } - - public class InnerTest { - @commitTransactional - public void innerMethod() { - System.out.println("I am the inner class. The Advice should be run after. "); - } - } -} diff --git a/SpringJava/SpringLTW/spring-ltw-example/src/main/resources/META-INF/aop.xml b/SpringJava/SpringLTW/spring-ltw-example/src/main/resources/META-INF/aop.xml deleted file mode 100644 index 9f6ee4b..0000000 --- a/SpringJava/SpringLTW/spring-ltw-example/src/main/resources/META-INF/aop.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - - - - - - - diff --git a/SpringJava/SpringLTW/spring-ltw-example/src/main/resources/spring-config.xml b/SpringJava/SpringLTW/spring-ltw-example/src/main/resources/spring-config.xml deleted file mode 100644 index fee0328..0000000 --- a/SpringJava/SpringLTW/spring-ltw-example/src/main/resources/spring-config.xml +++ /dev/null @@ -1,64 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/SpringJava/SpringLTW/src/main/java/de/spring/example/MyAdvice.java b/SpringJava/SpringLTW/src/main/java/de/spring/example/MyAdvice.java new file mode 100644 index 0000000..6e25974 --- /dev/null +++ b/SpringJava/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/SpringLTW/src/main/java/de/spring/example/SpringContextLocator.java b/SpringJava/SpringLTW/src/main/java/de/spring/example/SpringContextLocator.java new file mode 100644 index 0000000..8a82a3a --- /dev/null +++ b/SpringJava/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/SpringLTW/src/main/java/de/spring/example/SpringStart.java b/SpringJava/SpringLTW/src/main/java/de/spring/example/SpringStart.java new file mode 100644 index 0000000..2aba4c2 --- /dev/null +++ b/SpringJava/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/SpringLTW/src/main/java/de/spring/example/TransactionManager.java b/SpringJava/SpringLTW/src/main/java/de/spring/example/TransactionManager.java new file mode 100644 index 0000000..53f3c59 --- /dev/null +++ b/SpringJava/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/SpringLTW/src/main/java/de/spring/example/annotation/commitTransactional.java b/SpringJava/SpringLTW/src/main/java/de/spring/example/annotation/commitTransactional.java new file mode 100644 index 0000000..1fda03f --- /dev/null +++ b/SpringJava/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/SpringLTW/src/main/java/de/spring/example/annotation/initTransactional.java b/SpringJava/SpringLTW/src/main/java/de/spring/example/annotation/initTransactional.java new file mode 100644 index 0000000..dad0b3e --- /dev/null +++ b/SpringJava/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/SpringLTW/src/main/java/de/spring/example/web/Test.java b/SpringJava/SpringLTW/src/main/java/de/spring/example/web/Test.java new file mode 100644 index 0000000..5cd7d2a --- /dev/null +++ b/SpringJava/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/SpringLTW/src/main/resources/META-INF/aop.xml b/SpringJava/SpringLTW/src/main/resources/META-INF/aop.xml new file mode 100644 index 0000000..2345268 --- /dev/null +++ b/SpringJava/SpringLTW/src/main/resources/META-INF/aop.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + diff --git a/SpringJava/SpringLTW/src/main/resources/log4j2.xml b/SpringJava/SpringLTW/src/main/resources/log4j2.xml new file mode 100644 index 0000000..8b3fc3f --- /dev/null +++ b/SpringJava/SpringLTW/src/main/resources/log4j2.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SpringJava/SpringLTW/src/main/resources/spring-config.xml b/SpringJava/SpringLTW/src/main/resources/spring-config.xml new file mode 100644 index 0000000..ffb978c --- /dev/null +++ b/SpringJava/SpringLTW/src/main/resources/spring-config.xml @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +