2014-10-07 82 views
5

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

目前我可以检索使用

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

有什么办法,我可以做注解使用同样的事情的资源?像 @Resource(name =“my/service”)?

+0

目前还不清楚你正在寻找实现的目标。您可以使用@Autowired在Spring上下文中自动装入任何bean。如果你想使用一个标识符,那么你的bean id就是为了这个目的。但是,我不确定为什么要自动装入JNDI工厂bean。 – Angad 2014-10-07 23:55:00

+0

你解决了这个问题吗?如果是的话,你是如何解决它的? – Xstian 2014-10-13 13:51:10

回答

2

我使用此配置注入JNDI资源

Spring配置

<?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; 
+0

为什么downvote没有评论? -.-” – Xstian 2016-12-21 08:11:27

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