Add support for JSR330 @Inject attribute

This commit is contained in:
Mike Bierlee 2023-03-07 03:14:58 +03:00
parent 733c2aa236
commit 3fa4e15d6f
6 changed files with 40 additions and 1 deletions

View file

@ -1,8 +1,9 @@
# Poodinis Changelog # Poodinis Changelog
**Version 8.1.4** (??????) **Version 8.2.0** (??????)
- FIX some dlang deprecation warnings (#43). Some remain due to further issues in dlang/phobos. - FIX some dlang deprecation warnings (#43). Some remain due to further issues in dlang/phobos.
- ADD support for JSR330 @Inject attribute. It will be the preferred attribute from now on. However, @Autowire is not deprecated and will still work.
**Version 8.1.3** (27-10-2022) **Version 8.1.3** (27-10-2022)

View file

@ -69,6 +69,8 @@ private struct UseMemberType {
* The members of an instance of "HybridCar" will now be autowired properly, because the autowire mechanism will * The members of an instance of "HybridCar" will now be autowired properly, because the autowire mechanism will
* autowire member "fuelEngine" as if it's of type "FuelEngine". This means that the members of instance "fuelEngine" * autowire member "fuelEngine" as if it's of type "FuelEngine". This means that the members of instance "fuelEngine"
* will also be autowired because the autowire mechanism knows that member "fuelEngine" is an instance of "FuelEngine" * will also be autowired because the autowire mechanism knows that member "fuelEngine" is an instance of "FuelEngine"
*
* See_Also: Inject
*/ */
struct Autowire(QualifierType) { struct Autowire(QualifierType) {
QualifierType qualifier; QualifierType qualifier;

21
source/poodinis/inject.d Normal file
View file

@ -0,0 +1,21 @@
/**
* An analogue to JSR330
*
* Authors:
* Mike Bierlee, m.bierlee@lostmoment.com
* Copyright: 2014-2023 Mike Bierlee
* License:
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
*/
module poodinis.inject;
import poodinis.autowire : Autowire;
/**
* UDA for annotating class members as candidates for injection.
*
* See_Also: Autowire
*/
alias Inject = Autowire;

View file

@ -16,5 +16,6 @@ public import poodinis.container;
public import poodinis.context; public import poodinis.context;
public import poodinis.factory; public import poodinis.factory;
public import poodinis.imports; public import poodinis.imports;
public import poodinis.inject;
public import poodinis.registration; public import poodinis.registration;
public import poodinis.valueinjection; public import poodinis.valueinjection;

View file

@ -648,4 +648,14 @@ version (unittest) {
assert(instance.lala == 42); assert(instance.lala == 42);
assert(instance.lala(77) == 77); assert(instance.lala(77) == 77);
} }
// Test autowiring using @Inject attribute
unittest {
auto container = new shared DependencyContainer();
container.register!ComponentA;
container.register!WithInjectAttribute;
auto instance = container.resolve!WithInjectAttribute;
assert(instance.componentA is container.resolve!ComponentA);
}
} }

View file

@ -650,4 +650,8 @@ version (unittest) {
return valla; return valla;
} }
} }
class WithInjectAttribute {
public @Inject ComponentA componentA;
}
} }