Skip to content

Commit b79ebcb

Browse files
author
Daniel Thommes
committed
New AsyncTaskLoader for asynchronously loading the Spring context
1 parent 5e31248 commit b79ebcb

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

RoboSpring-JUnit4Android-Tests/.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
1010
<classpathentry combineaccessrules="false" exported="true" kind="src" path="/RoboSpring"/>
1111
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
12+
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
1213
<classpathentry kind="output" path="bin/classes"/>
1314
</classpath>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* Created on 18.03.2014
3+
*
4+
* © 2014 Daniel Thommes
5+
*/
6+
package org.robospring;
7+
8+
import org.springframework.context.support.AbstractXmlApplicationContext;
9+
10+
import android.content.Context;
11+
import android.support.v4.content.AsyncTaskLoader;
12+
13+
/**
14+
* Loader for asynchronous loading of the ApplicationContext
15+
*
16+
* @author Daniel Thommes
17+
*/
18+
public class RoboSpringLoader extends
19+
AsyncTaskLoader<AbstractXmlApplicationContext> {
20+
21+
private String configLocation;
22+
23+
private Object autowireTarget;
24+
25+
/**
26+
* @param context
27+
*/
28+
public RoboSpringLoader(Context context) {
29+
super(context);
30+
}
31+
32+
/**
33+
* @param context
34+
*/
35+
public RoboSpringLoader(Context context, Object autowireTarget) {
36+
super(context);
37+
this.autowireTarget = autowireTarget;
38+
}
39+
40+
/**
41+
* @param context
42+
*/
43+
public RoboSpringLoader(Context context, String configLocation) {
44+
super(context);
45+
this.configLocation = configLocation;
46+
}
47+
48+
/**
49+
* @param context
50+
*/
51+
public RoboSpringLoader(Context context, String configLocation,
52+
Object autowireTarget) {
53+
super(context);
54+
this.configLocation = configLocation;
55+
this.autowireTarget = autowireTarget;
56+
}
57+
58+
/**
59+
* {@inheritDoc}
60+
*
61+
* @see android.support.v4.content.AsyncTaskLoader#loadInBackground()
62+
*/
63+
public AbstractXmlApplicationContext loadInBackground() {
64+
Thread.currentThread().setContextClassLoader(
65+
RoboSpringLoader.class.getClassLoader());
66+
AbstractXmlApplicationContext context;
67+
if (configLocation != null) {
68+
context = RoboSpring.getContext(getContext(), configLocation);
69+
if (autowireTarget != null) {
70+
RoboSpring.autowire(autowireTarget, configLocation);
71+
}
72+
}
73+
else {
74+
context = RoboSpring.getContext(getContext());
75+
if (autowireTarget != null) {
76+
RoboSpring.autowire(autowireTarget);
77+
}
78+
}
79+
return context;
80+
}
81+
82+
}

0 commit comments

Comments
 (0)