Customizing modules before starting

Sometimes it might be useful to modify certain semantics of a module.

For example:

  • You might want to inject extra configuration classes into another module.

  • You might want to expose some extra classes from another module.

On this page, you will:

  • Learn about AcrossBootstrapConfigurer to customize a module.

  • Use @ModuleConfiguration to customize a module.

AcrossBootstrapConfigurer

To modify the Across context configuration or a bootstrapped module the AcrossBootstrapConfigurer interface can be implemented. Auto-configuration compatibility adapters often implement this interface to inject the configurations into the relevant modules.

For the following example we will use the AdminWebModule to enhance the standard security configuration.

First, add a dependency to admin-web-module in your pom.xml.

<dependencies>
    <dependency>
        <groupId>com.foreach.across.modules</groupId>
        <artifactId>admin-web-module</artifactId>
    </dependency>
</dependencies>

Add AdminWebModule to your application descriptor.

Adding AcrossWebModule to the application descriptor
@AcrossApplication(
		modules = {
				AdminWebModule.NAME (1)
		}
)
@Import(SecurityConfiguration.class) (2)
public class DemoApplication
{
	public static void main( String[] args ) {
		SpringApplication.run( DemoApplication.class, args );
	}
}
1 The AdminWebModule which provides login/logout functionality to a backend.
2 We import the configuration class which implements AcrossBootstrapConfigurer.
Example AcrossBootstrapConfigurer configuration class for extending a module
@Configuration
public class SecurityConfiguration implements AcrossBootstrapConfigurer {
    @Override
    public void configureContext(AcrossBootstrapConfig contextConfiguration) {
        contextConfiguration.extendModule(SpringSecurityModule.NAME, AuthenticationConfiguration.class); (1)
    }
}
1 The AuthenticationConfiguration class is injected into the SpringSecurityModule. This module centralizes everything related to security.
Example configuration class for in memory authentication
public class AuthenticationConfiguration
{
    @Autowired
    public void configureAuthentication( AuthenticationManagerBuilder auth ) throws Exception { (1)
        auth.inMemoryAuthentication()
                .withUser( "admin" ).password( "admin" )
                .authorities( "access administration" ).and()
                .withUser( "user" ).password( "user" )
                .authorities( Collections.emptyList() );
    }
}
1 When Spring Security configures the AuthenticationManagerBuilder, the configuration will be extended with our implementation.

Now run the application.

$ mvn spring-boot:run

When you navigate to http://localhost:8080/login in your browser you should be able to login with username admin and password admin.

Creating this structure is quite convoluted when only needing to inject a configuration into another module. An easier alternative is to use @ModuleConfiguration, explained in the next section.

Module extensions with @ModuleConfiguration

This specialised annotation acts like a @Configuration class, but executes inside the scope of a specific module.

The above example can be rewritten to using just one configuration class.

First, add a dependency to admin-web-module in your pom.xml.

<dependencies>
	<dependency>
		<groupId>com.foreach.across.modules</groupId>
		<artifactId>admin-web-module</artifactId>
	</dependency>
</dependencies>

Add AdminWebModule to your application descriptor.

Adding AcrossWebModule to the application descriptor
@AcrossApplication(
		modules = {
				AdminWebModule.NAME (1)
		}
)
public class DemoApplication
{
	public static void main( String[] args ) {
		SpringApplication.run( DemoApplication.class, args );
	}
}

Create an extensions package under your application package. The extensions package is automatically scanned by Across for @ModuleConfiguration classes.

Add the following configuration class to the extensions package.

Example configuration class for in memory authentication with @ModuleConfiguration
@ModuleConfiguration(SpringSecurityModule.NAME) (1)
public class SecurityConfiguration {
    @Autowired
    public void configureAuthentication( AuthenticationManagerBuilder auth ) throws Exception { (2)
        auth.inMemoryAuthentication()
                .withUser( "admin" ).password( "admin" )
                .authorities( "access administration" ).and()
                .withUser( "user" ).password( "user" )
                .authorities( Collections.emptyList() );
    }
}
1 This configuration class will be injected in the scope of the SpringSecurityModule. If only @ModuleConfiguration is specified, it is injected in ALL modules.
2 The configuration will be customized when the AuthenticationManagerBuilder from the SpringSecurityModule is created.
Only the extensions package is scanned for @ModuleConfiguration classes, if you place it anywhere else, it will be ignored.

Now run the application.

$ mvn spring-boot:run

When you navigate to http://localhost:8080/login in your browser you should be able to login with username admin and password admin.