Spring Framework Interview Notes : Part Two Wiring
The Spring container is the guy responsible to get the train moving when working with the Spring framework. The dependency magic gets the things working by somehow getting all the stuff up and running.
Now somehow we need to indicate to the spring container as to where and how to “wire” these beans. Remember these beans are also known as components. Components combine to form the structure and thus make the functionality. The concept of component scanning or component finding comes into the picture, literally searching the applications to find the beans required to create these beans. @ComponentScan is the annotation used by giving the the base packages where these components are defined using annotation or xml .

Spring Framework Interview Wiring
You can also use Autowiring with its attributes, which finds the beans by-name or by-type and injects them appropriately.
@Component, @ComponentScan, @Autowired, @Named, @Bean with requires and qualifier attributes are important to use for proper bean wiring in Spring.
You can actually make it easier for Spring to autowire a dependency by providing @Primary annotation for beans to inject a specific bean in case more than one bean of same type are found during autowiring.
@Qualifier actually names a bean and thus you can ask for an specific bean to be injected.
Also these beans are working in scopes default being the Singleton, if multiple instance are needed use Prototype. For web applications Session and Request are helpful are intuitive enough to understand. @Scope is the annotation used to work with these scopes in java configuration files.
Sometimes we need ways to inject values at runtime, the “env” which use to be read in basic java using the system property can be simulated using the @PropertySource annotation giving it the the file location.
Comments
Post a Comment