Annotations handler and programming with Aspects.
Could be useful or not, but at least it seems interesting.
android:versionName="1.0">
<uses-sdk android:minSdkVersion="13" />
- <application android:icon="@drawable/icon" android:label="@string/app_name">
+ <application android:label="My AndroidTetris">
<activity android:name=".AndroidTetrisActivity"
- android:label="@string/app_name">
+ android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
+import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
*
*/
public class DrawView extends SurfaceView {
- int position = 150;
private SurfaceHolder holder;
private AndroidTetrisThread gameLoopThread;
-
private static final int TILESIZE=16;
- //now for the map...
private static final int MAPWIDTH=10;
private static final int MAPHEIGHT=30;
private static final int GREY=8;
-
-
private AndroidTetrisThread thread;
-
- Bitmap[] tileArray;
- Tile[][] mapMatrix;
- Piece prePiece;
- Piece currentPiece;
+ private Bitmap[] tileArray;
+ private Tile[][] mapMatrix;
+ private Piece prePiece;
+ private Piece currentPiece;
class AndroidTetrisThread extends Thread
{
private DrawView view;
-
private boolean running = false;
{
while (running)
{
- Canvas c = null;
- try {
- c = view.getHolder().lockCanvas();
- synchronized (view.getHolder())
- {
- view.move(0, 1);
- view.drawMap(c);
- //view.onDraw(c);
- }
- }finally {
- if (c != null)
- view.getHolder().unlockCanvasAndPost(c);
- }
- }
+ Canvas c = view.getHolder().lockCanvas();
+ synchronized (view.getHolder())
+ {
+ view.move(0, 1);
+ view.drawMap(c);
+ //view.onDraw(c);
+ }
+ view.getHolder().unlockCanvasAndPost(c);
+ }
}
}
public DrawView(Context context)
{
super(context);
- this.resetTiles(10);
- for (Tile color : Tile.values() )
- {
- this.loadTile(color.getColor(), color.getColorRGBA());
- }
- mapMatrix = new Tile[MAPWIDTH][MAPHEIGHT+1];
- this.NewGame();
- this.newBlock();
-
+
+ this.newGame();
+ currentPiece = newBlock();
+ currentPiece.x = MAPWIDTH/2-2;
+ currentPiece.y = -1;
+ prePiece = newBlock();
+ prePiece.x=MAPWIDTH+2;
+ prePiece.y=GREY/4;
+
// register our interest in hearing about changes to our surface
//SurfaceHolder holder = getHolder();
//holder.addCallback(this);
- gameLoopThread = new AndroidTetrisThread(this);
- holder = getHolder();
- holder.addCallback(new SurfaceHolder.Callback() {
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- boolean retry = true;
- gameLoopThread.setRunning(false);
- while (retry) {
- try {
- gameLoopThread.join();
- retry = false;
- } catch (InterruptedException e) {
-
- }
- }
- }
-
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- gameLoopThread.setRunning(true);
- gameLoopThread.start();
- }
-
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format,
- int width, int height) {
-
- }
+ gameLoopThread = new AndroidTetrisThread(this);
+ holder = getHolder();
+ holder.addCallback(new SurfaceHolder.Callback() {
+ @Override
+ public void surfaceDestroyed(SurfaceHolder holder) {
+ boolean retry = true;
+ gameLoopThread.setRunning(false);
+ while (retry) {
+ try {
+ gameLoopThread.join();
+ retry = false;
+ } catch (InterruptedException e) {
+
+ }
+ }
+ }
+
+ @Override
+ public void surfaceCreated(SurfaceHolder holder) {
+ gameLoopThread.setRunning(true);
+ gameLoopThread.start();
+ }
+
+ @Override
+ public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
+
+ }
+ });
+ }
- });
- }
public void resetTiles(int tilecount) {
tileArray = new Bitmap[tilecount];
}
+
public void loadTile(int key, int color)
{
}
tileArray[key] = bitmap;
}
+
- protected void NewGame()
+ protected void newGame()
{
+ this.resetTiles(10);
+ for (Tile color : Tile.values() )
+ {
+ this.loadTile(color.getColor(), color.getColorRGBA());
+ }
+ mapMatrix = new Tile[MAPWIDTH][MAPHEIGHT+1];
+
//start out the map
for(int x=0;x< MAPWIDTH;x++)
{
}
}
- protected void newBlock()
+ protected Piece newBlock()
{
Random random = new Random();
- currentPiece = Piece.getPiece(random.nextInt(7)%7);
- currentPiece.x = MAPWIDTH/2-2;
- currentPiece.y = -1;
+ Piece piece = Piece.getPiece(random.nextInt(7)%7);
- prePiece = Piece.getPiece(random.nextInt(7)%7);
- prePiece.x=MAPWIDTH+2;
- prePiece.y=GREY/4;
+ return piece;
}
protected void drawTile(Canvas canvas, int color, int x, int y)
{
if (this.collisionTest(x, y))
{
- this.newBlock();
+ currentPiece = prePiece;
+ currentPiece.x = MAPWIDTH/2-2;
+ currentPiece.y = -1;
+ prePiece = newBlock();
+ prePiece.x=MAPWIDTH+2;
+ prePiece.y=GREY/4;
}
else
{
return false;
}
+
+ @Override
+ public boolean onKeyDown(int keyCode, KeyEvent msg) {
+ if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
+ try {
+ AndroidTetrisThread.sleep(1000000);
+ } catch (InterruptedException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ Canvas c =null;
+ c = this.getHolder().lockCanvas();
+ synchronized (this.getHolder())
+ {
+ this.move(1, 0);
+ this.drawMap(c);
+ //view.onDraw(c);
+ }
+ return true;
+ }
+ return false;
+ }
+
+
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+
+<project
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
+ xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <artifactId>custom-annotations</artifactId>
+ <groupId>de.spring.example</groupId>
+ <version>2.0-SNAPSHOT</version>
+ </parent>
+
+ <groupId>de.spring.example</groupId>
+ <artifactId>custom-annotations-implementation</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ <name>custom-annotations-implementation</name>
+ <url>http://maven.apache.org</url>
+
+ <dependencies>
+
+ <!-- Assembly version info -->
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-model</artifactId>
+ <version>2.2.1</version>
+ </dependency>
+
+ <!--
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-aspects</artifactId>
+ <version>2.5.6</version>
+ </dependency>
+ -->
+
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-context</artifactId>
+ <version>2.5.6</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-jdbc</artifactId>
+ <version>2.5.6</version>
+ </dependency>
+
+ <dependency>
+ <groupId>c3p0</groupId>
+ <artifactId>c3p0</artifactId>
+ <version>0.9.1.2</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.aspectj</groupId>
+ <artifactId>aspectjrt</artifactId>
+ <version>1.6.12</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.aspectj</groupId>
+ <artifactId>aspectjweaver</artifactId>
+ <version>1.6.12</version>
+ </dependency>
+
+ <!--
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-agent</artifactId>
+ <version>2.5.6</version>
+ </dependency>
+ -->
+
+ </dependencies>
+
+</project>
--- /dev/null
+package de.spring.example;
+
+import java.util.Map;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+
+
+public class AnnotationsHandler implements ApplicationContextAware, InitializingBean {
+ private ApplicationContext applicationContext;
+
+ @Override
+ public void afterPropertiesSet() {
+ GenericBeanFactoryAccessor genericBeanFactoryAccessor = new GenericBeanFactoryAccessor(applicationContext);
+
+ final Map<String, Object> transactionalClass = genericBeanFactoryAccessor.getBeansWithAnnotation(TransactionalN2A.class);
+
+ for (final Object myFoo : transactionalClass.values()) {
+ final Class<? extends Object> fooClass = myFoo.getClass();
+ final TransactionalN2A annotation = fooClass.getAnnotation(TransactionalN2A.class);
+ System.out.println("Found 1 foo class: " + fooClass + ", with tags: ");
+ }
+ }
+
+ @Override
+ public void setApplicationContext(final ApplicationContext applicationContext)
+ throws BeansException {
+ this.applicationContext = applicationContext;
+ }
+ }
+
--- /dev/null
+package de.spring.example;
+
+import java.util.Map;
+
+import org.aspectj.lang.annotation.Aspect;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+
+@Aspect
+public class AspectHandler {
+
+
+
+
+ }
+
--- /dev/null
+package de.spring.example;
+
+
+public class Prueba {
+ public void bar(){
+ System.out.println("I am not a number, I am a free man!");
+ }
+
+ @TransactionalN2A
+ public class InnerService {
+ public void innerMethod() {
+ System.out.println("xxx: AopService$InnerClass.innerMethod()");
+ }
+ }
+
+}
+
--- /dev/null
+package de.spring.example;
+
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+
+/**
+ * Localizador de beans para de los dispositivos
+ *
+ * @author rvp001es
+ */
+public final class SpringContextLocator {
+
+
+ // Singleton Pattern
+ private static SpringContextLocator instance;
+
+ // Spring ApplicationContext
+ private static ApplicationContext context;
+
+ // Dispositivos logicos
+ private static final String SPRING_CONFIG_CONTEXT="spring-config.xml";
+ //private static final String DATABASE_CONFIG="database-config.xml";
+
+
+ /**
+ * Private constructor. Singleton pattern.
+ */
+ private SpringContextLocator() {
+ String[] factoryFiles = null;
+ System.out.println("Loading files context " +
+ SpringContextLocator.SPRING_CONFIG_CONTEXT);
+
+ factoryFiles = new String[] { SPRING_CONFIG_CONTEXT };
+
+ SpringContextLocator.context = new ClassPathXmlApplicationContext(factoryFiles);
+
+ System.out.println("The N2A devices context and test " +
+ "context has been loaded successfully!! ");
+ }
+
+ /**
+ * Singleton pattern. GetInstance()
+ */
+ public synchronized static SpringContextLocator getInstance() {
+ if (SpringContextLocator.instance == null) {
+ SpringContextLocator.instance = new SpringContextLocator();
+ }
+ return SpringContextLocator.instance;
+ }
+
+ /**
+ * Return a bean in application context.
+ */
+ public Object getBean(final String name) {
+ return SpringContextLocator.context.getBean(name);
+ }
+}
--- /dev/null
+package de.spring.example;
+
+public class SpringStart {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ System.out.println("HOLA");
+ SpringContextLocator.getInstance();
+
+ }
+
+}
--- /dev/null
+package de.spring.example;
+
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
+import org.springframework.transaction.TransactionStatus;
+
+
+public class TransactionManagerN2A {
+ private static TransactionManagerN2A instance = new TransactionManagerN2A();
+ private DataSourceTransactionManager transactionManager;
+ private TransactionStatus transactionStatus;
+
+ //Why could you want to extend this class?
+ private TransactionManagerN2A() {}
+
+ public static TransactionManagerN2A getInstance() {
+ return instance;
+ }
+
+ public void initTransaction()
+ {
+ transactionStatus = this.transactionManager.getTransaction(null);
+ }
+
+ public void rollbackTransaction()
+ {
+ this.transactionManager.rollback(this.transactionStatus);
+ }
+
+ public void commitTransaction()
+ {
+ this.transactionManager.commit(this.transactionStatus);
+ }
+
+
+ /************************* Setters and getters *******************************************/
+ public void setTransactionManager(final DataSourceTransactionManager transactionManager)
+ {
+ this.transactionManager = transactionManager;
+ }
+}
+
--- /dev/null
+/**
+ *
+ */
+package de.spring.example;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import org.springframework.stereotype.Component;
+
+
+@Retention(RetentionPolicy.RUNTIME)
+@Component
+public @interface TransactionalN2A {
+}
--- /dev/null
+<!DOCTYPE aspectj PUBLIC
+ "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
+ <aspectj>
+ <weaver options="-verbose -showWeaveInfo"/>
+ <!-- only weave classes in our application-specific packages -->
+ <include within="es.dia.*"/>
+ </weaver>
+ <aspects>
+ <aspect name="es.dia.pos.n2a.aspectj.annotations.AnnotationsHandler"/>
+ </aspects>
+ </aspectj>
\ No newline at end of file
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:context="http://www.springframework.org/schema/context"
+ xmlns:aop="http://www.springframework.org/schema/aop"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans
+ http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
+ http://www.springframework.org/schema/aop
+ http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
+ http://www.springframework.org/schema/context
+ http://www.springframework.org/schema/context/spring-context-2.5.xsd
+ http://www.springframework.org/schema/tx
+ http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
+
+ <aop:aspectj-autoproxy proxy-target-class="true"/>
+
+ <context:load-time-weaver
+ weaver-class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"
+ aspectj-weaving="on"/>
+
+
+ <context:component-scan base-package="es.dia">
+ <context:include-filter
+ type="annotation"
+ expression="es.dia.pos.n2a.aspectj.annotations.TransactionalN2A" />
+ </context:component-scan>
+
+
+ <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
+ <property name="user" value="root"/>
+ <property name="password" value=""/>
+ <property name="driverClass" value="com.mysql.jdbc.Driver"/>
+ <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/n2a?autoReconnect=true"/>
+ <property name="initialPoolSize" value="5"/>
+ <property name="maxPoolSize" value="20"/>
+ <property name="minPoolSize" value="10"/>
+ <property name="acquireIncrement" value="1"/>
+ <property name="acquireRetryAttempts" value="5"/>
+ <property name="acquireRetryDelay" value="1000"/>
+ <property name="automaticTestTable" value="con_test"/>
+ <property name="checkoutTimeout" value="5000"/>
+ </bean>
+
+ <bean id="annotationsHandler" class="es.dia.pos.n2a.aspectj.annotations.AnnotationsHandler" >
+
+ </bean>
+
+ <!--
+ <bean class="org.springframework.transaction.aspectj.AnnotationTransactionAspect" factory-method="aspectOf">
+ <property name="transactionManagerN2A" ref="transactionManagerN2A"/>
+ </bean>
+ -->
+
+ <bean id="pruebaOuter" class="es.dia.pos.n2a.aspectj.annotations.Prueba"/>
+ <bean id="pruebaInner" class="es.dia.pos.n2a.aspectj.annotations.Prueba$InnerService">
+ <constructor-arg ref="pruebaOuter"/>
+ </bean>
+
+ <!--
+ I do not care about the N2A default configuration, so I use here the scope singleton and lazy-init false.
+ -->
+ <bean id="transactionManagerN2A"
+ class="es.dia.pos.n2a.aspectj.annotations.TransactionManagerN2A"
+ factory-method="getInstance"
+ scope="singleton"
+ lazy-init="false" >
+ <property name="transactionManager">
+ <ref bean="transactionManager" />
+ </property>
+ </bean>
+
+ <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
+ <property name="dataSource" ref="dataSource"/>
+ </bean>
+
+</beans>
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>de.spring.example</groupId>
+ <artifactId>custom-annotations</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ <name>custom-annotations</name>
+ <url>http://maven.apache.org</url>
+ <packaging>pom</packaging>
+
+ <dependencies>
+ </dependencies>
+
+ <modules>
+ <module>custom-annotations-implementation</module>
+ </modules>
+</project>
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>de.spring.example</groupId>
+ <artifactId>spring-aspectj</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ <name>custom-annotations</name>
+ <url>http://maven.apache.org</url>
+ <packaging>pom</packaging>
+
+ <dependencies>
+ </dependencies>
+
+ <modules>
+ <module>spring-aspectj-example</module>
+ </modules>
+</project>
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+
+<project
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
+ xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <artifactId>spring-aspectj</artifactId>
+ <groupId>de.spring.example</groupId>
+ <version>2.0-SNAPSHOT</version>
+ </parent>
+
+ <groupId>de.spring.example</groupId>
+ <artifactId>spring-aspectj-example</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ <name>spring-aspectj-example</name>
+ <url>http://maven.apache.org</url>
+
+ <dependencies>
+
+ <!-- Assembly version info -->
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-model</artifactId>
+ <version>2.2.1</version>
+ </dependency>
+
+ <!--
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-aspects</artifactId>
+ <version>2.5.6</version>
+ </dependency>
+ -->
+
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-context</artifactId>
+ <version>2.5.6</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-jdbc</artifactId>
+ <version>2.5.6</version>
+ </dependency>
+
+ <dependency>
+ <groupId>c3p0</groupId>
+ <artifactId>c3p0</artifactId>
+ <version>0.9.1.2</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.aspectj</groupId>
+ <artifactId>aspectjrt</artifactId>
+ <version>1.6.12</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.aspectj</groupId>
+ <artifactId>aspectjweaver</artifactId>
+ <version>1.6.12</version>
+ </dependency>
+
+ <!--
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-agent</artifactId>
+ <version>2.5.6</version>
+ </dependency>
+ -->
+
+ </dependencies>
+
+</project>
--- /dev/null
+package de.spring.example;
+
+import java.util.Map;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+
+
+public class AnnotationsHandler implements ApplicationContextAware, InitializingBean {
+ private ApplicationContext applicationContext;
+
+ @Override
+ public void afterPropertiesSet() {
+ GenericBeanFactoryAccessor genericBeanFactoryAccessor = new GenericBeanFactoryAccessor(applicationContext);
+
+ final Map<String, Object> transactionalClass = genericBeanFactoryAccessor.getBeansWithAnnotation(TransactionalN2A.class);
+
+ for (final Object myFoo : transactionalClass.values()) {
+ final Class<? extends Object> fooClass = myFoo.getClass();
+ final TransactionalN2A annotation = fooClass.getAnnotation(TransactionalN2A.class);
+ System.out.println("Found 1 foo class: " + fooClass + ", with tags: ");
+ }
+ }
+
+ @Override
+ public void setApplicationContext(final ApplicationContext applicationContext)
+ throws BeansException {
+ this.applicationContext = applicationContext;
+ }
+ }
+
--- /dev/null
+package de.spring.example;
+
+import java.util.Map;
+
+import org.aspectj.lang.annotation.Aspect;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+
+@Aspect
+public class AspectHandler {
+
+
+
+
+ }
+
--- /dev/null
+package de.spring.example;
+
+
+public class Prueba {
+ public void bar(){
+ System.out.println("I am not a number, I am a free man!");
+ }
+
+ @TransactionalN2A
+ public class InnerService {
+ public void innerMethod() {
+ System.out.println("xxx: AopService$InnerClass.innerMethod()");
+ }
+ }
+
+}
+
--- /dev/null
+package de.spring.example;
+
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+
+/**
+ * Localizador de beans para de los dispositivos
+ *
+ * @author rvp001es
+ */
+public final class SpringContextLocator {
+
+
+ // Singleton Pattern
+ private static SpringContextLocator instance;
+
+ // Spring ApplicationContext
+ private static ApplicationContext context;
+
+ // Dispositivos logicos
+ private static final String SPRING_CONFIG_CONTEXT="spring-config.xml";
+ //private static final String DATABASE_CONFIG="database-config.xml";
+
+
+ /**
+ * Private constructor. Singleton pattern.
+ */
+ private SpringContextLocator() {
+ String[] factoryFiles = null;
+ System.out.println("Loading files context " +
+ SpringContextLocator.SPRING_CONFIG_CONTEXT);
+
+ factoryFiles = new String[] { SPRING_CONFIG_CONTEXT };
+
+ SpringContextLocator.context = new ClassPathXmlApplicationContext(factoryFiles);
+
+ System.out.println("The N2A devices context and test " +
+ "context has been loaded successfully!! ");
+ }
+
+ /**
+ * Singleton pattern. GetInstance()
+ */
+ public synchronized static SpringContextLocator getInstance() {
+ if (SpringContextLocator.instance == null) {
+ SpringContextLocator.instance = new SpringContextLocator();
+ }
+ return SpringContextLocator.instance;
+ }
+
+ /**
+ * Return a bean in application context.
+ */
+ public Object getBean(final String name) {
+ return SpringContextLocator.context.getBean(name);
+ }
+}
--- /dev/null
+package de.spring.example;
+
+public class SpringStart {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ System.out.println("HOLA");
+ SpringContextLocator.getInstance();
+
+ }
+
+}
--- /dev/null
+package de.spring.example;
+
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
+import org.springframework.transaction.TransactionStatus;
+
+
+public class TransactionManagerN2A {
+ private static TransactionManagerN2A instance = new TransactionManagerN2A();
+ private DataSourceTransactionManager transactionManager;
+ private TransactionStatus transactionStatus;
+
+ //Why could you want to extend this class?
+ private TransactionManagerN2A() {}
+
+ public static TransactionManagerN2A getInstance() {
+ return instance;
+ }
+
+ public void initTransaction()
+ {
+ transactionStatus = this.transactionManager.getTransaction(null);
+ }
+
+ public void rollbackTransaction()
+ {
+ this.transactionManager.rollback(this.transactionStatus);
+ }
+
+ public void commitTransaction()
+ {
+ this.transactionManager.commit(this.transactionStatus);
+ }
+
+
+ /************************* Setters and getters *******************************************/
+ public void setTransactionManager(final DataSourceTransactionManager transactionManager)
+ {
+ this.transactionManager = transactionManager;
+ }
+}
+
--- /dev/null
+/**
+ *
+ */
+package de.spring.example;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import org.springframework.stereotype.Component;
+
+
+@Retention(RetentionPolicy.RUNTIME)
+@Component
+public @interface TransactionalN2A {
+}
--- /dev/null
+<!DOCTYPE aspectj PUBLIC
+ "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
+ <aspectj>
+ <weaver options="-verbose -showWeaveInfo"/>
+ <!-- only weave classes in our application-specific packages -->
+ <include within="es.dia.*"/>
+ </weaver>
+ <aspects>
+ <aspect name="es.dia.pos.n2a.aspectj.annotations.AnnotationsHandler"/>
+ </aspects>
+ </aspectj>
\ No newline at end of file
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:context="http://www.springframework.org/schema/context"
+ xmlns:aop="http://www.springframework.org/schema/aop"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans
+ http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
+ http://www.springframework.org/schema/aop
+ http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
+ http://www.springframework.org/schema/context
+ http://www.springframework.org/schema/context/spring-context-2.5.xsd
+ http://www.springframework.org/schema/tx
+ http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
+
+ <aop:aspectj-autoproxy proxy-target-class="true"/>
+
+ <context:load-time-weaver
+ weaver-class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"
+ aspectj-weaving="on"/>
+
+
+ <context:component-scan base-package="es.dia">
+ <context:include-filter
+ type="annotation"
+ expression="es.dia.pos.n2a.aspectj.annotations.TransactionalN2A" />
+ </context:component-scan>
+
+
+ <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
+ <property name="user" value="root"/>
+ <property name="password" value=""/>
+ <property name="driverClass" value="com.mysql.jdbc.Driver"/>
+ <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/n2a?autoReconnect=true"/>
+ <property name="initialPoolSize" value="5"/>
+ <property name="maxPoolSize" value="20"/>
+ <property name="minPoolSize" value="10"/>
+ <property name="acquireIncrement" value="1"/>
+ <property name="acquireRetryAttempts" value="5"/>
+ <property name="acquireRetryDelay" value="1000"/>
+ <property name="automaticTestTable" value="con_test"/>
+ <property name="checkoutTimeout" value="5000"/>
+ </bean>
+
+ <bean id="annotationsHandler" class="es.dia.pos.n2a.aspectj.annotations.AnnotationsHandler" >
+
+ </bean>
+
+ <!--
+ <bean class="org.springframework.transaction.aspectj.AnnotationTransactionAspect" factory-method="aspectOf">
+ <property name="transactionManagerN2A" ref="transactionManagerN2A"/>
+ </bean>
+ -->
+
+ <bean id="pruebaOuter" class="es.dia.pos.n2a.aspectj.annotations.Prueba"/>
+ <bean id="pruebaInner" class="es.dia.pos.n2a.aspectj.annotations.Prueba$InnerService">
+ <constructor-arg ref="pruebaOuter"/>
+ </bean>
+
+ <!--
+ I do not care about the N2A default configuration, so I use here the scope singleton and lazy-init false.
+ -->
+ <bean id="transactionManagerN2A"
+ class="es.dia.pos.n2a.aspectj.annotations.TransactionManagerN2A"
+ factory-method="getInstance"
+ scope="singleton"
+ lazy-init="false" >
+ <property name="transactionManager">
+ <ref bean="transactionManager" />
+ </property>
+ </bean>
+
+ <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
+ <property name="dataSource" ref="dataSource"/>
+ </bean>
+
+</beans>