When using Java 9 module system, you need to pay attention to naming specifications, packaging control, dependency management, automatic module usage and layered design. It is recommended to adopt a reverse domain name style such as com.example.mymodule to avoid conflicts and enhance recognition; try to be clear in semantics, do not use vague or duplicate names, and do not start with numbers or special characters. Exports and requirements should be clearly defined inside the module, only necessary packages should be exported, dependencies should be explicitly declared, and maintainability should be improved and coupling should be prevented. Use automatic modules with caution, and give priority to modular libraries. If necessary, confirm the stability of their module name and avoid mapping conflicts. The correct --module-path should be configured in the build tool instead of the old classpath. The module structure should be reasonably layered, such as placing the interface, implementation, and main program in different modules to improve project clarity and scalability. Plan the module boundaries in advance to ensure convenient long-term maintenance.
The module system (Jigsaw) introduced in Java 9 makes the project structure clearer and the dependency management clearer, but there are actually some things to do with it well. This article does not talk about principles, but only practical suggestions.

Module naming should be standardized, so don't save trouble
Although the module name is just a string, it will affect the maintainability of the entire project. It is recommended to use reverse domain name styles uniformly, such as com.example.mymodule
, which can avoid naming conflicts and facilitate identification of attribution.
Some people like to call core
or utils
directly, which is OK in small projects, but it is easy to get confused when collaborating with large teams or multiple people. If you have to use a short name, at least prefix it, for example com.example.core
.

- Try to be semantic and clear
- Avoid duplicate or blurred module names
- Don't start with numbers or strange characters
Clearly exports and requirements, don't expose them all
One of the biggest benefits of a module system is its packaging capability. Don't exports
all packages for the sake of convenience. Only exposing the parts that really need to be visible to the outside, while the rest remains hidden inside can effectively prevent misuse and excessive coupling.
At the same time, requires
must be written clearly, especially when calling across modules. If you rely on a module, be sure to declare it explicitly and do not expect the JVM to handle it automatically.

For example:
module com.example.service { require com.example.api; exports com.example.service.impl; }
In the above code:
- Only
impl
package is exported, and other packages are not visible to the public by default. - It explicitly relies on the
com.example.api
module
In this way, others will know what your module depends on and what services you provide.
Be careful when using automatic modules
Some old projects or third-party libraries are not modular yet, so Java allows you to introduce them through automatic modules
. Although convenient, this approach is risky: the JVM automatically infers module names, which can lead to conflicts or difficult to trace.
suggestion:
- Try to use modular libraries
- If you have to use automatic module, it is best to confirm whether the automatically inferred module name is stable
- Avoid multiple libraries mapping to the same automatic module name
Also, when using JPMS in build tools such as Maven or Gradle, be careful to configure the correct module path ( --module-path
) instead of the old classpath ( -classpath
).
Layered design of module structure, don't make it a whole pot of porridge
The module system does not allow you to tear a bunch of files at will, but encourages you to do reasonable layering. For example, a common approach is to separate the interface and implementation into different modules, like this:
-
com.example.api
: defines interface, public type -
com.example.service
: implement specific logic -
com.example.app
: Main program entry, combining various modules
This structure is clear and easy to maintain, and is also convenient for testing and replacement implementation.
If you make a big module from the beginning, it will be very troublesome to split it later. Planning the module boundaries in advance is very important for long-term maintenance.
Basically that's it. The module system itself is not complicated, but when it is really implemented in the project, you still have to pay attention to the details, otherwise it will easily become "it seems to have used modules, but it is actually the same as before."
The above is the detailed content of Java Module System (Jigsaw) Best Practices. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Lazy loading only queries when accessing associations can easily lead to N 1 problems, which is suitable for scenarios where the associated data is not determined whether it is needed; 2. Emergency loading uses with() to load associated data in advance to avoid N 1 queries, which is suitable for batch processing scenarios; 3. Emergency loading should be used to optimize performance, and N 1 problems can be detected through tools such as LaravelDebugbar, and the $with attribute of the model is carefully used to avoid unnecessary performance overhead.

UseaRESTAPItobridgePHPandMLmodelsbyrunningthemodelinPythonviaFlaskorFastAPIandcallingitfromPHPusingcURLorGuzzle.2.RunPythonscriptsdirectlyfromPHPusingexec()orshell_exec()forsimple,low-trafficusecases,thoughthisapproachhassecurityandperformancelimitat

Python's memory management is based on reference counting and garbage collection mechanisms. 1. The reference counting mechanism ensures that objects are released immediately when the reference number is 0. The return value of sys.getrefcount() is 1 more than the actual reference because it increases its reference itself; 2. Circular references cannot be cleaned through reference counting, and it depends on the generational recycling of the gc module. Calling gc.collect() can recycle unreachable objects; 3. In actual development, long-term holding of large object references should be avoided. We can use weakref weak references, timely place None to release memory, and use tracemalloc to monitor memory allocation; 4. Summary: Python combines reference counting and garbage collection to manage memory, developers can use tools and optimize reference pipes.

Laravel supports the use of native SQL queries, but parameter binding should be preferred to ensure safety; 1. Use DB::select() to execute SELECT queries with parameter binding to prevent SQL injection; 2. Use DB::update() to perform UPDATE operations and return the number of rows affected; 3. Use DB::insert() to insert data; 4. Use DB::delete() to delete data; 5. Use DB::statement() to execute SQL statements without result sets such as CREATE, ALTER, etc.; 6. It is recommended to use whereRaw, selectRaw and other methods in QueryBuilder to combine native expressions to improve security

Responsive programming implements high concurrency, low latency non-blocking services in Java through ProjectReactor and SpringWebFlux. 1. ProjectReactor provides two core types: Mono and Flux, supports declarative processing of asynchronous data flows, and converts, filters and other operations through operator chains; 2. SpringWebFlux is built on Reactor, supports two programming models: annotation and functional. It runs on non-blocking servers such as Netty, and can efficiently handle a large number of concurrent connections; 3. Using WebFlux Reactor can improve the concurrency capability and resource utilization in I/O-intensive scenarios, and naturally supports SSE and WebSo.

MapStruct is a compile-time code generator used to simplify mapping between JavaBeans. 1. It automatically generates implementation classes by defining interfaces to avoid manually writing lengthy set/get mapping code; 2. It has type-safe, no runtime overhead, supports automatic mapping of the same name fields, custom expressions, nested objects and collection mapping; 3. It can be integrated with Spring and uses @Mapper(componentModel="spring") to inject mapper into Springbean; 4. Simple configuration, just introduce mapstruct dependencies and annotationProcessorPaths inserts

Use regular expression capture group in Notepad to effectively reorganize text. First, you need to open the replacement dialog box (Ctrl H), select "Search Mode" as "regular expression", 1. Use () to define the capture group, such as (\w ) to capture words; 2. Use \1 and \2 to reference the corresponding group in the replacement box; 3. Example: Exchange the name "JohnDoe" as "Doe, John", find (\w )\s (\w ), replace it with \2,\1; 4. Date format conversion 2023-12-25 to 25/12/2023, find (\d{4})-(\d{2})-(\d{2}), replace it with \3/\2/\1; 5. Log reordering can extract time, level, ID and other information

UseefficientdatastructureslikeArrayListoverLinkedListandprimitivecollectionstoreduceoverhead;2.Minimizeobjectcreationbyreusingobjects,usingStringBuilderforconcatenation,andcachingexpensiveobjects;3.Preventmemoryleaksbynullifyingreferences,usingstatic
