Setting properties using Unity XML - unity-container

I want to set the Entity Framework ObjectContext in a repository class - the ObjectContext being a property of the repository.
I am using Unity XML configuration:
<register type="IUsersRepository" mapTo="SqlUsersRepository" >
<property name="MyObjectContext">
<value value="Per-Request" typeConverter="ObjectContextTypeConverter" />
</property>
</register>
The ObjectContextTypeConverter interprets the value of the property - in this case "Per-Request" - and uses an ObjectContext stored in HttpContext.Current.Items (a per request collection in asp.net).
The ObjectContextTypeConverter.ConvertFrom method is only called once when the Unity configurations is loaded in the Application_Start method of the Global.asax file. Yet when I try to resolve an interface using Unity - the ConvertFrom method won't be called again.
Is there a way to solve this problem?

The reason is that you're setting it as a value. You can set it as a dependency:
<property name="MyObjectContext">
<dependency />
</property>
and inject the context through an in injection factory:
Container.RegisterType<ObjectContext>(
new InjectionFactory(c => GetTheContextInstance()));

Related

java.lang.VerifyError exception while authenticating for Google Vision Api

The code for vision api works fine when using a java application however when using spring a java.lang.VerifyError exception is thrown on the following line.
Labels app = new Labels(label.getVisionService());
It was certain that i had specified the json credentials for the system and not the web app so i have included the following bean in my root-context.xml:-
<bean id="googleVisionApiAuthenticator" class="com.something.mypackage.Labels">
<property name="APPLICATION_NAME" value="myCompany-VisionLabelSample/1.0" />
<property name="MAX_LABELS" value="3" />
</bean>
After inclusion of these lines in the root-context its gives page not found.
Well the problem was that some of the jars present were duplicate hence the error. A detailed study of the dependencies and removing the extra jars helped solve the error.

Where can I find the full documentation of deployment descriptor(web.xml)

Is there a full documentaion of the deployment descriptor that describes each element and each sub-element?
I realy can't find it.
P.S. I ask because I found the way to set maxAge of session cookies by adding
<session-config>
<session-timeout>525600</session-timeout>
<cookie-config>
<max-age>31536000</max-age>
</cookie-config>
</session-config>
into DD. But I cannot find any official documentation that describes <cookie-config> element.
For the standard Java EE deployment descriptor elements, that follows the servlet 3.0 specification, you can address, for instance, Oracle's Weblogic 12c web.xml docs.
Furthermore, for the missing sub-elements that aren't contemplated in the documentation mentioned above, I'd suggest you to give a look to the web-common_3_0.xsd file, which is the common XML Schema for the Servlet 3.0 deployment descriptor (...) in turn used by web.xml and web-fragment.xml web application's war file.
Event though it will force you to read XML, in this file you may check all the elements, as well as their sub-elements, that can be used in web.xml deployment descriptor as, for instance, the cookie-config:
<xsd:element name="cookie-config"
type="javaee:cookie-configType"
minOccurs="0">
<xsd:annotation>
<xsd:documentation>
The cookie-config element defines the configuration of the
session tracking cookies created by this web application.
</xsd:documentation>
</xsd:annotation>
</xsd:element>

Jrebel, Spring MVC and TIles. Can't get Jrebel to pick up changes to views.xml

I'm trying to use Jrebel with IntelliJ 10. I'm working on Spring Roo generated project which uses tiles. There are multiple views.xml in my class path.
What I would like to know is if JRebel will pick up changes to views.xml. So far I can't seem to make it.
Just report it to JRebel forum - it will be registered as a feature request
If you are using spring roo generated project than it uses tiles 2.2.1 at least. in that case you dont need Jrebel to do it as Tiles can do it directly, just set the useMutableTilesContainer and checkRefresh properties to true on spring's TilesConfigurer bean
<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"
id="tilesConfigurer" >
<property name="useMutableTilesContainer">true</property>
<property name="checkRefresh">true</property>
it's explained in detail here: http://lalyos.github.com/blog/2012/08/29/tiles-reload-definitions-in-springmvc/

Using component-scan in a web project

I have an Eclipse "Dynamic Web Project" and a Tomcat server configured in Eclipse to use that project. I have a file extension mapped to a servlet, and in the servlet config I have a component-scan element setup like this:
<context:component-scan base-package="com.mycompany.web" />
When my web server starts up, I see this error message in the log:
May 6, 2011 9:50:23 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet cap
java.lang.IllegalArgumentException: Resource path [C:\...\src\main\webapp\WEB-INF\classes\com\mycompany\web] does not denote a directory
That directory does not exist (except in the packaged .war file). However, the com.mycompany.web package does exist.
Is there another way to do a component-scan? It needs to either look in another folder, or somehow find it in my project or something...
It doesn't appear to be possible to do a component-scan in this way. It does a directory listing, which I guess isn't possible when the class files aren't physically located where Spring is expecting. I solved this just by not using a component-scan, and listing the controllers in the spring config like this:
<context:annotation-config />
<bean class="com.mycompany.web.MyController"/>
Annotations in the controller still work, so listed the controllers manually is the only extra step.
it's possible to do what you want, but you need a little extra configuration.
You can annotate your classes with #Controller or #Repository or other Spring stereotype annotations and Spring will pick them up automatically.
Or you can give context:component-scan a bit more info. Example:
<context:component-scan base-package="com.mycompany.web">
<!-- scan all classes in the com.mycompany.web package (but
not in subpackages) -->
<context:include-filter type="aspectj" expression="com.mycompany.web.*"/>
<!-- or if you want to include subpackages this config scans all
classes in com.mycompany.web.special and in its subpackages
(that's what the ..* means) -->
<context:include-filter type="aspectj" expression="com.mycompany.web.special..*"/>
</context:component-scan>
Finally, if you don't like using aspectj, context:include-filter's type can be a regular expression, custom annotation, etc. See this link for more info.

Multiple database with NHibernate

I have two databases. One from Oracle 10g. Another from Mysql. I have configured my web application with Nhibernate for Oracle and now I am in need of using the MySQL database. So how can i configure the hibernate.cfg.xml so that i can use both of the database at the same application?
My current hibernate.cfg.xml is:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
<property name="connection.connection_string">Data Source=xe;Persist Security Info=True;User ID=hr;Password=hr;Unicode=True</property>
<property name="show_sql">false</property>
<property name="dialect">NHibernate.Dialect.Oracle9Dialect</property>
<!-- mapping files -->
<mapping assembly="DataTransfer" />
</session-factory>
</hibernate-configuration>
You can check "Burrow" addin for nhibernate
http://nhforge.org/wikis/burrow/introduction.aspx
Multiple databases support. multiple databases becomes easy, you don't need a SessionFactoryPath, you simply give Burrow an entity type, and Burrow will find the right ISession for you. If you are using GenericDAO, the multiple DB support can be very transparent - your code does not need to know there are multiple databases.
Seems that you could also use unhaddins, haven't used it and I didn't find the documentation.
http://code.google.com/p/unhaddins/
I had to do that many time ago I followed this article, it's a more complex solution but I paste it just in case.
http://www.codeproject.com/KB/aspnet/NHibernateMultipleDBs.aspx
Here may be a simpler solution. It provides a new feature that allows the hibernate-configuration to have multiple session-factory elements by giving them names, and extends the NHibernate.Cfg.Configuration.Configure to accept a sessionFactoryName.
Configure NHibernate hibernate.cfg.xml file to have more connection strings

Resources