#301 DI does not work in akka-spring when specifying an interface
This commit is contained in:
parent
7a155c70ba
commit
3e7980a466
6 changed files with 78 additions and 27 deletions
|
|
@ -49,6 +49,20 @@ class ActiveObjectFactoryBean extends AbstractFactoryBean[AnyRef] with Logging w
|
|||
@BeanProperty var property:PropertyEntries = _
|
||||
@BeanProperty var applicationContext:ApplicationContext = _
|
||||
|
||||
// Holds info about if deps has been set or not. Depends on
|
||||
// if interface is specified or not. We must set deps on
|
||||
// target instance if interface is specified
|
||||
var hasSetDependecies = false
|
||||
|
||||
|
||||
override def isSingleton:Boolean = {
|
||||
if(scope.equals(VAL_SCOPE_SINGLETON)) {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* @see org.springframework.beans.factory.FactoryBean#getObjectType()
|
||||
*/
|
||||
|
|
@ -63,11 +77,6 @@ class ActiveObjectFactoryBean extends AbstractFactoryBean[AnyRef] with Logging w
|
|||
* @see org.springframework.beans.factory.config.AbstractFactoryBean#createInstance()
|
||||
*/
|
||||
def createInstance: AnyRef = {
|
||||
if(scope.equals(VAL_SCOPE_SINGLETON)) {
|
||||
setSingleton(true)
|
||||
} else {
|
||||
setSingleton(false)
|
||||
}
|
||||
var argumentList = ""
|
||||
if (isRemote) argumentList += "r"
|
||||
if (hasInterface) argumentList += "i"
|
||||
|
|
@ -87,6 +96,10 @@ class ActiveObjectFactoryBean extends AbstractFactoryBean[AnyRef] with Logging w
|
|||
}
|
||||
|
||||
private def setProperties(ref:AnyRef) : AnyRef = {
|
||||
if(hasSetDependecies) {
|
||||
return ref
|
||||
}
|
||||
|
||||
log.debug("Processing properties and dependencies for target class %s",target)
|
||||
val beanWrapper = new BeanWrapperImpl(ref);
|
||||
if(ref.isInstanceOf[ApplicationContextAware]) {
|
||||
|
|
@ -109,6 +122,8 @@ class ActiveObjectFactoryBean extends AbstractFactoryBean[AnyRef] with Logging w
|
|||
throw new AkkaBeansException("Either property@ref or property@value must be set on property element")
|
||||
}
|
||||
}
|
||||
//un-set so next bean can be managed
|
||||
hasSetDependecies = false
|
||||
ref
|
||||
}
|
||||
|
||||
|
|
@ -139,10 +154,12 @@ class ActiveObjectFactoryBean extends AbstractFactoryBean[AnyRef] with Logging w
|
|||
if (transactional) config.makeTransactionRequired
|
||||
config
|
||||
}
|
||||
|
||||
private[akka] def aNewInstance[T <: AnyRef](clazz: Class[T]) : T = {
|
||||
clazz.newInstance().asInstanceOf[T]
|
||||
}
|
||||
def aNewInstance[T <: AnyRef](clazz: Class[T]) : T = {
|
||||
var ref = clazz.newInstance().asInstanceOf[T]
|
||||
setProperties(ref)
|
||||
hasSetDependecies = true
|
||||
ref
|
||||
}
|
||||
|
||||
private[akka] def isRemote = (host != null) && (!host.isEmpty)
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
package se.scalablesolutions.akka.spring;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
|
||||
public class Pojo implements PojoInf,ApplicationContextAware {
|
||||
|
||||
private String string;
|
||||
|
||||
private boolean gotApplicationContext = false;
|
||||
|
||||
public boolean gotApplicationContext() {
|
||||
return gotApplicationContext;
|
||||
}
|
||||
public void setApplicationContext(ApplicationContext context) {
|
||||
gotApplicationContext = true;
|
||||
}
|
||||
|
||||
public void setString(String s) {
|
||||
string = s;
|
||||
}
|
||||
|
||||
public String getString() {
|
||||
return string;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
package se.scalablesolutions.akka.spring;
|
||||
|
||||
public interface PojoInf {
|
||||
|
||||
public String getString();
|
||||
public boolean gotApplicationContext();
|
||||
|
||||
}
|
||||
|
|
@ -1,23 +1,13 @@
|
|||
package se.scalablesolutions.akka.spring;
|
||||
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import se.scalablesolutions.akka.actor.annotation.shutdown;
|
||||
|
||||
public class SampleBean implements ApplicationContextAware {
|
||||
public class SampleBean {
|
||||
|
||||
public boolean down;
|
||||
|
||||
public boolean gotApplicationContext;
|
||||
|
||||
public SampleBean() {
|
||||
down = false;
|
||||
gotApplicationContext = false;
|
||||
}
|
||||
|
||||
public void setApplicationContext(ApplicationContext context) {
|
||||
gotApplicationContext = true;
|
||||
}
|
||||
|
||||
public String foo(String s) {
|
||||
|
|
|
|||
|
|
@ -22,4 +22,12 @@
|
|||
<bean id="string" class="java.lang.String">
|
||||
<constructor-arg value="someString"/>
|
||||
</bean>
|
||||
</beans>
|
||||
<akka:active-object id="pojoInf"
|
||||
target="se.scalablesolutions.akka.spring.Pojo"
|
||||
interface="se.scalablesolutions.akka.spring.PojoInf"
|
||||
scope="singleton"
|
||||
timeout="1000">
|
||||
<property name="string" value="akka rocks"/>
|
||||
</akka:active-object>
|
||||
|
||||
</beans>
|
||||
|
|
|
|||
|
|
@ -65,14 +65,15 @@ class ActiveObjectFactoryBeanTest extends Spec with ShouldMatchers {
|
|||
assert(target.getSource === entry.value)
|
||||
}
|
||||
|
||||
it("should create an application context and inject a string dependency") {
|
||||
it("should create an application context and verify dependency injection") {
|
||||
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)
|
||||
val pojoInf = ctx.getBean("pojoInf").asInstanceOf[PojoInf];
|
||||
println("pojoInf = " + pojoInf.getString)
|
||||
assert(pojoInf.getString == "akka rocks")
|
||||
assert(pojoInf.gotApplicationContext)
|
||||
}
|
||||
|
||||
it("should stop the created active object when scope is singleton and the context is closed") {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue