Announcement Announcement Module
Collapse
No announcement yet.
extending AbstractXmlApplicationContext problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • extending AbstractXmlApplicationContext problem

    Hi there,
    I'm implementing a class called
    ConditionalClassPathXMLApplicationContext it extends
    AbstractXmlApplicationContext but allows xml based
    configuration of what contexts are loaded.

    It is displaying strange behaviour however.

    The context is initialized allright, I know this because some of
    my beans have initialization methods and they log that they
    are initializing, in addition I can see that my hibernate stuff
    has initialized ok.

    However when I try to access a bean I get a
    java.lang.IllegalStateException: Application context not initialized

    ------------------------------------------------------------------------------------
    /*
    * Created on 30 août 2004
    */
    package ie.jestate.temp.spring;

    import ie.jestate.spring.service.ISystemUserService;
    import ie.jestate.util.ApplicationContextFactory;

    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;

    import org.springframework.beans.BeansException;
    import org.springframework.context.support.AbstractXmlApp licationContext;

    /**
    * @author Administrateur
    *
    */
    public class ConditionalClassPathXMLApplicationContext
    extends AbstractXmlApplicationContext {


    public static void main(String[] args) {

    Map mappings = new HashMap();
    mappings.put("jestate.unit.testing=enabled","class path:test/ie/jestate/spring/unit-testing.xml");

    Map variables = new HashMap();
    variables.put("jestate.unit.testing","enabled");


    ConditionalClassPathXMLApplicationContext test = new ConditionalClassPathXMLApplicationContext(mappings ,variables);
    ISystemUserService systemUserService = (ISystemUserService) ApplicationContextFactory.getApplicationContext(). getBean("SystemUserService");

    }

    private String[] configLocations ;
    /**
    * Matches mappings to variables and then loads appropriate application contexts
    * @param mappings of the form ("jestate.testing=true","classpath:test/ie/jestate/spring/application.xml")
    * @param variables or the form("jestate.testing","true");
    * @throws BeansException
    */

    public ConditionalClassPathXMLApplicationContext(Map mappings, Map variables)
    throws BeansException {
    super();
    Set mappingKeys = mappings.keySet();
    Set variableKeys = variables.keySet();
    Set configLocationSet = new HashSet();
    Iterator variableKeysIterator = variableKeys.iterator();

    while (variableKeysIterator.hasNext()) {


    String variableKey = (String ) variableKeysIterator.next();
    String mapping = (String) variables.get(variableKey);
    String mappingKey = variableKey + "=" + mapping;
    logger.debug("mappingKey='" + mappingKey + "'");
    logger.debug("mappings.get(mappingKey)='" + mappings.get(mappingKey) + "'");
    if(mappings.get(mappingKey) != null) {
    configLocationSet.add(mappings.get(mappingKey));
    }
    }

    configLocations = new String[configLocationSet.size()];

    Iterator iterator = configLocationSet.iterator();


    configLocations = (String[]) configLocationSet.toArray(configLocations);

    for (int i = 0; i < 3; i++) {
    logger.info("******* Config start *********");
    }


    for (int ii = 0; ii < configLocations.length; ii++) {
    logger.info(configLocations[ii]);
    }

    for (int i = 0; i < 10; i++) {
    logger.info("******* Config end *********");
    }

    refresh();

    }

    /* (non-Javadoc)
    * @see org.springframework.context.support.AbstractXmlApp licationContext#getConfigLocations()
    */
    protected String[] getConfigLocations() {
    return configLocations;
    }
    }

    ------------------------------------------------------------------------------------

    [@APPNAME@] INFO [main] ConditionalClassPathXMLApplicationContext.<init>(8 2) | classpath:test/ie/jestate/spring/unit-testing.xml
    [@APPNAME@] INFO [main] ConditionalClassPathXMLApplicationContext.<init>(8 6) | ******* Config end *********
    [@APPNAME@] INFO [main] ConditionalClassPathXMLApplicationContext.<init>(8 6) | ******* Config end *********
    [@APPNAME@] INFO [main] ConditionalClassPathXMLApplicationContext.<init>(8 6) | ******* Config end *********
    [@APPNAME@] INFO [main] ConditionalClassPathXMLApplicationContext.<init>(8 6) | ******* Config end *********
    [@APPNAME@] INFO [main] ConditionalClassPathXMLApplicationContext.<init>(8 6) | ******* Config end *********
    [@APPNAME@] INFO [main] ConditionalClassPathXMLApplicationContext.<init>(8 6) | ******* Config end *********
    [@APPNAME@] INFO [main] ConditionalClassPathXMLApplicationContext.<init>(8 6) | ******* Config end *********
    [@APPNAME@] INFO [main] ConditionalClassPathXMLApplicationContext.<init>(8 6) | ******* Config end *********
    [@APPNAME@] INFO [main] ConditionalClassPathXMLApplicationContext.<init>(8 6) | ******* Config end *********
    [@APPNAME@] INFO [main] ConditionalClassPathXMLApplicationContext.<init>(8 6) | ******* Config end *********
    [@APPNAME@] INFO [main] AbstractXmlApplicationContext.refreshBeanFactory(6 5) | Bean factory for application context 'ie.jestate.temp.spring.ConditionalClassPathXMLApp licationContext;hashCode=20754125': org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans [messageSource,placeholderConfig,AuthenticationHold er,dataSource,mySessionFactory,myTransactionManage r,myHibernateInterceptor,PropertyService,propertyS erviceTarget,PropertyDAO,SystemUserService,SystemU serServiceTarget,SystemUserDAO,SystemUserDAOTarget ,GeneralService,GeneralServiceTarget,GeneralDAO,Ge neralDAOTarget,CustomerService,CustomerServiceTarg et,CustomerDAO,CustomerDAOTarget]; Root of BeanFactory hierarchy
    [@APPNAME@] INFO [main] AbstractApplicationContext.refresh(246) | 22 beans defined in ApplicationContext [ie.jestate.temp.spring.ConditionalClassPathXMLAppl icationContext;hashCode=20754125]
    [@APPNAME@] INFO [main] AbstractApplicationContext.refreshListeners(355) | Refreshing listeners
    [@APPNAME@] DEBUG [main] AbstractApplicationContext.refreshListeners(357) | Found 0 listeners in bean factory
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(138) | Unsuccessful: alter table addresses add index FK34207BA2AF14425A (FK_ADDRESS), add constraint FK34207BA2AF14425A foreign key (FK_ADDRESS) references SYSTEM_USER_DETAILS (ID)
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(139) | Invalid argument value message from server: "Duplicate key name 'FK34207BA2AF14425A'"
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(138) | Unsuccessful: alter table Property add index FKC8A841F557919495 (agent_id), add constraint FKC8A841F557919495 foreign key (agent_id) references SYSTEM_USER (id)
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(139) | Invalid argument value message from server: "Duplicate key name 'FKC8A841F557919495'"
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(138) | Unsuccessful: alter table Property add index FKC8A841F5A8907F51 (airConditioning), add constraint FKC8A841F5A8907F51 foreign key (airConditioning) references PROPERTY_ATTRIBUTES (id)
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(139) | Invalid argument value message from server: "Duplicate key name 'FKC8A841F5A8907F51'"
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(138) | Unsuccessful: alter table SYSTEM_USER add index FK2696BBDB86DA5744 (FK_SYSTEM_USER_DETAILS), add constraint FK2696BBDB86DA5744 foreign key (FK_SYSTEM_USER_DETAILS) references SYSTEM_USER_DETAILS (ID)
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(139) | Invalid argument value message from server: "Duplicate key name 'FK2696BBDB86DA5744'"
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(138) | Unsuccessful: alter table ATTRIBUTE_DESCRIPTIONS add index FKE1A57CDA7B66B0D0 (parent_id), add constraint FKE1A57CDA7B66B0D0 foreign key (parent_id) references PROPERTY_ATTRIBUTES (id)
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(139) | Invalid argument value message from server: "Duplicate key name 'FKE1A57CDA7B66B0D0'"
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(138) | Unsuccessful: alter table PROPERTY_ATTRIBUTES add index FK94DE96017BC07FCC (FK_ONE), add constraint FK94DE96017BC07FCC foreign key (FK_ONE) references Property (id)
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(139) | Invalid argument value message from server: "Duplicate key name 'FK94DE96017BC07FCC'"
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(138) | Unsuccessful: alter table Correspondance add index FK6241AA3EC4AB08AA (parent), add constraint FK6241AA3EC4AB08AA foreign key (parent) references Correspondance (id)
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(139) | Invalid argument value message from server: "Duplicate key name 'FK6241AA3EC4AB08AA'"
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(138) | Unsuccessful: alter table SYSTEM_USER_DETAILS_LANGUGES_SPOKEN add index FKCA5952927B66B0D0 (parent_id), add constraint FKCA5952927B66B0D0 foreign key (parent_id) references SYSTEM_USER_DETAILS (ID)
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(139) | Invalid argument value message from server: "Duplicate key name 'FKCA5952927B66B0D0'"
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(138) | Unsuccessful: alter table DESCRIPTIONS add index FK1799E3F77B66B0D0 (parent_id), add constraint FK1799E3F77B66B0D0 foreign key (parent_id) references Property (id)
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(139) | Invalid argument value message from server: "Duplicate key name 'FK1799E3F77B66B0D0'"
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(138) | Unsuccessful: alter table SUMMARY_DESCRIPTIONS add index FKF4A984107B66B0D0 (parent_id), add constraint FKF4A984107B66B0D0 foreign key (parent_id) references Property (id)
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(139) | Invalid argument value message from server: "Duplicate key name 'FKF4A984107B66B0D0'"
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(138) | Unsuccessful: alter table area add index FK2DD08D13D6FB20 (urban_area_id), add constraint FK2DD08D13D6FB20 foreign key (urban_area_id) references area (id)
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(139) | Invalid argument value message from server: "Duplicate key name 'FK2DD08D13D6FB20'"
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(138) | Unsuccessful: alter table area add index FK2DD08DC35AEA64 (country_id), add constraint FK2DD08DC35AEA64 foreign key (country_id) references area (id)
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(139) | Invalid argument value message from server: "Duplicate key name 'FK2DD08DC35AEA64'"
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(138) | Unsuccessful: alter table area add index FK2DD08D46E7646 (region_id), add constraint FK2DD08D46E7646 foreign key (region_id) references area (id)
    [@APPNAME@] ERROR [main] SchemaUpdate.execute(139) | Invalid argument value message from server: "Duplicate key name 'FK2DD08D46E7646'"
    [@APPNAME@] DEBUG [main] SystemUserHibernateDAO.<init>(31) | SystemUserHibernateDAO:Instantiated
    [@APPNAME@] DEBUG [main] AbstractApplicationContext.publishEvent(186) | Publishing event in context [ie.jestate.temp.spring.ConditionalClassPathXMLAppl icationContext;hashCode=20754125]: org.springframework.context.event.ContextRefreshed Event[source=ie.jestate.temp.spring.ConditionalClassPath XMLApplicationContext: displayName=[ie.jestate.temp.spring.ConditionalClassPathXMLAppl icationContext;hashCode=20754125]; startup date=[Tue Aug 31 21:43:16 CEST 2004]; root of ApplicationContext hierarchy]
    java.lang.IllegalStateException: Application context not initialized
    at ie.jestate.util.ApplicationContextFactory.getAppli cationContext(ApplicationContextFactory.java:33)
    at ie.jestate.temp.spring.ConditionalClassPathXMLAppl icationContext.main(ConditionalClassPathXMLApplica tionContext.java:36)


    ----------------------------------------------------------------------------------
    This is not related but if I can get this working springframwork users will
    be able to use configuration like this.... I think this would make things
    a lot easier for some projects to maintain + modularise in conjunction
    with the PreferencesPlaceholderConfigurer

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    <bean id="contextConfigurer"
    class="ie.jestate.temp.spring.ConditionalClassPath XMLApplicationContext">
    <constructor-arg index="0">
    <map>
    <entry key="jestate.unit.testing=enabled">
    <value>/test/ie/jestate/spring/unit-testing.xml</value>
    </entry>
    </map>
    </constructor-arg>
    <constructor-arg index="1">
    <map>
    <entry key="jestate.unit.testing">
    <value>${jestate.unit.testing}</value>
    </entry>
    </map>
    </constructor-arg>
    </bean>
    </beans>

    <!-- Load some stuff from Preferences API -->
    <bean id="placeholderConfig"
    class="org.springframework.beans.factory.config.Pr eferencesPlaceholderConfigurer">
    <property name="userTreePath"><value>ie/jestate/prefs</value></property>
    </bean>

  • #2
    my mistake

    sorry main should have read as so
    snip=
    public static void main(String[] args) {

    Map mappings = new HashMap();
    mappings.put("jestate.unit.testing=enabled","/test/ie/jestate/spring/unit-testing.xml");

    Map variables = new HashMap();
    variables.put("jestate.unit.testing","enabled");


    ConditionalClassPathXMLApplicationContext test = new ConditionalClassPathXMLApplicationContext(mappings ,variables);
    BeanFactory beanFactory = (BeanFactory) test;

    ISystemUserService systemUserService = (ISystemUserService) test.getBean("SystemUserService");

    }
    =snip

    Everything is working now

    Comment


    • #3
      not quite yet

      but it does the following ....

      when loaded in this fashion ...
      return new ClassPathXmlApplicationContext("classpath:/ie/jestate/spring/application.xml");

      however it displays strange behaviour.

      The log files show that it is loading the beans as so ...

      [@APPNAME@] INFO [main] HibernateTransactionManager.afterPropertiesSet(231 ) | Using DataSource [org.apache.commons.dbcp.BasicDataSource@14275d4] from Hibernate SessionFactory for HibernateTransactionManager
      [@APPNAME@] INFO [main] AbstractBeanFactory.getBean(158) | Creating shared instance of singleton bean 'myHibernateInterceptor'

      NOTE THIS LINE !!!!!!!!!!!!!!!!

      [@APPNAME@] INFO [main] AbstractBeanFactory.getBean(158) | Creating shared instance of singleton bean 'PropertyService'


      [@APPNAME@] INFO [main] AbstractBeanFactory.getBean(158) | Creating shared instance of singleton bean 'PropertyDAO'

      But when I try to access them ie "PropertyService" ie get the following
      exception ....

      ) testAddProperty(test.ie.jestate.spring.service.Pro pertyServiceTest)org.springframework.beans.factory .NoSuchBeanDefinitionException: No bean named 'PropertyService' is defined: org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans [contextConfigurer]; Root of BeanFactory hierarchy
      at org.springframework.beans.factory.support.DefaultL istableBeanFactory.getBeanDefinition(DefaultListab leBeanFactory.java:226)
      at org.springframework.beans.factory.support.Abstract BeanFactory.getMergedBeanDefinition(AbstractBeanFa ctory.java:488)
      at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:143)
      at org.springframework.context.support.AbstractApplic ationContext.getBean(AbstractApplicationContext.ja va:394)
      at test.ie.jestate.spring.service.PropertyServiceTest .setUp(PropertyServiceTest.java:62)
      at test.ie.jestate.spring.service.PropertyServiceTest .main(PropertyServiceTest.java:66)


      I guess what I am doing wrong here is that my subclass of
      AbstractXmlApplicationContext must implement ApplicationContextAware
      and then my method is as so ...


      /* (non-Javadoc)
      * @see org.springframework.context.ApplicationContextAwar e#setApplicationContext(org.springframework.contex t.ApplicationContext)
      */
      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

      this.setParent(applicationContext);

      refresh();

      }

      But it has no effect .......

      I know I'm nearly there, has anyone got any suggestions ?

      Bryan

      Comment

      Working...
      X