Java geek
Related reading:
JAVA programming ideas (2) How to interface programming JAVA programming ideas (3) remove the awkward if, self-registration strategy mode elegant meet the open and close principle of JAVA programming ideas (4) Builder mode classic paradigm and factory mode how to choose? Java Programming Ideas (5) Event notification mode decoupling process (6) Event Notification mode decoupling process Java programming ideas (7) The use of composition and inheritance scenarios Java basics (3) ClassLoader implementation of hot loading Java concurrent programming introduction (11) flow limiting scenarios and Spring flow limiting device HikariPool source code (two) design ideas for reference in the workplace (a) IT big factory survival rules
1. How does dependency injection improve scalability
Extensibility is improved when an interface is injected and a specific interface implementation class is injected by the caller.
2. Timing of dependency injection
In Spring, dependency injection works in the following ways:
- Through class member injection
- Injection through the constructor
- Injection through method parameters
Ordinary Java programs can also be injected in these ways, so when and which dependency injection methods are appropriate? This should be considered from the following aspects:
scalability
With property injection, the injected class is already written dead at the time of class definition and cannot be changed. Constructor injection and method injection, on the other hand, can be passed in by the caller at invocation time, increasing extensibility.
The life cycle
Property injection and constructor injection mean that the injected class’s life cycle is the same as that of the host class. The injected class exists as long as the host class exists. By method injection, however, it means that the injected class is normally used only within the method. When the method ends, the injected class’s life cycle ends, freeing up memory.
Based on these two points, it is easy to determine when and which injection method should be used.
3. Examples of dependency injection to improve scalability
Let’s look at how dependency injection can improve extensibility through the ThreadPoolExecutor class in the Java JUC package.
class | Duties and responsibilities |
---|---|
ThreadPoolExecutor | Thread executor |
BlockingQueue | The buffer queue used by the thread pool. The queue length determines the maximum number of worker threads that can be buffered |
Runnable | Runnable in thread queue |
ThreadFactory | Create thread factories for worker threads |
RejectedExecutionHandler | Process policy interface for adding new threads when the thread queue in thread executor is full |
DiscardOldestPolicy | Discard the oldest pending worker thread in the thread queue and add a new worker thread. |
CallerRunsPolicy | Repeat until successful and use in closeConnectionExecutor. |
AbortPolicy | Discard worker threads that exceed thread queue load and throw an exception. |
DiscardPolicy | Worker threads that are complex beyond the thread queue are ignored and no processing is done. |
The injection mode of each interface is constructor injection:
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
Copy the code
4. To summarize
- Dependency injection is the best way to increase extensibility by leaving it to the caller with more freedom, rather than providing extensibility through multiple default implementations, which are not comparable in scale.
- Depending on the scenario, it is reasonable to determine whether the dependency injection method is constructor injection or method injection.
- Don’t use dependency injection all the time just because it provides great extensibility. Use it only when you need it, as applicable.
end.
<– Thanks for the triple punch, left likes and followings.