I'd like to expose a component's interface as an interface and the implementing class would be package protected (and maybe in some other package):
package baz.iface
interface Foo {
void bar();
}
package baz.whatever
@Component
class SpringyFoo implements baz.iface.Foo {
public void bar() { frobnicate(); }
}
Assuming baz.whatever
is in the component-scan
, will Spring be able to autowire a baz.iface.Foo
somewhere else?
class FooClient {
@Autowired
private baz.iface.Foo;
}
No, components don't have to be public. The only requirement is that they have a no-arg constructor.
No. It is used to explicitly declare a single bean, rather than letting Spring do it automatically. If any class is annotated with @Component it will be automatically detect by using classpath scan. We should use @bean, if you want specific implementation based on dynamic condition.
@Component is a class-level annotation. It is used to denote a class as a Component. We can use @Component across the application to mark the beans as Spring's managed components. A component is responsible for some operations.
No, components dont have to be public. The only requirement is that they have a no-arg constructor.
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