|
From: bryan <nih...@gm...> - 2004-08-31 21:54:56
|
First off appologies for cross-posting :-(=20
/*
* Created on 30 ao=FBt 2004 bryan hunt licence LGPL
*/
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.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.AbstractXmlApplicationContext;
/**
* @author Administrateur
*
*/
public class ConditionalClassPathXMLApplicationContext=20
extends AbstractXmlApplicationContext implements ApplicationContextAware{
=20
public static void main(String[] args) {
=20
Map mappings =3D new HashMap();
mappings.put("jestate.unit.testing=3Denabled","/test/ie/jestate/spr=
ing/unit-testing.xml");
=20
Map variables =3D new HashMap();
variables.put("jestate.unit.testing","enabled");
=20
=20
ConditionalClassPathXMLApplicationContext test =3D new
ConditionalClassPathXMLApplicationContext(mappings,variables);
ISystemUserService systemUserService =3D (ISystemUserService)
test.getBean("SystemUserService");
System.out.println(systemUserService.toString());
=20
}
private String[] configLocations ;
/**
* Matches mappings to variables and then loads appropriate
application contexts
* @param mappings of the form
("jestate.testing=3Dtrue","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 =3D mappings.keySet();
Set variableKeys =3D variables.keySet();
Set configLocationSet =3D new HashSet();
Iterator variableKeysIterator =3D variableKeys.iterator();
=20
while (variableKeysIterator.hasNext()) {
=20
=20
String variableKey =3D (String ) variableKeysIterator.next();
String mapping =3D (String) variables.get(variableKey);
String mappingKey =3D variableKey + "=3D" + mapping;
logger.debug("mappingKey=3D'" + mappingKey + "'");
logger.debug("mappings.get(mappingKey)=3D'" +
mappings.get(mappingKey) + "'");
if(mappings.get(mappingKey) !=3D null) {
configLocationSet.add(mappings.get(mappingKey));
}
}
=20
configLocations =3D new String[configLocationSet.size()];
=20
Iterator iterator =3D configLocationSet.iterator();
=20
=20
configLocations =3D (String[]) configLocationSet.toArray(configLoca=
tions);
=20
for (int i =3D 0; i < 3; i++) {
logger.info("******* Config start *********");
}
=20
=20
for (int ii =3D 0; ii < configLocations.length; ii++) {
logger.info(configLocations[ii]);
} =20
=20
for (int i =3D 0; i < 10; i++) {
logger.info("******* Config end *********");
}
refresh();
}
/* (non-Javadoc)
* @see org.springframework.context.support.AbstractXmlApplicationConte=
xt#getConfigLocations()
*/
protected String[] getConfigLocations() {
return configLocations;
}
/* (non-Javadoc)
* @see org.springframework.context.ApplicationContextAware#setApplicat=
ionContext(org.springframework.context.ApplicationContext)
*/
public void setApplicationContext(ApplicationContext
applicationContext) throws BeansException {
=20
this.setParent(applicationContext);
=20
refresh();
=20
}
=20
}
When I test this class using it's main method it works just fine but
when I test it
in this fashion ...
return new ClassPathXmlApplicationContext("classpath:/ie/jestate/spring/app=
lication.xml");
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.PropertyServiceTest)org.sp=
ringframework.beans.factory.NoSuchBeanDefinitionException:
No bean named 'PropertyService' is defined:
org.springframework.beans.factory.support.DefaultListableBeanFactory
defining beans [contextConfigurer]; Root of BeanFactory hierarchy
at org.springframework.beans.factory.support.DefaultListableBeanFactory.get=
BeanDefinition(DefaultListableBeanFactory.java:226)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedB=
eanDefinition(AbstractBeanFactory.java:488)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(Ab=
stractBeanFactory.java:143)
at org.springframework.context.support.AbstractApplicationContext.getBean(A=
bstractApplicationContext.java:394)
at test.ie.jestate.spring.service.PropertyServiceTest.setUp(PropertyService=
Test.java:62)
at test.ie.jestate.spring.service.PropertyServiceTest.main(PropertyServiceT=
est.java:66)
I am guessing that I have to do something to make it aware of it's
parent but I am't
quite sure what. I'm quite new to spring and haven't found anything obvious=
in=20
the documentation.
Any help would be much appreciated and I hope that my code can be used in=
=20
the spring framework.
--b
|