1.Spring Life cycle, IOC, Spring Container?

2. Difference between @Configuration and @Component?

3. Uses of Spring MVC Context Listener interface?

4. How URL authentication works?

5. What happens if you define the same Spring bean twice with same name ?

https://stackoverflow.com/questions/10993181/defining-the-same-spring-bean-twice-with-same-name

6. What is the difference between @Bean and @Component

https://stackoverflow.com/questions/10604298/spring-component-versus-bean
https://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02s02.html
https://stackoverflow.com/questions/38088977/spring-component-bean-annotation
https://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/html/beans.html#beans-stereotype-annotations

7. Can we use @Component annotation instead of @Service for Business Services?

https://stackoverflow.com/questions/12204035/what-is-persistence-exception-translation-for-repository-beans

8. Autowire Disambiguation

By default, Spring resolves @Autowired entries by type. If more than one beans of the same type are available in the container, the framework will throw a fatal exception indicating that more than one bean is available for autowiring.

@Component("fooFormatter")
public class FooFormatter implements Formatter {
public String format() {
return "foo";
}
}
@Component("barFormatter")
public class BarFormatter implements Formatter {
public String format() {
return "bar";
}
}
public class FooService {
    
    @Autowired
    private Formatter formatter;
}

 

Since there are two concrete implementations of Formatter available for the Spring container to inject, Spring will throw a NoUniqueBeanDefinitionException exception when constructing the FooService:

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type [com.autowire.sample.Formatter] is defined:
expected single matching bean but found 2: barFormatter,fooFormatter

This can be avoided by narrowing the implementation using a @Qualifier annotation:

public class FooService {
    
    @Autowired
    @Qualifier("fooFormatter")
    private Formatter formatter;
}
By specifying the @Qualifier with the name of the specific implementation, in this case as fooFormatter, we can avoid ambiguity when Spring finds multiple beans of the same type.

Please note that the value of the @Qualifier annotation matches with the name declared in the @Component annotation of our FooFormatter implementation.

9. How do we inject different bean depending on the configuration in application.properties?

Consider the example

interface GreetingService {
public String sayHello();
}

Two components

@Component(value="real")
class RealGreetingService implements GreetingService {
	public String sayHello() {
		return "I'm real";
	}
}

 
@Component(value="mock")
class MockGreetingService implements GreetingService {
	public String sayHello() {
		return "I'm mock";
	}
}

application.properties

application.greeting: real 

Adding @Resource with the name of the property

@RestController
public class WelcomeController {
 
@Resource(name="${application.greeting}")
private GreeterService service1;