PDA

View Full Version : Spring can't find DBCP in Ant


linuxerwang
Feb 11th, 2006, 12:10 PM
Hello, all

I wrote the following codes, but it failed to work:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>org.postgresql.Driver</value>
</property>
......


<target name="initdb" depends="compile, mapping">
<taskdef name="SystemInit" classname="tests.SystemInit" classpathref="classpath">
</taskdef>
<SystemInit configPath="${web}/WEB-INF/config" dataPath="${base}/initdata"/>
</target>


private void setUp() throws Exception {
System.out.println("---> "+Class.forName("org.apache.commons.dbcp.BasicDataSource").newInstance());
appContext = new FileSystemXmlApplicationContext(
new String[] {
configPath + "/dataAccessContext-local.xml",
configPath + "/applicationContext.xml"
}
);
......
}


And when run it through ant (from the command line), it gives exception:

[SystemInit] org.springframework.beans.factory.BeanDefinitionSt oreException: Error registering bean with name 'dataSource' defined in file [/home/test/project/web/WEB-INF/config/dataAccessContext-local.xml]: Bean class [org.apache.commons.dbcp.BasicDataSource] not found; nested exception is java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource

It's surprising that Class.forName can find the same class but Spring can't find it. Seems some contextloader problem, but I don't know how to solve it.

I am using spring 1.2.6.

Help me please, thank you very much.

kevinstembridge
Feb 11th, 2006, 12:45 PM
Have you tried putting the DBCP classes in ANT_HOME/lib ?

I've had issues before with Ant taskdef not correctly finding all classes on the classpath that is passed to it.

linuxerwang
Feb 11th, 2006, 01:12 PM
Thank you for your reply.

I found an old post:
http://forum.springframework.org/showthread.php?t=14635&highlight=FileSystemXmlApplicationContext

and it refers this article:

http://www.javaworld.com/javaworld/jw-02-2005/jw-0214-antspring.html

So it's the problem of the way ant using classloader. Use the following codes before loading the spring framework to solve it:

AntClassLoader2 antClassLoader = null;
Object obj = this.getClass().getClassLoader();
if (obj instanceof AntClassLoader2) {
antClassLoader = (AntClassLoader2) obj;
antClassLoader.setThreadContextLoader();
}