#304 Fixed Support for ApplicationContextAware in akka-spring

This commit is contained in:
Johan Rask 2010-07-05 15:53:49 +02:00
parent da275f45dd
commit 2deb9faf3d
4 changed files with 25 additions and 4 deletions

View file

@ -12,6 +12,7 @@ import org.springframework.beans.BeanWrapper
import org.springframework.beans.BeanUtils
import org.springframework.util.ReflectionUtils
import org.springframework.util.StringUtils
import org.springframework.context.{ApplicationContext,ApplicationContextAware}
import org.springframework.beans.factory.BeanFactory
import org.springframework.beans.factory.config.AbstractFactoryBean
import se.scalablesolutions.akka.actor.ActiveObject
@ -26,7 +27,7 @@ import se.scalablesolutions.akka.util.Logging
* @author michaelkober
* @author <a href="johan.rask@jayway.com">Johan Rask</a>
*/
class ActiveObjectFactoryBean extends AbstractFactoryBean[AnyRef] with Logging {
class ActiveObjectFactoryBean extends AbstractFactoryBean[AnyRef] with Logging with ApplicationContextAware {
import StringReflect._
import AkkaSpringConfigurationTags._
@ -42,6 +43,7 @@ class ActiveObjectFactoryBean extends AbstractFactoryBean[AnyRef] with Logging {
@BeanProperty var dispatcher: DispatcherProperties = _
@BeanProperty var scope:String = VAL_SCOPE_SINGLETON
@BeanProperty var property:PropertyEntries = _
@BeanProperty var applicationContext:ApplicationContext = _
/*
* @see org.springframework.beans.factory.FactoryBean#getObjectType()
@ -78,6 +80,10 @@ class ActiveObjectFactoryBean extends AbstractFactoryBean[AnyRef] with Logging {
private def setProperties(ref:AnyRef) : AnyRef = {
log.debug("Processing properties and dependencies for target class %s",target)
val beanWrapper = new BeanWrapperImpl(ref);
if(ref.isInstanceOf[ApplicationContextAware]) {
log.debug("Setting application context")
beanWrapper.setPropertyValue("applicationContext",applicationContext)
}
for(entry <- property.entryList) {
val propertyDescriptor = BeanUtils.getPropertyDescriptor(ref.getClass,entry.name)
val method = propertyDescriptor.getWriteMethod();

View file

@ -1,6 +1,15 @@
package se.scalablesolutions.akka.spring;
public class SampleBean {
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationContext;
public class SampleBean implements ApplicationContextAware {
public boolean gotApplicationContext = false;
public void setApplicationContext(ApplicationContext context) {
gotApplicationContext = true;
}
public String foo(String s) {
return "hello " + s;

View file

@ -18,4 +18,6 @@
<bean id="string" class="java.lang.String">
<constructor-arg value="someString"/>
</bean>
<akka:active-object id="sample" target="se.scalablesolutions.akka.spring.SampleBean" timeout="1000" />
</beans>

View file

@ -69,6 +69,10 @@ class ActiveObjectFactoryBeanTest extends Spec with ShouldMatchers {
var ctx = new ClassPathXmlApplicationContext("appContext.xml");
val target:ResourceEditor = ctx.getBean("bean").asInstanceOf[ResourceEditor]
assert(target.getSource === "someString")
val sampleBean = ctx.getBean("sample").asInstanceOf[SampleBean];
Thread.sleep(300)
assert(sampleBean.gotApplicationContext)
}
}
}