0

I have a code which starts my h2 database web server. It should be executed before datacontext will be read by Spring.

I have read the following article: Article

can't find appropriate event.

Is there another way to achieve what I want?

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • 1
    A typical spring boot application has a main class, containing a mein() method. Why don't you put the code there? What does "I cannot find appropriate event" means? – JB Nizet Dec 20 '15 at 10:00
  • I don't use spring boot – gstackoverflow Dec 20 '15 at 10:04
  • 1
    A typical Spring MVC application is bootstrapped using a ContextLoaderListser (http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/context/ContextLoaderListener.html). Create a subclass, and do what you need to do at bootstrap in the `contextInitialized()` method, before calling `super.contextInitialized()`. It would help if you told us a little bit about what you're using, how you're starting your spring app, and why you need to start a web server (?) before starting the app. – JB Nizet Dec 20 '15 at 10:12
  • @JB Nizet it is continue of http://stackoverflow.com/questions/34238142/how-to-show-content-of-local-h2-databaseweb-console – gstackoverflow Dec 20 '15 at 11:01
  • @JB Nizet your advice is helpful. but can I do this depends on active spring profile? – gstackoverflow Dec 20 '15 at 11:10
  • I don't see how you could know the active spring profile before bootstrapping spring. – JB Nizet Dec 20 '15 at 11:51

1 Answers1

0

If you use hibernate, and your code are just some SQL statements, then you could use Hibernate hibernate.hbm2ddl.import_files"

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
       ...
            <prop key="hibernate.hbm2ddl.import_files">initial_data.sql</prop>
        </props>
    </property>
</bean>
Ralph
  • 118,862
  • 56
  • 287
  • 383