Announcement Announcement Module
Collapse
No announcement yet.
How to store sample data Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    It's a workaround, but this seems to be fine:

    Code:
    package com.springsource.vote;
    
    import org.springframework.context.ApplicationListener;
    import org.springframework.context.event.ContextRefreshedEvent;
    import org.springframework.stereotype.Component;
    import org.springframework.transaction.annotation.Transactional;
    
    import com.springsource.vote.domain.Choice;
    
    @Component
    public class Loader implements ApplicationListener<ContextRefreshedEvent>{
    
            @Override
            @Transactional
            public void onApplicationEvent(ContextRefreshedEvent event) {
            	if (event.getApplicationContext().getParent() == null) {
            		System.out.println(">>>>>>>>  Parent started");
                    System.out.println("before: " + Choice.countChoices());
                    new Choice().persist();
                    System.out.println("after: " + Choice.countChoices());
            	} else {
            		System.out.println(">>>>>>>>  Child started; ignoring");
            	}
            }
    }

    Comment


    • #17
      CSV Load

      We use a CSV loader based on open CSV, but still haven't figured out how to deal with relationships properly...

      Comment


      • #18
        I wanted to do the same thing and was having the same problem. I think I have a solution although it seems like a hack. I noticed that the component scan was being done in the applicationContext.xml before the entityManager was created. I moved the component scan to the end of the file and that worked for me.

        <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
        <bean class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean" id="entityManagerFactory">
        <property name="dataSource" ref="dataSource"/>
        </bean>

        <context:component-scan base-package="thing">
        <context:exclude-filter expression=".*_Roo_.*" type="regex"/>
        <context:exclude-filter expression="org.springframework.stereotype.Control ler" type="annotation"/>
        </context:component-scan>
        </beans>

        Comment


        • #19
          It's only a hack if you don't comment it in your XML file!

          Comment


          • #20
            Originally posted by Ben Alex View Post
            It's a workaround, but this seems to be fine:

            Code:
            ...
            @Component
            public class Loader implements ApplicationListener<ContextRefreshedEvent>{
            ...

            Basically, this works. But the problem is that on some application starts, it gets fired on a child applicationcontext, thus it is ignored. So I need to restart again until it is fired after root ApplicationContext setup.

            Any ideas?

            Comment


            • #21
              Hi,

              Nevermind... this question can be safely ignored. After rebuilding my project from the scripts I eliminated this problem. I think it originated in the Roo 1.2 upgrade of my project
              I have tried Ben's suggestion (using Roo 1.2.0 M1) like this,

              Code:
              	@Override
              	public void onApplicationEvent(ContextRefreshedEvent event) {
              		if (event instanceof ContextRefreshedEvent ) {
              			if (!User.findAllUsers().isEmpty()) {
                                      ....
              but am getting an error complaining about the EntityManager not being injected. Has anyone else seen this?

              Code:
              011-10-16 13:11:23,032 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'insertTestData'
              2011-10-16 13:12:02,147 [main] ERROR org.springframework.web.context.ContextLoader - Context initialization failed
              java.lang.IllegalStateException: Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)
              	at com.kn.domain.User_Roo_Entity.ajc$interMethod$com_kn_domain_User_Roo_Entity$com_kn_domain_User$entityManager(User_Roo_Entity.aj:57)
              	at com.kn.domain.User.entityManager(User.java:1)
              	at com.kn.domain.User_Roo_Entity.ajc$interMethodDispatch1$com_kn_domain_User_Roo_Entity$com_kn_domain_User$entityManager(User_Roo_Entity.aj)
              	at com.kn.domain.User_Roo_Entity.ajc$interMethod$com_kn_domain_User_Roo_Entity$com_kn_domain_User$findAllUsers(User_Roo_Entity.aj:66)
              	at com.kn.domain.User.findAllUsers(User.java:1)
              	at com.kn.domain.User_Roo_Entity.ajc$interMethodDispatch1$com_kn_domain_User_Roo_Entity$com_kn_domain_User$findAllUsers(User_Roo_Entity.aj)
              	at com.kn.config.InsertTestData.initMe(InsertTestData.java:55)
              	at com.kn.config.InsertTestData.onApplicationEvent(InsertTestData.java:46)
              	at com.kn.config.InsertTestData.onApplicationEvent(InsertTestData.java:1)
              	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:97)
              	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:303)
              	at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:911)
              	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:428)
              	at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:282)
              	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:204)
              	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
              	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4135)
              	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4630)
              	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
              	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
              	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:568)
              	at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:637)
              	at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:563)
              	at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:498)
              	at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1282)
              Last edited by md10024; Oct 16th, 2011, 11:57 PM.

              Comment

              Working...
              X