Tutorial by Examples

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;...
To declare a bean, simply annotate a method with the @Bean annotation or annotate a class with the @Component annotation (annotations @Service, @Repository, @Controller could be used as well). When JavaConfig encounters such a method, it will execute that method and register the return value as a b...
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...
In order to dynamically decide what beans to inject, we can use FactoryBeans. These are classes which implement the factory method pattern, providing instances of beans for the container. They are recognized by Spring and can be used transparently, without need to know that the bean comes from a fac...
The container creates a singleton bean and injects collaborators into it only once. This is not the desired behavior when a singleton bean has a prototype-scoped collaborator, since the prototype-scoped bean should be injected every time it is being accessed via accessor. There are several solution...
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...

Page 1 of 1