#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
|
|
@ -48,6 +48,20 @@ class ActiveObjectFactoryBean extends AbstractFactoryBean[AnyRef] with Logging w
|
||||||
@BeanProperty var scope:String = VAL_SCOPE_SINGLETON
|
@BeanProperty var scope:String = VAL_SCOPE_SINGLETON
|
||||||
@BeanProperty var property:PropertyEntries = _
|
@BeanProperty var property:PropertyEntries = _
|
||||||
@BeanProperty var applicationContext:ApplicationContext = _
|
@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()
|
* @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()
|
* @see org.springframework.beans.factory.config.AbstractFactoryBean#createInstance()
|
||||||
*/
|
*/
|
||||||
def createInstance: AnyRef = {
|
def createInstance: AnyRef = {
|
||||||
if(scope.equals(VAL_SCOPE_SINGLETON)) {
|
|
||||||
setSingleton(true)
|
|
||||||
} else {
|
|
||||||
setSingleton(false)
|
|
||||||
}
|
|
||||||
var argumentList = ""
|
var argumentList = ""
|
||||||
if (isRemote) argumentList += "r"
|
if (isRemote) argumentList += "r"
|
||||||
if (hasInterface) argumentList += "i"
|
if (hasInterface) argumentList += "i"
|
||||||
|
|
@ -86,7 +95,11 @@ class ActiveObjectFactoryBean extends AbstractFactoryBean[AnyRef] with Logging w
|
||||||
super.destroy
|
super.destroy
|
||||||
}
|
}
|
||||||
|
|
||||||
private def setProperties(ref:AnyRef) : AnyRef = {
|
private def setProperties(ref:AnyRef) : AnyRef = {
|
||||||
|
if(hasSetDependecies) {
|
||||||
|
return ref
|
||||||
|
}
|
||||||
|
|
||||||
log.debug("Processing properties and dependencies for target class %s",target)
|
log.debug("Processing properties and dependencies for target class %s",target)
|
||||||
val beanWrapper = new BeanWrapperImpl(ref);
|
val beanWrapper = new BeanWrapperImpl(ref);
|
||||||
if(ref.isInstanceOf[ApplicationContextAware]) {
|
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")
|
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
|
ref
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -139,10 +154,12 @@ class ActiveObjectFactoryBean extends AbstractFactoryBean[AnyRef] with Logging w
|
||||||
if (transactional) config.makeTransactionRequired
|
if (transactional) config.makeTransactionRequired
|
||||||
config
|
config
|
||||||
}
|
}
|
||||||
|
def aNewInstance[T <: AnyRef](clazz: Class[T]) : T = {
|
||||||
private[akka] def aNewInstance[T <: AnyRef](clazz: Class[T]) : T = {
|
var ref = clazz.newInstance().asInstanceOf[T]
|
||||||
clazz.newInstance().asInstanceOf[T]
|
setProperties(ref)
|
||||||
}
|
hasSetDependecies = true
|
||||||
|
ref
|
||||||
|
}
|
||||||
|
|
||||||
private[akka] def isRemote = (host != null) && (!host.isEmpty)
|
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;
|
package se.scalablesolutions.akka.spring;
|
||||||
|
|
||||||
import org.springframework.context.ApplicationContextAware;
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
|
|
||||||
import se.scalablesolutions.akka.actor.annotation.shutdown;
|
import se.scalablesolutions.akka.actor.annotation.shutdown;
|
||||||
|
|
||||||
public class SampleBean implements ApplicationContextAware {
|
public class SampleBean {
|
||||||
|
|
||||||
public boolean down;
|
public boolean down;
|
||||||
|
|
||||||
public boolean gotApplicationContext;
|
|
||||||
|
|
||||||
public SampleBean() {
|
public SampleBean() {
|
||||||
down = false;
|
down = false;
|
||||||
gotApplicationContext = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setApplicationContext(ApplicationContext context) {
|
|
||||||
gotApplicationContext = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String foo(String s) {
|
public String foo(String s) {
|
||||||
|
|
|
||||||
|
|
@ -22,4 +22,12 @@
|
||||||
<bean id="string" class="java.lang.String">
|
<bean id="string" class="java.lang.String">
|
||||||
<constructor-arg value="someString"/>
|
<constructor-arg value="someString"/>
|
||||||
</bean>
|
</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)
|
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");
|
var ctx = new ClassPathXmlApplicationContext("appContext.xml");
|
||||||
val target:ResourceEditor = ctx.getBean("bean").asInstanceOf[ResourceEditor]
|
val target:ResourceEditor = ctx.getBean("bean").asInstanceOf[ResourceEditor]
|
||||||
assert(target.getSource === "someString")
|
assert(target.getSource === "someString")
|
||||||
|
|
||||||
val sampleBean = ctx.getBean("sample").asInstanceOf[SampleBean];
|
val pojoInf = ctx.getBean("pojoInf").asInstanceOf[PojoInf];
|
||||||
Thread.sleep(300)
|
println("pojoInf = " + pojoInf.getString)
|
||||||
assert(sampleBean.gotApplicationContext)
|
assert(pojoInf.getString == "akka rocks")
|
||||||
|
assert(pojoInf.gotApplicationContext)
|
||||||
}
|
}
|
||||||
|
|
||||||
it("should stop the created active object when scope is singleton and the context is closed") {
|
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