Created
November 1, 2016 18:14
-
-
Save johnou/48d744e1a63396e49e80cdf250ae25c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cloud.orbit.actors.extensions.LifetimeExtension; | |
import cloud.orbit.actors.runtime.AbstractActor; | |
import cloud.orbit.concurrent.Task; | |
import cloud.orbit.exception.UncheckedException; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.beans.factory.config.AutowireCapableBeanFactory; | |
import org.springframework.stereotype.Component; | |
/** | |
* {@link SpringLifetimeExtension} allows Spring DI for {@link cloud.orbit.actors.runtime.AbstractActor}. | |
* Autowire mode can be set via <code>orbit.actors.autowireMode</code>. | |
* | |
* @author Johno Crawford ([email protected]) | |
*/ | |
@Component | |
public class SpringLifetimeExtension implements LifetimeExtension { | |
@Autowired | |
private AutowireCapableBeanFactory autowireCapableBeanFactory; | |
@Value("${orbit.actors.autowireMode:" + AutowireCapableBeanFactory.AUTOWIRE_BY_NAME + "}") | |
private int autowireMode; | |
/* | |
@Override | |
public Task preActivation(AbstractActor abstractActor) { | |
autowireCapableBeanFactory.autowireBeanProperties(abstractActor, autowireMode, false); | |
autowireCapableBeanFactory.initializeBean(abstractActor, abstractActor.getClass().getName()); // bean post processors | |
return Task.done(); | |
} | |
*/ | |
@Override | |
@SuppressWarnings("unchecked") | |
public <T> T newInstance(Class<T> concreteClass) { | |
try { | |
T abstractActor = concreteClass.newInstance(); | |
autowireCapableBeanFactory.autowireBeanProperties(abstractActor, autowireMode, false); | |
return (T) autowireCapableBeanFactory.initializeBean(abstractActor, abstractActor.getClass().getName()); | |
} catch (Exception ex) { | |
throw new UncheckedException(ex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment