上QQ阅读APP看书,第一时间看更新
Best practices for configuring the DI pattern
The following are the best practices for configuring the DI pattern:
- Configuration files should be separated categorically. Application beans should be separate from infrastructure beans. Currently, it's a bit difficult to follow.
- Always specify the component name; never rely on generated names by the container.
- It is a best practice to give a name along with a description of what the pattern does, where to apply it, and the problems it addresses.
- The best practices for component scanning are as follows:
- The components are scanned at startup, and it scans the JAR dependencies as well.
- Bad practice: It scans all the packages of com and org. It increases the startup time of the application. Avoid such type of component scanning:
@ComponenttScan (( {{ "org", "com" }} ))
-
- Optimized: It scans only the specific packages as defined by us.
@ComponentScan ( {
"com.packt.patterninspring.chapter4.
bankapp.repository", "com.packt.patterninspring.chapter4.bankapp.service"}
)
- Best practices in choosing implicit configuration:
- Choose annotation-based configurations for frequently changing beans
- It allows for very rapid development
- It is a single place to edit the configuration
- Best practices in choosing explicit via Java configuration:
- It is centralized in one place
- Strong type checking enforced by the compiler
- Can be used for all classes
- Spring XML Best Practices: XML has been around for a long time, there are many shortcuts and useful techniques available in XML configuration as well, they are listed follow:
- factory-method and factory-bean attributes
- Bean Definition Inheritance
- Inner Beans
- p and c namespaces
- Using collections as Spring beans