View Full Version : Referencing beans in different config files
halcyon
Aug 11th, 2004, 07:28 PM
Woo first post in this category, go me :D
I'm trying to setup a standalone that uses database connections. What I
wanted to do was seperate my dao's and service beans into their own context file, and put the datasource bean in a different config file that is
modifyable by the user. An example would be:
/datasource.xml contains the datasource bean
/myContext.xml contains my dao's which take as a parameter the datasource
bean
then I use a classpathcontext loader like so:
static {
String[] contextLocations = new String[]
{"/dataSource.xml","/resources/myContext.xml"};
applicationContext = new
ClassPathXmlApplicationContext(contextLocations);
}
However it appears the DTD or something is holding me back from doing this..
Caused by: org.springframework.beans.factory.BeanDefinitionSt oreException:
Line 24 in XML document from class path resource [resources/myContext.xml]
is invalid; nested exception is org.xml.sax.SAXParseException: An element
with the identifier "dataSource" must appear in the document.
at
org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefini
tions(XmlBeanDefinitionReader.java:133)
Is the only solution to somehow create a 'dumby' bean with the name
dataSource within the dao context just to satisfy the dtd? that seams stupid to me.. and if so whats the easiest way to create a dumby bean just to satisfy
the dtd?
Thanks,
David
dima767
Aug 11th, 2004, 08:19 PM
David,
can you post contents of datasource.xml and myContext.xml?
halcyon
Aug 11th, 2004, 08:24 PM
Sure thing. dataSource.xml:
<?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="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>org.gjt.mm.mysql.Driver</value>
</property>
<property name="maxActive">
<value>100</value>
</property>
<property name="maxIdle">
<value>30</value>
</property>
<property name="maxWait">
<value>100</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/blah?autoReconnect=true</value>
</property>
<property name="username">
<value>protectThe</value>
</property>
<property name="password">
<value>innocent</value>
</property>
</bean>
</beans>
and myContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- DAO's -->
<bean id="mileageDao" class="com.something.MileageDaoImpl">
<property name="dataSource"><ref local="dataSource"/></property>
</bean>
....
<!-- Services -->
<bean id="nadaService" class="com.something.ServiceImpl">
<property name="mileageDao"><ref local="mileageDao"/></property>
</bean>
</beans>
So you can see the xml parser is freaking out because theres no bean named dataSource within the myContext.xml file... even though it is being loaded into the spring context from the other file...
-David
dima767
Aug 11th, 2004, 08:29 PM
David,
in myContext.xml use "ref bean" instead of "ref local" i.e.
<property name="dataSource"><ref bean="dataSource"/></property>
With "ref local" you can only reference elements defined in the same file whereas "ref bean" alows referencing "global" elements defined in different files.
pmorelli
Aug 11th, 2004, 08:30 PM
Try <ref bean="datasource" instead of <ref local="datasource
http://www.springframework.org/docs/reference/beans.html#beans-factory-properties-detailed
Specifying the target bean by using the local attribute leverages the ability of the XML parser to validate XML id references within the same file. The value of the local attribute must be the same as the id attribute of the target bean. The XML parser will issue an error if no matching element is found in the same file. As such, using the local variant is the best choice (in order to know about errors are early as possible) if the target bean is in the same XML file.
--pete
pmorelli
Aug 11th, 2004, 08:31 PM
Doh, beat me to the punch. :D
halcyon
Aug 11th, 2004, 09:04 PM
Aha thanks everyone =)
-David
vikas
Oct 23rd, 2007, 07:26 AM
I tried out to use this in my xml file to reference to another xml file at same location but i m getting error
<bean id="loginDataService" class="jkt.hms.login.dataservice.LoginDataServiceImpl">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
Error is------>Can't resolve reference to bean 'sessionFactory' while setting property 'sessionFactory'; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'sessionFactory' is defined: org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans [loggingBean,tracingBeforeAdvisor,tracingAfterAdvis or,loggingThrowsAdvisor,theTracingBeforeAdvice,the TracingAfterAdvice,theLoggingThrowsAdvice,loginDat aService]; root of BeanFactory hierarchy
mbogoevici
Oct 23rd, 2007, 09:29 PM
I tried out to use this in my xml file to reference to another xml file at same location but i m getting error
<bean id="loginDataService" class="jkt.hms.login.dataservice.LoginDataServiceImpl">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
Error is------>Can't resolve reference to bean 'sessionFactory' while setting property 'sessionFactory'; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'sessionFactory' is defined: org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans [loggingBean,tracingBeforeAdvisor,tracingAfterAdvis or,loggingThrowsAdvisor,theTracingBeforeAdvice,the TracingAfterAdvice,theLoggingThrowsAdvice,loginDat aService]; root of BeanFactory hierarchy
vikas, you should either <import> the other xml file or make sure that the ApplicationContext you are using uses both files. For now, your app configuration consists of a single file.
vikas
Oct 24th, 2007, 03:46 AM
hi mbogoevici,
thnks for your help, i tried out using import(and it helped me also) but the problem is i want to reference my another xml file which is in another package and my import tag is in xml file which is in WEB-INF FOLDER.
mbogoevici
Oct 24th, 2007, 09:06 PM
You can essentially add them to your WebApplicationContext definition in your web.xml file, using something like:
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml,
classpath:/full/path/to/context.xml
</param-value>
</context-param>
...
</web-app>
You can find more details here:
http://static.springframework.org/spring/docs/2.0.x/reference/resources.html#resources-classpath-wildcards
vikas
Oct 29th, 2007, 02:57 AM
hi mbogoevici ,
Thanks for the reply i really helped me a lot.
Regards
Vikas
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.