views:

41

answers:

3

Hi all,

I'm developing a Java webapp using Spring, Spring Security, Tomcat and MySQL. Right now I'm still in a phase where I'm making fairly frequent changes to the database as well as recreating the database to purge test data. Ultimately, this won't be an issue, but is there a way to have a bean that is instantiated or code that is executed to bootstrap the webapp? I'd like to be able to take advantage of IoC functionality to use the same, for instance, PasswordEncoder that my application uses to populate the original user accounts. Additionally, I'd like to have this script create the database if it doesn't exist, etc. Is there a way to do this? Is there a better way than I'm suggesting? I'm not married to the algorithm I just described, but it's the best way I can think of to convey what I'm looking for.

Thanks in advance!

+1  A: 

A good way to create the database schema is liquibase. You can also insert data. Liquibase offers a Spring integration via bean: liquibase.spring.SpringLiquibase, so the database will be updated if needed.

Arne Burmeister
This is a really cool looking framework. I'll definitely look into this. It isn't quite what I'm looking for though: I need to be able to call the insert queries from Java to guarantee I use the same encoder.
Chris Thompson
+1  A: 

If you use Spring, you can register a ContextLoaderListener to automatically start the ApplicationContext. Your ApplicationContext can then initialize the Database, ideally via JPA or Hibernate , but also using plain jdbc

seanizer
This was exactly along the lines of what I was looking for, thanks!
Chris Thompson
A: 

you could implement a bean with the ApplicationListener interface.

perform you necessary action when you receive the ContextRefreshedEvent

the upside of this approach is that the framework will have loaded up the context and all of your beans for you, so you have access to them all for what ever bootstrapping you want to do.

the downside is that this event is triggered on initialization and on refresh

ContextRefreshedEvent Published when the ApplicationContext is initialized or refreshed. Initialized here means that all beans are loaded, singletons are pre-instantiated and the ApplicationContext is ready for use.

Aaron Saunders