Video in progress
What is the difference between exports and opens in module-info.java, and which one do Spring or Hibernate need?
exports makes a package's public types visible to other modules, both at compile time and at runtime. opens opens the way to deep reflection (private members, setAccessible) at runtime. Frameworks usually need opens.
📖 Answer
module com.shop.orders {
requires spring.context;
requires jakarta.persistence;
exports com.shop.orders.api; // others can import it
opens com.shop.orders.domain // Hibernate fills in private fields
to org.hibernate.orm.core;
opens com.shop.orders.config to spring.core, spring.beans;
}
| Visible at compile time | Reflection on a public member | Reflection on a private member | |
|---|---|---|---|
| neither | no | no | no |
exports |
yes | yes | no |
opens |
no | yes | yes |
| both | yes | yes | yes |
Why frameworks need opens: Spring writes into private @Autowired fields, Hibernate fills entity fields directly, and Jackson reads private fields. exports doesn't give you that.
opens ... to X opens only to a specific module, which is better encapsulation. You can also open the whole module with open module: it's quick, but everything stays open.
To be honest, many Spring Boot applications don't write a module-info.java at all: everything is on the classpath, inside the "unnamed module". Modules are more useful for library authors and for building a small runtime with jlink.
Sign up to mark studied questions and save your progress.