Lets assume that we have 3 classes:
@Component
public class A {
}
@Component
public class B {
}
@Component
public class C {
@Autowired
public C(A a, B b) { }
}
By default, each bean sees any other defined bean:
What I want to achieve is to limit visibility of bean A:
I thought that I can create two contexts: common, which holds only A bean definition, and child context which sees all beans defined in the first context and also declares its own beans (B and C). Unfortunately, I didn't find any way to do this with Java Config.
Do you know any way to achieve such solution?
I'm not sure if it's OK if C can see A (looks like maybe that's desired and there is just a typo). If it is OK, then I think the following will work:
Since you are using Spring annotations, then the beans can be package scoped.
http://sahits.ch/blog/blog/2014/02/16/package-private-beans/
That should do it:
package the.first
@Component
public class A {
}
package the.second
@Component
//package protected
class B {
}
package the.second
@Component
//package protected
class C {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With