Tutorial by Examples: autowiring

If you've got multiple implementations of the same interface, Spring can autowire them all into a collection object. I'm going to use an example using a Validator pattern1 Foo Class: public class Foo { private String name; private String emailAddress; private String errorMessage;...
Autowiring is done using a sterotype annotation to specify what classes are going to be beans in the ApplicationContext, and using the Autowired and Value annotations to specify bean dependencies. The unique part of autowiring is that there is no external ApplicationContext definition, as it is all...
Interface: public interface FooService { public int doSomething(); } Class: @Service public class FooServiceImpl implements FooService { @Override public int doSomething() { //Do some stuff here return 0; } } It should be noted that a class must imple...
If you've got multiple implementations of the same interface, Spring needs to know which one it should autowire into a class. I'm going to use a Validator pattern in this example.1 Foo Class: public class Foo { private String name; private String emailAddress; private String erro...
If you've got an interface with a generic type parameter, Spring can use that to only autowire implementations that implement a type parameter you specify. Interface: public interface GenericValidator<T> { public T validate(T object); } Foo Validator Class: @Component public class...
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