|
| 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