PDA

View Full Version : Embedded Configuration Files


michael1d
02-14-2005, 12:57 AM
Hi,

I am a Spring newbie and have had a few issues getting an embedded configuration resource working.

Firstly, the MovieFinder example shows the <object> definition as using a "class" attribute not "type". After much frustration, I finally added the DTD to the document and found that "type" was indeed the correct attribute. So have I missed something or should the doco be updated with the correct name - this is the starting point for any newbie...

Secondly, in the documentation for XmlApplicationContext, it says the format should be "assembly://assemblyName/namespace.resourceName", which I tried and it didn't work - what I have found by looking at the code is that the format should be "assembly://assemblyName/namespace/resourceName" and now I can instantiate an object successfully.

Another question. I am now trying to split my object definitions into multiple XML files embedded in an assembly. When I try to get the application context I get the following exception "{"Could not instantiate type [Void .ctor(System.String[])]; is it an interface or an abstract class? Does it have a no-arg constructor?"}". My config files looks like:

<spring>
<context>
<resource uri="assembly://assemblyname/namespace/SpringA.xml"/>
<resource uri="assembly://assemblyname/namespace/SpringB.xml"/>
</context>
</spring>

I find that if I remove the second resource entry then everything is OK. Is only one resource entry of type "assembly" supported? I notice that the doco gives an example of "assembly" and "file" together...

Hopefully my second day with Spring will be better :)

Cheers,

Mike

Mark Pollack
02-14-2005, 02:23 PM
Hi Mike,

Well I do hope your next day goes better. Where did you see reference to 'class' instead of 'type'? The quickstart docs in the reference documentation show 'type' as well as the example code. I'm sorry about the SDK docs for XmlApplicationContext showing the wrong namespace naming scheme - I've made that change now. As a rule of thumb, right now the reference docs are more accurate (we will be refreshing those on the web in between releases) and we will be going over the SDK docs with fine tooth comb for the 0.6.0 final release.

I saw the issue you are talking about but don't remember off the top of my head now what I did to resolve it. (I believe one of our classes in missing a string[] ctor..) Let me get back to you a little later on that.

Cheers,
Mark

Anonymous
02-15-2005, 12:48 AM
Hi Mark,

Today has started much better thanks!

In the Movie Finder sample, section "First Object Definition" and following sections use a class attribute.

Otherwise, I did manage to get my proof of concept working with Spring :D This includes the multiple embedded resource files issue described above. Basically there was an error in one of my object's constructors that Spring was creating - it would be REALLY nice if Spring could raise the actual error that caused the application load to fail - I could imagine in an app with a very large configuration file, that this could be very difficult to debug...

I am now very much looking forward to the NHibernate libraries and specifically declarative transactions - any more news on when this is to be delivered? Is there anything I can use from CVS yet?

Cheers and thanks,

Mike

Mark Pollack
02-15-2005, 03:03 PM
Hi Mike,

Glad to hear it went well! I remembered today on my way to work where I saw that error before. You are certainly correct in your diagnosis. We use reflection to create the application context instance and the top level exception is that quasi-meaningless reflection like error. If you print out Exception.GetBaseException() then you will see a meaningful message. I changed the code in MovieFinder to that type of usage. In general the custom and nested exception usage in Spring.NET makes just printing the top level exception not as useful. We'll look into how we can improve this.

There have a been a few people who contacted me regarding doing NHibernate support but as of yet there is nothing in CVS and no one from the dev team has taken a look at what NHibernate support would entail. Griffin has commited a fair amount of transaction support code including some inteterceptor code in Spring.Data and another new member has ported the "jdbc.object" package from spring.java but not yet commited. I need to check myself on the status of the core SqlClientTemplate, but take a look anyway to see where we are going. I'll have Griffin post a reply here.

Timeline wise the data access functionality would come after 0.6.0 final (~1 month), 0.7 (AOP/Web) (~2 months), so we are probably looking at ~3-4 months from now.

Thanks for pointing out the doc error.

Cheers,
Mark

JasonGerard
02-15-2005, 06:36 PM
Mike and Mark,

another new member has ported the "jdbc.object" package from spring.java but not yet commited. I need to check myself on the status of the core SqlClientTemplate, but take a look anyway to see where we are going. I'll have Griffin post a reply here.

I have refactored the Template code. There is now a central AdoTemplate class that contains the core functionality. SqlClientTemplate is still there but it now inherits from AdoTemplate instead of IAdoOperations. It's pretty small and just provides functionality for creating SqlConnections and SqlDataAdapters to be used by the AdoTemplate class. I sent the code to Griffin to review and check in since I don't have CVS access yet.

-Jason

