问题
Is there way to configure the objects from inside the code rather than configuring it in xml or app.config file.
回答1:
On the spring.net homepage, you'll find an announcement for the CodeConfig project. CodeConfig allows you to create spring configuration from code, like for instance:
[Configuration]
public class MovieFinderConfiguration
{
[Definition]
public virtual MovieLister MyMovieLister()
{
MovieLister movieLister = new MovieLister();
movieLister.MovieFinder = FileBasedMovieFinder();
return movieLister;
}
[Definition]
public virtual IMovieFinder FileBasedMovieFinder()
{
return new ColonDelimitedMovieFinder(new FileInfo("movies.txt"));
}
}
You can use this together with any xml configuration you might already have.
来源:https://stackoverflow.com/questions/6678409/spring-net-without-configuring-it-in-app-config