classpath reosource as value spring DSL - spring

I want to have some alternative to this using Spring DSL in Grails.
<bean name="someName" class="SomeClass">
<property name="resource" value="classpath*:Queues.xml"></property>
</bean>

If you really want to, you can use resources.xml instead of resources.groovy for a more conventionally Spring-y style.

Related

Working of Resource Injection #Resource and Spring Framework?

I want to understand how the annotation works. I have this piece of code which I have used in a simple Spring project.
#Resource(name="dataSource")
private DataSource dataSouce;
The dataSource I have defined in an XML config file:
<!-- The Apache DBCP implementation of DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:tcp://localhost/~/test"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
Other snippet from spring xml config file
<context:annotation-config /> <!-- This enables the annotation's actions, else annotations don't do their work. -->
<context:component-scan base-package="com.example.dao.jdbc.impl"/> <!-- This is for component scan -->
<bean id="jdbcOperImpl" class="com.example.dao.jdbc.impl.JdbcOperImpl"/>
As I understand the Resource annotations comes from javax.annotation.Resource. I looked its source code, and I notices the annotation is defined by JDK SE, and it is just a simple definition of an annotation. How does this do the injection? Does Spring Framework use this annotation and does Injection? How is #Resource annotation and Spring framework related?
Actually Spring supports two types of annotations which are Spring based annotations and Java based annotations. For dependencies you can use #Autowired that completely spring annotations.
#Resource and #Inject are the standard java based annotations. For more clarity see this link http://blogs.sourceallies.com/2011/08/spring-injection-with-resource-and-autowired/

How to configure flushMode property of OpenSessionInViewInterceptor of spring 3.1.4

As I am planning to update from "hibernate3" to "hibernate4" & "spring 3.0.5" to "spring 3.1.4".
I have configured OpenSessionInViewInterceptor in spring 3.0.5 so want to configure same in 3.1.4.
But I am not able to configure flushMode in OpenSessionInViewInterceptor of Spring 3.1.4;
My Previous setting for spring 3.0.5 was:
<bean name="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
<property name="flushMode">
<bean
id="org.springframework.orm.hibernate3.HibernateAccessor.FLUSH_NEVER"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />
</property>
</bean>
Now tried to configure same for spring 3.1.4 as below:
<bean name="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
<property name="flushMode">
<bean
id="org.springframework.orm.hibernate3.HibernateAccessor.FLUSH_NEVER"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />
</property>
</bean>
then it throws below exception:
org.springframework.beans.NotWritablePropertyException: Invalid property 'flushMode' of bean class [org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor]: Bean property 'flushMode' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
And there is no similar class found in alternate to org.springframework.orm.hibernate3.HibernateAccessor in spring 3.1.4
So my question is how to set flushMode property of OpenSessionInViewInterceptor of spring 3.1.4 ?
It looks like a mess, with unbound links to property accessors. I'd guess that a copy-paste job was done without much thinking about cleaning things up given the different inheritance hierarchies. I hate it when that happens…
Can you use the Hibernate 3 version instead? Yes, it really does appear to be there; here's the link: org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor
Longer term, look more carefully whether the Hibernate 4 code does what you want without specifying the flag at all. Unfortunately, you'll have to ignore the documentation (at least for now) and study the source itself.

Reference grailsApplication in resources.xml or other xml

Is there any way to reference grailsApplication from either resources.xml or a different xml file that is imported by resources.groovy?
For example, I am wiring up some beans in foo.xml that will be imported in resources.groovy. I would like to pull in some values from the external config, so it would be nice if I could do something like:
<bean id=fooBean class="com.beans.foo.FooBean">
<property name="exVar" value="${grailsApplication.config.some.path.to.ext.variable}"
</bean>
I've done this quite a bit in resources.groovy, so was wondering if there were a way to get at this object from xml. Thanks!
Looking at this related question you can use:
<bean id=fooBean class="com.beans.foo.FooBean">
<property name="exVar" value="${some.path.to.ext.variable}"
</bean>

how Spring MVC find its handler mapping

in Spring MVC we can configure handler mapping as bean.but how spring examine what is the handler mapping we mentioned in xml?
simpliy
<bean id="simplehandler" class="" />
do we need to specify "simplehandler" bean id to somewhere for spring to identify bean handler?
First thing that must be clear is that: Spring has several handler mappings.
And the "DefaulAnnotationHandlerMapping" is activated by default (See DispatcherServlet.properties in the Spring distribution or just google for it. All default handlers are listed there). Spring will choose "DefaulAnnotationHandlerMapping" by default.
If you want Spring to use another handler mapping strategy, you have to tell him explicitly
e.g.:
<bean class="org.blablabla......ControllerClassNameHandlerMapping" />
Note that this cancels the use of the default handler mapping strategy
You can also tell Spring to use several handler mapping strategy and prioritized them by using the order property in the mappers declaration
something like
<bean class="org.blabla....DefaulAnnotationHandlerMapping" >
<property name="order" value="0"/>
</bean>
<bean class="org.blablabla......ControllerClassNameHandlerMapping">
<property name="order" value="1"/>
</bean>
Hope this helps. And sorry if the syntax of my bean declaration is not 100% correct. I had to write quickly ;-)

Spring El Expression

I'm switching from faces-config to Spring and wanted to know how you can pass a property from one bean to another:
e.g.
<bean id="myBean" class="Bean1">
</bean>
<bean id="myBean2" class="Bean2">
<constructor-arg ref="#{myBean1.value}"/>
</bean>
Upgraded to Spring 3.0 which has spring el support
First things first, the purpose of the D.I container is to fully initialize your system prior to execution; that is, all dependencies being set, the app is ready to run.
There are both #property and #value annotations in Spring for similar purposes, but since you want to use and specific bean property value for other bean the best solution would be:
<bean id="myBean" class="Bean1">
</bean>
<bean id="myBean2" class="Bean2">
<constructor-arg ref="myBean"/>
</bean>
If you argue that you just want to set the value at instantiation time, and not establish a dependency, then skip the D.I part and set the value directly.

Resources