Autowire JNDI Resource in Spring

こ雲淡風輕ζ 提交于 2019-12-07 06:36:56

问题


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") ?


回答1:


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;



回答2:


@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;
    }
}


来源:https://stackoverflow.com/questions/26244783/autowire-jndi-resource-in-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!