Tutorial by Examples

Consider the following Java classes: class Foo { private Bar bar; public void foo() { bar.baz(); } } As can be seen, the class Foo needs to call the method baz on an instance of another class Bar for its method foo to work successfully. Bar is said to be a dependency for Foo sin...
The same examples as shown above with XML configuration can be re-written with Java configuration as follows. Constructor injection @Configuration class AppConfig { @Bean public Bar bar() { return new Bar(); } @Bean public Foo foo() { return new Foo(bar()); } } Property in...
Dependencies can be autowired when using the component scan feature of the Spring framework. For autowiring to work, the following XML configuration must be made: <context:annotation-config/> <context:component-scan base-package="[base package]"/> where, base-package is th...
Constructor injection through Java configuration can also utilize autowiring, such as: @Configuration class AppConfig { @Bean public Bar bar() { return new Bar(); } @Bean public Foo foo(Bar bar) { return new Foo(bar); } }

Page 1 of 1