In my dependency injection configuration file - I don't know (and can't find an example) on how to set into a member some predefined environment variable file.
Can't tell how to do it in Spring.Net - your help will be appreciated.
Use an EnvironmentVariableSource, the current docs are somewhat lacking at the moment: http://springframework.net/docs/1.3.2/reference/html/objects.html#objects-variablesource. I've written some additional docs, they'll probably be included in the next 2.0 release of the build. In the meantime you can also check this example code on Github https://github.com/serra/spring-net-examples/tree/master/Spring.IoCQuickStart.VariableSources.
Take a look to the VariablePlaceholderConfigurer object. It should do the job:
http://www.springframework.net/doc-latest/reference/html/objects.html#objects-variablesource
<object type="Spring.Objects.Factory.Config.VariablePlaceholderConfigurer, Spring.Core">
<property name="VariableSources">
<list>
<object type="Spring.Objects.Factory.Config.EnvironmentVariableSource, Spring.Core"/>
</list>
</property>
</object>
<object type="MyObject">
<property name="MyProperty" value="${MyEnvironmentVariableName}"/>
</object>
Related
The project structure is:
Now I am trying to create a bean NHibernateSessionFactory as:
<object id="NHibernateSessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate32">
<property name="DbProvider" ref="DbProvider"/>
<property name="MappingResources">
<list>
<value>EMSApplication.Domain/EMSApplication.hbm.xml</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<entry key="dialect" value="NHibernate.Dialect.MsSql2008Dialect"/>
<entry key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
<entry key="proxyfactory.factory_class" value="NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate"/>
<entry key="show_sql" value="true"/>
<entry key="hbm2ddl.auto" value="update"/>
<entry key="cache.use_query_cache" value="true"/>
</dictionary>
</property>
<property name="ExposeTransactionAwareSessionFactory" value="true" />
</object>
But I am getting the error message:
Error creating context 'spring.root': file [C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\EMSApplication.Domain\EMSApplication.hbm.xml] cannot be resolved to local file path - resource does not use 'file:' protocol.
Now the project is on D drive, I am wondering why this is pointing C drive?
Also how can I add the specified hbm file in the mapping resource? This file is set as 'Embedded Resource'.
Is there any way to specify a directory here to scan for the multiple hbm files?
Any information will be very helpful to me.
You are not using a protocol identifier (e.g. file:// or assembly://) in your resource name, so Spring uses the default file protocol. It tries to resolve the hbm file from the location where the web app is running (the devserver path), not where it's files are stored, which you seem to expect.
To reference a file in your website use a ~ to identify the root of your web site, e.g. <value>file://~/EMSApplication.Domain/EMSApplication.hbm.xml</value>. But make sure the file is copied, which might not be the case, since you have set it to embedded resource (why?).
Consider the following improvements, they'll make your live more easy:
move the code in your folders dao, domain and NHibernate to a separate assembly (a class library project that is referenced from your web application) named Your.AssemblyName.
The session factory also supports specifying mapping assemblies to scan, so that you don't have to specify each file - see the docs on setting up a session factory using spring.net. Specify Your.AssemblyName in the mapping assembly list:
<object id="MySessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate">
<property name="DbProvider" ref="DbProvider"/>
<property name="MappingAssemblies">
<list>
<value>Your.AssemblyName</value>
</list>
</property>
<property name="HibernateProperties">
<!-- snip -->
</object>
And check out the NHibernate example in the spring docs - it's very applicable to the questions you are raising here.
We're building a project using NHibernate and Castle with the Validators project. I'm trying to upgrade it to the latest supported version between all of those. I've gotten the application working without errors, but I'm getting the exception below in a few of my unit tests. These are tests that don't actually touch the database in any way, but test functionality around the mapped entities.
NHibernate.Bytecode.ProxyFactoryFactoryNotConfiguredException:
The ProxyFactoryFactory was not configured.
Initialize 'proxyfactory.factory_class' property of the session-factory
configuration section with one of the available NHibernate.ByteCode providers.
Example:
<property name='proxyfactory.factory_class'>
NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
</property>
Example:
<property name='proxyfactory.factory_class'>
NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
</property>
[Continues down stack trace...]
Below is my config file:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="Linx2">
<property
name="connection.driver_class">NHibernate.Driver.NpgsqlDriver</property>
<property name="dialect">Linx2.Common.Framework.PostgreSQL83Dialect,
Linx2.Common.Framework</property>
<property name="connection.connection_string">[Hidden so I don't get fired.]</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">false</property>
<property name="use_outer_join">true</property>
<property name="command_timeout">60</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name="proxyfactory.factory_class">
NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
</property>
<property name="connection.release_mode">after_transaction</property>
<mapping assembly="NHibernate.Collection.Observable" />
</session-factory>
</hibernate-configuration>
I have the config mapping there, and it works in the application. I'm also including the NHibernate.ByteCode dll. However, in these tests it is ignored. I've tried manually starting the configuration in individual test and even stopped and confirmed mid-test that the configuration has the item. However, the exception is thrown in the code below on the IsInitialized call.
if (NHibernateUtil.IsInitialized(ChildAssociations))
{
ChildAssociations.ForEach(x => isValid = isValid && x.Element.IsValid(validatedObjects));
}
This worked previously with no problems in NHibernate build for 2.2. Any help would be greatly appreciated. I've been beating my head on it for the last 4 hours.
Apparently NHibernateUtil needs not only to have the configuration initialized, but needs the session factory to be built. I was able to get it to work by manually running the config and building the session factory in the tests. It wasn't a problem in the app because the session factory had been built before hand.
var cfg = new NHibernate.Cfg.Configuration().Configure();
var sessionFactory = cfg.BuildSessionFactory();
Is there way in Spring for .NET how to use translated message from an IMessageSource as constructor argument for other object in the xml application context file? Something like <spring:message> in Java. My example:
<!-- my message source -->
<object name="messageSource" type="MyMessageSource"></object>
<object type="MyLocalizedObject">
<!-- my object, where I need to pass tranlated message into the argument: -->
<constructor-arg name="localizedTitle" value=">{translated title.key}<"/>
</object>
Well, you could do it with an expression:
<object name="messageSource" type="MyMessageSource"></object>
<object type="q9257449_springmessage.MyClass, q9257449_springmessage">
<constructor-arg name="localizedTitle"
expression="#(messageSource).GetMessage('HelloMessage', 'mr.', 'Anderson')" />
</object>
There might be a better way to achieve this, but in the meantime this should get you going.
I'm using Spring.NET's IoC container and everything has been working just fine....until now. Somehow, in one of our previous releases, we introduced a circular dependency. Since we use setter based injection as opposed to constructor based injection, Spring.NET just kept humming along fine, but the behavior of our app changed.
Now I have a solution with a hundred or so components, and somewhere in that pile of components exists a circular dependency, which I now need to find.
Are there any tools that can take my Spring.NET config files and give me a graphical picture of my components and their dependencies?
AFAIK there isn't such a tool available, although there is one for spring for Java.
This thread on the spring.net forum
discusses the issue and proposes a solution.
I made a quick-and-dirty proof of concept based on Thomas Darimont's approach using QuickGraph.
For the following configuration file:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object id="a1" type="q7446068.ClassA, q7446068" >
<property name="MyOtherA" ref="a2" />
</object>
<object id="a2" type="q7446068.ClassA, q7446068" >
<property name="MyOtherA" ref="a1" />
</object>
<object id="a3" type="q7446068.ClassA, q7446068" />
</objects>
I was able to create the following dot file:
digraph G {
0 [label="a1"];
1 [label="a2"];
2 [label="a3"];
0 -> 1 [];
1 -> 0 [];
}
Which shows the circular dependency.
The code is available as a gist.
I have the Membase server installed with a couple buckets setup and I was looking for a good tutorial or example of how to use this as the 2nd level cache with NHibernate.
I am interested in what a sample configuration would look like and if there is anything I need to do in code or if I can handle it all from my NHibernate mappings.
Thanks for any assistance.
In your mapping files, you will need to include the property:
<class name="ClassName" table="Table">
<cache usage="read-write" />
<!-- SNIP -->
</class>
Options are read-write (read committed isolation), nonstrict-read-write (objects that are rarely written, better performance but increased chance of stale data), or read-only (data that never changes).
Then, in your web (or app) config you need a section to configure memcached:
<configuration>
<configSections>
<!-- SNIP -->
<section name="memcache" type="NHibernate.Caches.MemCache.MemCacheSectionHandler,NHibernate.Caches.MemCache" />
</configSections>
<memcache>
<memcached host="127.0.0.1" port="11211" weight="2" />
</memcache>
<!-- SNIP -->
</configuration>
Finally, in your session factory configuration be sure to use:
<hibernate-configuration>
<session-factory>
<!-- SNIP -->
<property name="expiration">300</property> <!--memcache uses seconds -->
<property name="cache.provider_class">NHibernate.Caches.MemCache.MemCacheProvider,NHibernate.Caches.MemCache</property>
<property name="cache.use_second_level_cache">true</property>
<property name="cache.use_query_cache">false</property> <!-- true if you want to cache query results -->
</session-factory>
</hibernate-configuration>
Of course you will need to download and reference a dll from the appropriate version of NHibernate.Caches to get the right cache provider. The memcached one takes a dependency on ICSharpCode.SharpZipLib and Memcached.ClientLibrary as well (s/b included in the download)
If you're using fluent NHibernate, there is a .Cache method in the setup chain for a session factory that you can use, though some of the properties need to be set manually through a call to .ExposeConfiguration.