14

I have a module-info.java file that looks like -

module foo.microservice {
    requires spring.core;
    requires spring.beans;
    requires spring.context;
    requires java.sql; // required for Spring Annotation based configuration :(

    opens foo.microservice.configuration to spring.core;
    opens foo.microservice.internal.services to spring.beans;
    opens foo.microservice.internal.controllers to spring.beans;

    exports foo.microservice.configuration;
    exports foo.microservice.controllers;
    exports foo.microservice.models;
    exports foo.microservice.services;
}

I'm hoping that it might be possible to allow Spring to access all internal packages without explicitly adding each one to the module-info.java.

Is there a way to have a more generic opens statement to allow all my internal packages (and sub-packages) to be accessible to spring.beans?

Naman
  • 27,789
  • 26
  • 218
  • 353
TK.
  • 46,577
  • 46
  • 119
  • 147
  • 'exports' and 'opens' have very different functionality within the module-info.java, so I didn't see these questions as duplicates of this one. – TK. Dec 18 '17 at 10:52
  • 5
    You can use `open module` to open all packages (internal or not) to all modules. I don't think there's any intermediate granularity. – Alexey Romanov Dec 18 '17 at 10:53
  • 1
    @nullpointer Nicolai's answer explains why it isn't allowed for `exports` and states this reason doesn't apply for `opens`. So no, I don't think it answers why it isn't allowed for `opens`. – Alexey Romanov Dec 18 '17 at 10:57

2 Answers2

11

Currently no, as the JLS defines a module declaration as a list of directives where each directive has the below syntax:

ModuleDirective:
     requires {RequiresModifier} ModuleName ;
     exports PackageName [to ModuleName {, ModuleName}] ;
     opens PackageName [to ModuleName {, ModuleName}] ;
     uses TypeName ;
     provides TypeName with TypeName {, TypeName} ; 

The same syntax applies for both exports and opens: no wildcards are allowed in the package name. Maybe that could be improved in the future, but I think it would be a bad practice, similar to the bad practice of using such wildcards in import statements.

M A
  • 71,713
  • 13
  • 134
  • 174
  • 3
    Plus one for the JLS link of the same. – Naman Dec 18 '17 at 11:01
  • 3
    The answer is correct, but I want to add that this does not have to be seen as unfortunate. Quite the opposite, I think that packages should always be explicitly and deliberately exported/opened. Furthermore, this would foster the misunderstanding packages with the same prefix are somehow related, which they aren't. – Nicolai Parlog Dec 18 '17 at 20:35
7

You can use open module to open all packages (internal or not) to all modules. I don't think there's any intermediate granularity.

open module foo.microservice {
  requires spring.core;
  requires spring.beans;
  requires spring.context;
  requires java.sql; // required for Spring Annotation based configuration :(

  exports foo.microservice.configuration;
  exports foo.microservice.controllers;
  exports foo.microservice.models;
  exports foo.microservice.services;
}

(copied from Alexey Romanov's comment)

ruediste
  • 2,434
  • 1
  • 21
  • 30