Anonymous
03-04-2005, 09:01 AM
I have created web based application using Spring.NET rc2. When I want to put into external file (using resource file), I am unable to load my configuration file using relative path. But if I run it on windows/console application it works well. I try to do debugging the Spring Core IO, then I found that FileSystemResource when create System.IO.FileInfo object, its base directory is point to {WINDOWS}\system32 (in web application mode). So I do some changes on the constructor of FileSystemResource.cs, I want to sent it to you to be further review or published.

Thanks,
Adhari C Mahendra

JasonGerard
03-04-2005, 04:24 PM
Adhari,

I do not work on this part of Spring and am really not familiar with it's underlying implementation. If you think you have found a bug, look in the Spring.NET JIRA to see if it has already been reported. If not, you should be able to report it as a bug. You should include any fix you have in the bug report.

Thanks,
Jason

Mark Pollack
03-05-2005, 05:49 PM
Hi Adhari,

This does sound like a bug, I'll check up on it and get back to you. Thanks for posting the issue. If you feel like it you can email or use the form to send a private message to either myself or Aleks Seovic with the changes you made.

Cheers,
Mark
b

Mark Pollack
03-08-2005, 02:11 AM
Hi Adhari,

Can you post or send to me via email (click on memberlist and then send private message) more details of your web.config file and the code you are using.

Thanks,
Mark

Aleks Seovic
03-08-2005, 03:35 AM
Hi Adhari,

I believe you are using incorrect application context implementation. If you didn't specify context type within your config file it will default to XmlApplicationContext, which uses FileResources by default. Because base directory for IIS is {windows}\system32, it tries to load resources relative to that directory instead of your webapp directory.

You need to use WebApplicationContext which will default resource type to WebResource and use Server.MapPath to resolve specified resources. Take a look at
http://opensource.atlassian.com/confluence/spring/display/NET/Spring.Web for more information on Spring.Web features and for setup instructions.

Regards,

Aleks

Aleks Seovic
03-08-2005, 03:38 AM
BTW, I'm not sure you'll be able to do what you want using RC2. As far as I know Spring.Web is not part of RC2, so that might be part of the problem.

Your best bet is to get latest code from the CVS and build it yourself using "nant test" target, which will build all Spring assemblies.

Mark, should we start nightly (or at least weekly) builds of the whole codebase so people can play with the stuff that hasn't been released yet?

-- Aleks

Mark Pollack
03-08-2005, 07:11 AM
Hola..

I made some simple changes to the build script to create a 'nightly' build with all that is in CVS and the docs. The link to 'iterim builds' is on our download (http://www.springframework.net/download.html) page. Adhari, try using this download and the instructions on the wiki listed previously.

- Mark

Ted Husted
03-23-2005, 01:19 PM
If you need to read the Spring.NET configuratoin from a console program (e.g. NUnit) as well as a web application, and aren't ready to jump into Spring.Web, another alternative is to use a singleton to load the configuraiton.

Here's one we've been using:


public class Objects
{

static string[] files = {
"/Resources/Command/AppConfig.xml",
"/Resources/Command/Catalog.xml"
};

private Objects ()
{
// private constructor prevents instantiation.
}

private static string _rootDirectory =
AppDomain.CurrentDomain.BaseDirectory.Replace (@"\bin", "").Replace (@"\Debug", "").Replace (@"\Release", "");

private static volatile IApplicationContext _Factory = null;

public static IApplicationContext Factory ()
{
if (_Factory == null)
{
lock (typeof (XmlObjectFactory))
{
int i = 0;
string[] foo = new string[files.Length];
foreach (string f in files)
{
foo[i] = "file://" + _rootDirectory + f; i++;
}
if (_Factory == null) // double-check
_Factory = new XmlApplicationContext (foo);
}
}
return _Factory;
}

}
}


(With RC2, we found that trying this with ObjectFactory exposed an ASP.NET permissions exception, but ApplicationContext works just fine.)

Taking this a step farther, you can minimize Spring.Net references within your application code by using your own Controller object, that you can instantiate via a call to the boostrap singleton:


public class Controller : IController, IApplicationContextAware
{

private IApplicationContext _Factory = null;

public IApplicationContext ApplicationContext
{
get { return _Factory; }
set { _Factory = value; }
}

public Controller () {}

public Controller (IApplicationContext value)
{
ApplicationContext = value;
}

public IApplicationContext Factory ()
{
return _Factory;
}

public object GetObject (string name)
{
if (null == name)
throw new ArgumentNullException("name","IController.GetObject")
return Factory ().GetObject (name);
}


Of course, the Controller strategy also works with Spring.Net dependency injection, since the IController implementation can be Spring aware. In that case, there are no Spring references in the ASP.NET code, since the IController implementation simply appears already instantiated. :)

HTH, Ted.