Add autowiring of application contexts on registration

This commit is contained in:
Mike Bierlee 2015-12-24 20:31:26 +01:00
parent 2e4c688ab2
commit f206b594c8
2 changed files with 76 additions and 0 deletions

View file

@ -373,6 +373,7 @@ synchronized class DependencyContainer {
auto context = new Context(); auto context = new Context();
context.registerDependencies(this); context.registerDependencies(this);
context.registerContextComponents(this); context.registerContextComponents(this);
autowire(this, context);
} }
/** /**

View file

@ -22,6 +22,41 @@ version(unittest) {
} }
} }
class AutowiredTestContext : ApplicationContext {
@Autowire
public UnrelatedClass unrelatedClass;
@Component
public ClassWrapper wrapper() {
return new ClassWrapper(unrelatedClass);
}
}
class ComplexAutowiredTestContext : ApplicationContext {
@Autowire
public UnrelatedClass unrelatedClass;
@Autowire
public ClassWrapper classWrapper;
public override void registerDependencies(shared(DependencyContainer) container) {
container.register!UnrelatedClass;
}
@Component
public ClassWrapper wrapper() {
return new ClassWrapper(unrelatedClass);
}
@Component
public ClassWrapperWrapper wrapperWrapper() {
return new ClassWrapperWrapper(classWrapper);
}
}
interface TestInterface { interface TestInterface {
} }
@ -129,6 +164,22 @@ version(unittest) {
public Wants wants; public Wants wants;
} }
class ClassWrapper {
public Object someClass;
this(Object someClass) {
this.someClass = someClass;
}
}
class ClassWrapperWrapper {
public ClassWrapper wrapper;
this(ClassWrapper wrapper) {
this.wrapper = wrapper;
}
}
// Test register concrete type // Test register concrete type
unittest { unittest {
shared(DependencyContainer) container = new DependencyContainer(); shared(DependencyContainer) container = new DependencyContainer();
@ -514,4 +565,28 @@ version(unittest) {
assert(instance !is null); assert(instance !is null);
} }
// Test autowiring application context
unittest {
shared(DependencyContainer) container = new DependencyContainer();
container.register!UnrelatedClass;
container.registerContext!AutowiredTestContext;
auto instance = container.resolve!ClassWrapper;
assert(instance !is null);
assert(instance.someClass !is null);
}
// Test autowiring application context with dependencies registered in same context
unittest {
shared(DependencyContainer) container = new DependencyContainer();
container.registerContext!ComplexAutowiredTestContext;
auto instance = container.resolve!ClassWrapperWrapper;
auto wrapper = container.resolve!ClassWrapper;
auto someClass = container.resolve!UnrelatedClass;
assert(instance !is null);
assert(instance.wrapper is wrapper);
assert(instance.wrapper.someClass is someClass);
}
} }