| Start |
|---|
| Autowiring all beans of a specific type 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: Interface: Name Validator Class: Email Validator Class: You can now autowire these validators individually or together into a class. Interface: Class: It's worth noting that if you have more than one implementation of an interface in the Spring IoC container and don't specify which one you want to use with the 1: This is not the right way to do such simple validations. This is a simple example about autowiring. If you want an idea of a much easier validation method look up how Spring does validation with annotations. |
| Basic annotation autowiring Interface: Class: It should be noted that a class must implement an interface for Spring to be able to autowire this class. There is a method to allow Spring to autowire stand-alone classes using load time weaving, but that is out of scope for this example. You can gain access to this bean in any class that instantiated by the Spring IoC container using the Usage: The This annotation can be applied in several different ways. Constructor injection: Field injection: Setter injection: |
| Declaring Bean To declare a bean, simply annotate a method with the When JavaConfig encounters such a method, it will execute that method and register the return value as a bean within a BeanFactory. By default, the bean name will be that of the method name. We can create bean using one of three ways: Now all three beans with name |
| Using FactoryBean for dynamic bean instantiation In order to dynamically decide what beans to inject, we can use Configuration: Getting the bean: To get the actual FactoryBean, use the ampersand prefix before the bean's name: Please note that you can only use Note that scoping refers to the actual instances being created, not the factory bean itself. |
| Inject prototype-scoped beans into singletons 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 solutions to this problem:
Approaches #3 and #4 are generally discouraged, since they strongly tie an app to Spring framework. Thus, they are not covered in this example. Lookup method injection via XML configuration and an abstract method Java Classes XML Lookup method injection via Java configuration and @Component Java Classes Java configuration Manual lookup method injection via Java configuration Java Classes Java configuration Injection of a protoype-scoped bean into singleton via Java classes XML The same approaches can be used for other scopes as well (e.g. for injection a request-scoped bean into singleton). |
| Autowiring specific bean instances with @Qualifier 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: Interface: Name Validator Class: Email Validator Class: You can now autowire these validators individually into a class. Interface: Class: It's worth noting that if you have more than one implementation of an interface in the Spring IoC container and don't specify which one you want to use with the 1: This is not the right way to do such simple validations. This is a simple example about autowiring. If you want an idea of a much easier validation method look up how Spring does validation with annotations. |
| Autowiring specific instances of classes using generic type parameters 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: Foo Validator Class: Bar Validator Class: You can now autowire these validators using type parameters to decide which instance to autowire. Interface: Class: |
Read Article
https://develop.horje.com/learn/1434/reference