使用Java EE 6和Glassfish 3.1使用@Resource注入资源 [英] Trouble injecting resources with Java EE 6 and Glassfish 3.1 using @Resource

查看:114
本文介绍了使用Java EE 6和Glassfish 3.1使用@Resource注入资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用Java EE 6和Glassfish 3.1(嵌入式)使用@Resource注释。我想查找一个JNDI数据源,所以我试图让它使用Glassfish中的默认数据源。在我的代码中我有:

  @Resource(lookup =java:global / env / jdbc / __ default)
DataSource dataSource;

它编译得很好。我正在使用Maven +的建议 here



但是,当我部署我的.war时,我总是收到以下错误:

 code>警告:@Resource注释类定义不正确 - 缺少查找属性
符号:FIELD
location:javax.sql.DataSource ResourceLookup.dataSource
/ pre>

这是一个简单的例子,如果它是一个错误,我会非常惊讶。我一定要做错事任何想法?

解决方案

作为测试,你可以尝试jdbc / __ default作为查找字符串吗?对于我的数据源,我使用我指定的jndi名称查找它们,总是使用模式jdbc / MyDataSourceName。



@Resource(lookup =jdbc / MyDataSourceName ) 效果很好。从来没有尝试查找默认ds,特别是不通过java:全局命名空间。


I'm having trouble trying to use the @Resource annotation with Java EE 6 and Glassfish 3.1 (embedded). I want to look up a JNDI datasource, so I'm trying to get it working with the default datasource in Glassfish. In my code I have:

@Resource(lookup = "java:global/env/jdbc/__default")
DataSource dataSource;

It compiles fine. I'm using Maven + the advice here.

However, when I deploy my .war I always get the following error:

WARNING: Incorrect @Resource annotation class definition - missing lookup attribute
  symbol: FIELD
  location: javax.sql.DataSource ResourceLookup.dataSource

It's such a simple example that I'd be extremely surprised if it were a bug. I must be doing something wrong. Any ideas?

解决方案

As a test, can you try "jdbc/__default" as the lookup string? For my datasources I look them up using a jndi name I have specified, always with the pattern "jdbc/MyDataSourceName".

@Resource(lookup="jdbc/MyDataSourceName") works well. Never tried to look up the default ds, particularly not through the java:global namespace.

这篇关于使用Java EE 6和Glassfish 3.1使用@Resource注入资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