Best Practices and Design Patterns
1. What is the Dependency Injection design pattern?
Dependency Injection is a pattern where a class receives its
dependencies from outside rather than creating them itself.
Best Practices and Design Patterns
2. How does Spring implement Dependency Injection?
Spring creates objects and injects them into other objects that
need them. It can do this through constructors, setters, or fields.
Best Practices and Design Patterns
3. What are the benefits of using Dependency Injection?
It makes code more modular, easier to test, and easier to change.
It also reduces the coupling between classes.
Best Practices and Design Patterns
4. Can you give a simple example of Dependency Injection in
Spring?
Sure, here's a basic example:
Code Screen shot here...
Best Practices and Design Patterns
5. What's the difference between constructor and setter
injection?
Constructor injection is done when the object is created, while
setter injection happens after creation. Constructor injection is
often preferred as it ensures all required dependencies are set.
Best Practices and Design Patterns
6. How does Dependency Injection help with unit testing?
It allows you to easily replace real dependencies with mock
objects in tests, making it simpler to test a class in isolation.
Best Practices and Design Patterns
7. What is the Factory pattern?
The Factory pattern is a creational pattern that provides an
interface for creating objects without specifying their exact
classes.
Best Practices and Design Patterns
8. How does Spring use the Factory pattern?
Spring uses factory patterns in its BeanFactory and
ApplicationContext. These create and manage Spring beans.
Best Practices and Design Patterns
9. What's an example of a factory in Spring?
The FactoryBean interface is a good example. Classes that
implement this interface act as factories for other beans.
Best Practices and Design Patterns
10. How do you create a custom FactoryBean in Spring?
You create a class that implements the FactoryBean interface.
Here's a simple example:
Code Screen shot here...
Best Practices and Design Patterns
11. What's the difference between BeanFactory and
FactoryBean?
BeanFactory is a container that manages beans. FactoryBean is
an interface for beans that are themselves factories for other
beans.
Best Practices and Design Patterns
12. How does Spring's factory pattern help with complex object
creation?
It allows Spring to handle complex initialization logic and runtime
decisions about which objects to create.
Best Practices and Design Patterns
13. What is the Singleton pattern?
The Singleton pattern ensures a class has only one instance and
provides a global point of access to it.
Best Practices and Design Patterns
14. How does Spring implement the Singleton pattern?
By default, all Spring beans are singletons. Spring creates only
one instance of each bean and reuses it whenever that bean is
needed.
Best Practices and Design Patterns
15. How can you make a Spring bean not a singleton?
You can change the bean's scope. For example, you can use
@Scope("prototype") to create a new instance each time the
bean is requested.
Best Practices and Design Patterns
16. Is Spring's singleton the same as the traditional Singleton
pattern?
Not exactly. Spring's singleton is per container, while the
traditional Singleton is per classloader. Spring's approach is
often considered safer and more flexible.
Best Practices and Design Patterns
17. What are the benefits of Spring's singleton beans?
They save memory, improve performance by reusing objects, and
make it easier to manage shared resources.
Best Practices and Design Patterns
18. Are Spring singletons thread-safe?
Spring singleton beans are not automatically thread-safe. You
need to handle thread safety yourself if a singleton bean will be
used by multiple threads.
Best Practices and Design Patterns
19. Can you have a non-singleton bean depend on a singleton
bean?
Yes, this is a common scenario. The singleton bean will be
shared among all instances of the non-singleton bean.