在Spring中自动装配JNDI资源

时间:2014-10-07 20:33:43

标签: spring spring-mvc jndi

我想知道如何使用注释在Spring控制器中自动装配JNDI资源。

目前我可以使用

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

有什么办法,我可以使用注释做同样的事情吗?就像是 @Resource(name =&#34; my / service&#34;)?

2 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:3)

我使用此配置注入JNDI资源

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>

<强>类

@Autowired
private javax.jms.Destination destination;