@Embeddedโ
Clawject provides @Embedded
decorator that allows to register elements from a plain object as a beans.
Note that
@Embedded
decorator can't be applied to class-constructor Bean foo = Bean(Foo)
.
Embedding object into contextโ
All properties of an object that is declared in IFoo
will be available as a dependency in a configuration,
and the IFoo
object itself will be available as a bean.
interface IFoo {
fooProperty: string;
}
const foo: IFoo = { fooProperty: 'fooValue' };
@ClawjectApplication
class Application {
@Bean
@Embedded
fooBean = foo;
@PostConstruct
postConstruct(
fooObject: IFoo, // <- foo will be injected here
fooBeanFooProperty: string // <- foo.fooProperty will be injected here
) {}
}
Namingโ
If you have more than one bean with the same type, and you want to inject bean specifically from an Embedded
bean,
you should specify the correct name for the dependency.
In the embedded beans name will be defined as a class property name + property name from an embedded object with a first letter capitalized.
Let's look at the example:
interface IEmbedded {
stringBean: string;
}
@ClawjectApplication
class Application {
@Bean
@Embedded
embeddedBean: IEmbedded = { stringBean: 'embeddedStringBean' };
stringBean = 'stringBeanValue';
@PostConstruct
postConstruct(
stringBean: string, // <- stringBean will be injected here
embeddedBeanStringBean: string // <- embeddedBean.stringBean will be injected here
) {}
}