Does Spring Java Configuration bean method calls get injected with managed beans at runtime? -
consider next code:
public class aimpl implements {} public class bimpl implements b { private final a; public b(a a){ this.a = a; } } @configuration public class myconfiguration { @bean public bean1(){ homecoming new aimpl(); } @bean public b bean2() { homecoming new bimpl(bean1()); } @bean public b bean3() { homecoming new bimpl(bean1()); } } would reference bean2 , bean3 have bean1 spring managed singleton or new instance? i.e. spring intercept method calls in java configuration class proxies proper application context?
update understanding, cglib library involved in java configuration classes modify bytecode, mean latter scenario true? haven't quite wrapped head around how works yet.
class a should represented in both bean2 , bean2 same object around same applicationcontext (especially if declaring beans methods in @configuration), can utilize dependency injections implemented in spring container. code in sentiment more clear , unequivocal, reader must know how autoriwiring works.
this code equivalently:
@configuration public class myconfiguration { @bean public bean1(){ homecoming new aimpl(); } @bean public b bean2(a bean1) { homecoming new bimpl(bean1); } @bean public b bean3(a bean1) { homecoming new bimpl(bean1); } } second way:
add @component , @inject/@autowired on declared classes (you can't in legacy code, without aop).
@component public class aimpl implements {} @component public class bimpl implements b { private final a; @inject public b(a a){ this.a = a; } } but solution need add together @componentscan(packages="name.of.your.package") @configuration class.
@componentscan(packages="name.of.your.package") @configuration public class myconfiguration { ... } java spring
No comments:
Post a Comment