Autowire JNDI Resource in Spring

I would like to know how to auto-configure a JNDI resource in a Spring controller using annotation.

Currently I can get the resource using

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

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

+5
source share
2 answers

I use this configuration to insert 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; 
+2
source
 @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; } } 
+6
source

Source: https://habr.com/ru/post/1204207/


All Articles