Advanced Autowire Features in Spring 4

Advanced Autowire Features in Spring 4

0 Comments

At the heart of the Spring Framework is its support of dependency injection through its Inversion of Control container. In this video, I look at using some of the advanced autowire features of Spring.

@Primary

By default, Spring will autowire by type. When you have more than one Spring Bean of a given type, you can use the @Primary annotation to give a specific bean preference over the others. If Spring cannot determine which bean should be wired by type when more than one is defined, the Spring context will fail on startup with an org.springframework.beans.factory.NoUniqueBeanDefinitionException  exception.

@Qualifier

You also have the option of using the @Qualifier annotation in conjunction with the @Autowired annotation to control how beans are autowired in Spring. While the default behavior of Spring is to autowire by type. The Qualifier allows you to specify the id or name of the bean you wish autowired into the bean.

Demonstration

The following video demonstration is a module from my Spring Core online course. In this video, I show you how to work with Spring’s autowire by type. Then show you how to fine-tune Spring’s Autowire functionality through the use of Spring Profiles, the @Primary annotation, and the @Qualifier annotation.

While this is a very simple demonstration, I hope you can see the amount of control you have when you are configuring Spring to autowire beans in your application. It’s not uncommon to be dealing with more than one datasource. Naturally, you’re going to wish to have control over which datasource is autowired into your bean.

 

About jt

    You May Also Like

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.