Looks like no one added any tags here yet for you.
What are we using interfaces for?
Interfaces are used for decoupling implementations of funcionality
Rather than implement SorterByAddress we can use Sorter interface that is used in object that we need. So the refactor later is simpler and thing are not coupled and hardwired.
What is service naming used for?
Objects implementing uses cases. e.g. object implementing publishing comments (CommentsService)
What is repository naming used for?
Object that work directly with database. Sometime referred as DAO (data access object). CommentRepository
What are proxy naming used for?
Objects those responsibility is establish communication with something outside of the app. Like API … NotificationProxy.
what is the way to add multiple bean packages?
We can use annotation @ComponentScan(basePackages = {…}
If there is multiple beans that implement same interface. How we can decide which one is the correct that needs to be used in dependency?
We use @Primary or @Qualifier annotation.
Using the @Primary annotation to mark one of the beans for kimplementation as the default
Using the @Qualifier annotation to name a bean and then refer to it by its name for DI
How to properly use @Qualifier for multiple beans of the same type?
U annotate the class you want to use with @Qualifier(“name”) and then you can inject a specific bean with @Qualifier(“name”) again where dependency is needed.
Bean we want to inject:
@Component
@Qualifier("PUSH")
public class CommentPushNotificationProxy implements CommentNotificationProxy {
@Override
public void sendComment(Comment comment) {
System.out.println("Sending push notification for comment: " + comment.getText());
};
}
DI:
@Component
public class CommentServiceImpl implements CommentService {
private final CommentRepository commentRepository;
private final CommentNotificationProxy commentNotificationProxy;
public CommentServiceImpl(CommentRepository commentRepository, @Qualifier("PUSH") CommentNotificationProxy commentNotificationProxy) {
this.commentRepository = commentRepository;
this.commentNotificationProxy = commentNotificationProxy;
}
@Override
public void publishComment(Comment comment) {
commentRepository.storeComment(comment);
commentNotificationProxy.sendComment(comment);
}
}
Singleton scope vs singleton pattern?
In spring singlton scope means we have only one instance with the same name but the type of the bean can be of the same type.
in singlton pattern you can have only one instance of the same type
What is the default scope in spring application if we dont set it explicitly?
Its singleton scope
What are the cons of using singleton scope and what is usually used for?
Because multiple components can share same object instance these beans needs to be immutable.
When app is multithreaded and bean would be mutable we can cause race condition Singleton components are not mean to be synchronized.
They’re commonly used to define an app’s backbone class design and delegate responsibilities one to another.
What are 3 rules for using beans in spring?
Make an object bean in the Spring context only if you need Spring to manage it so that the framework can augment that bean with a specific capability. If the object doesn’t need any capability offered by the framework, you don’t need to make it a bean.
If you need to make an object bean in the Spring context, it should be singleton only if it’s immutable. Avoid designing mutable singleton beans.
If a bean needs to be mutable, an option could be to use the prototype scope.
What are type of instantiation for beans?
lazy and eager.
My advice is to go with the default, which is an eager instantiation. This approach generally brings more benefits.
In most cases, it’s more comfortable to let the framework create all the instances at the beginning when the context is instantiated (eager)
In a lazy instantiation, the framework has to first check if the instance exists and eventually create it if it doesn’t,
so from the performance point of view, it’s better to have the instances in the context already (eager) because it spares some checks the framework needs to do when one bean delegates to another
Another advantage of eager instantiation is when something is wrong and the framework cannot create a bean; we can observe this issue when starting the app.
How prototype scopes works?
Everytime you request reference to prototype scoped bean is new instance created and lifecycle is managed by spring
They are good when we need mutate objects of bean.
With prototype beans, we no longer have concurrency problems because each thread that requests the bean gets a different instance, so defining mutable prototype beans is not a problem
Comparion singleton vs instance?