Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autowire JNDI Resource in Spring

I would like to know how to autowire JNDI resource in Spring controller using annotation.

Currently I can retrieve the resource using

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="my/service"/>
</bean>

Is there any way, I can do the same thing using annotation? Something like @Resource(name="my/service") ?

like image 380
user3422290 Avatar asked Oct 07 '14 20:10

user3422290


2 Answers

@Configuration
public class Configuration {
    @Bean(destroyMethod = "close")
    public DataSource dataSource() {
        JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
        dsLookup.setResourceRef(false);
        DataSource dataSource = dsLookup.getDataSource("my/service");       
        return dataSource;
    }
}
like image 126
Ivan Rodrigues Avatar answered Oct 23 '22 00:10

Ivan Rodrigues


I use this configuration to inject a JNDI resource

spring config

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/jee 
            http://www.springframework.org/schema/jee/spring-jee.xsd">

    <jee:jndi-lookup id="destination" jndi-name="java:/queue/inbound/jndiname" />

</beans>

Class

@Autowired
private javax.jms.Destination destination;
like image 45
Xstian Avatar answered Oct 22 '22 22:10

Xstian