Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Grails like BootStrap functionality [SPR-9889] #14522

Closed
spring-projects-issues opened this issue Oct 18, 2012 · 3 comments
Closed

Add Grails like BootStrap functionality [SPR-9889] #14522

spring-projects-issues opened this issue Oct 18, 2012 · 3 comments
Assignees
Labels
status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Oct 18, 2012

Marcel Overdijk opened SPR-9889 and commented

It would be very nice to have simple BootStrap functionality like Grails available:

class BootStrap {

	def init = { servletContext ->
	}

	def destroy = {
	}
}

It would be nice to configure such classes in AppConfig or maybe just an annotation would be enough?


Issue Links:

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Oct 24, 2012

Chris Beams commented

Hi Marcel,

Could you provide a bit more context here? i.e. what would you like to configure in these classes? How would you see them working with regard to typical Spring (web) application needs? Feel free to brainstorm, and see #14521 in the meantime.

@spring-projects-issues
Copy link
Collaborator Author

Marcin Kuthan commented

Hi Chris, Marcel

Some sort of support for application bootstrap would be really useful. I implemented bootstrap feature with Spring application listener and events:

@Component
public class ApplicationBootstrap implements ApplicationListener<ContextRefreshedEvent> {

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		ApplicationContext applicationContext = event.getApplicationContext();

		if (isRootApplicationContext(applicationContext)) {
			event.getApplicationContext().publishEvent(new BootstrapEvent(applicationContext));
		}

	}

	private boolean isRootApplicationContext(ApplicationContext applicationContext) {
		return applicationContext.getParent() == null;
	}

}

Listeners are ordered so there is built-in feature to decouple listeners if there are bootstrap process dependencies.

@Component
@Profile("local")
public class ExampleBootstrapListener implements ApplicationListener<BootstrapEvent>, Ordered {

	@Autowired
	private ExampleRepository exampleRepository;

	@Override
	@Transactional
	public void onApplicationEvent(BootstrapEvent event) {
		if (!examplesExist()) {
                    ...
		}
	}

	@Override
	public int getOrder() {
		return BootstrapOrder.EXAMPLE;
	}

	private boolean examplesExist() {
		return exampleRepository.count() > 0;
	}
}

You can find example project at: https://github.com/mkuthan/example-spring/. Do we need abstraction layer on top spring events/listeners for application bootstrap? I'm not sure.

@spring-projects-issues
Copy link
Collaborator Author

Sébastien Deleuze commented

I guess Spring Boot is a better answer for this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants