geronimo-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "timo.ratilainen" <trati...@yahoo.com>
Subject Re: Injecting datasource with the @Resource annotation
Date Thu, 05 Jun 2008 14:01:52 GMT

Thanks, it helped. For some reason I was thinking that JEE 5 web container
will inject the resource directly from the JNDI reference. Maybe there are
some basics in the Spring that I do not understand. Does Spring 'override'
handling of all common annotations? How?


For those who need to know the solution:

Option 1:

For example, use JndiObjectFactoryBean to create beans from JNDI references
by adding old-fashioned bean definition in the application context:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
          
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
               
	<bean id="sutkiDatasource "
class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName">
            <value>java:comp/env/jdbc/SutkiDB</value>
        </property>
        <property name="lookupOnStartup">
            <value>true</value>
        </property>
	</bean>

	<context:annotation-config/>
	<context:component-scan base-package="fi.company" />     
   
</beans>

or if You don't like xml configuration (I don't) then You may use annotated
version:

Option 2:

Create Java class like:

package fi.company.sutki.util;

import org.springframework.jndi.JndiObjectFactoryBean;
import org.springframework.stereotype.Component;

@Component
public class SutkiDatasource extends JndiObjectFactoryBean {

    private static final String jndiName = "java:comp/env/jdbc/SutkiDB";

    public SutkiDatasource() {
        setJndiName(jndiName);
        setLookupOnStartup(true);
        setCache(true);
    }
}

And the applicationContext.xml stays cleaner:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
          
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 
	<context:annotation-config/>
	<context:component-scan base-package="fi.company" />     
   
</beans>




In both options, the client side is:

@Autowired
private DataSource sutkiDatasource;

Regards,
Timo

PS. Somehow I still don't like this intermediate-datasource-bean-model. Is
it really necessary? Why  the datasource object can not be used directly
from the JNDI reference, or at least, why programmer have to create this
bean, when the framework could do it for You. Most of the cases, programmer
don't need to modify the datasource object. 



Gianny Damour-2 wrote:
> 
> Hello Timo,
> 
> This @Resource annotation will be processed and resolved by Spring 2.5 
> +. You need to define a bean named SutkiDB in your Spring application  
> context by using the standard Spring provided JNDI lookup helpers.
> 
> FWIW, I believe that the @Autowired annotation is way more flexible  
> and powerful than the @Resource one. So, you may want to use  
> @Autowired instead.
> 
> Thanks,
> Gianny
> 

-- 
View this message in context: http://www.nabble.com/Injecting-datasource-with-the-%40Resource-annotation-tp17647494s134p17670568.html
Sent from the Apache Geronimo - Users mailing list archive at Nabble.com.


Mime
View raw message