What is Spring?
Spring is a framework for building Java applications
How can a configuration class import another configuration class?
@Import({a.class, b.class})
What is the default scope of a bean?
Singleton
How can you change the scope of a bean?
@Scope(...)
Describe how a prototype bean works
A new bean is created anytime it is referenced
How can you autowire a dependency only if it exists?
@Autowired(required=false)
For non-Spring Boot applications, if @Component is used, what other annotation must be used on the configuration class?
@ComponentScan(…)
What problem occurs in the following code and how can you fix it?
@Component("myService")
class MyServiceImpl implements MyService {
@Autowired
MyServiceImpl(MyRepository myRepository) {...}
}
---------------------------------------------------------------------------
@Component("jdbcMyRepository")
class JdbcMyRepository implements MyRepository {...}
---------------------------------------------------------------------------
@Component("jpaMyRepository")
class JpaMyRepository implements MyRepository {...}Ambiguity problem since MyServiceImpl(MyRepository ...) does not know which implementation to use. This can be solved by:
MyServiceImpl(@Qualifier("jpa") MyRepository myRepository) {...}
-----------------------------------------------------------------------------------------
@Component("jpaMyRepository")
@Qualifier("jpa") // or @Primary
class JpaMyRepository implements MyRepository {...}What are the 6 stereotype annotations?
True or false:
ApplicationContext is a subtype of BeanFactory
True
Describe the 5 different phases of a Spring bean’s lifecycle
The Environment bean has access to the 3 following property sources…
What are the 2 different ways to access property values inside a configuration class?
@PropertySource(...)
@Configuration
public class MyConfig {...}When might you need to use @PropertySource(…)?
When you need to access the properties of a file (Spring Framework)
When you need to access the properties of a file that is not located in application.properties (Spring Boot)
True or False
@Profile(…) can be used on both bean class and bean method definitions
True
What is a bean?
A bean is an object managed by the Spring IOC container (ApplicationContext)
Should a bean method definition return an interface type or a class type?
Interface type
Always remember, “depend on abstractions, not concretions”
What is a Project Object Model?
POM is an xml file containing all the information Maven needs to build an application
BeanPostProcessor
Which bean is responsible for creating bean proxies?
BeanPostProcessor
What does @Autowired do?
Injects dependencies by type