我正在尝试在我的 Spring Web 应用程序 WebApplicationInitializer
中复制 web.xml
的 resource-ref
属性来配置 JNDI。
我该怎么做:
<resource-ref>
<description>Connection Pool</description>
<res-ref-name>jdbc/LocalCheddar</res-ref-name>
<res-type>javax.sql.Datasource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
在 java config 中而不是在 xml 中?
最佳答案
查看 servlet 3.0 的规范,我发现了 @Resource
注释。而不是在我的 WebApplicationInitializer
类中,它现在在我的 WebConfig
类中。
@Bean
@Resource(name="jdbc/MyDB")
public DataSource dataSourceLookup() {
final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
dsLookup.setResourceRef(true);
DataSource dataSource = dsLookup.getDataSource("java:comp/env/jdbc/MyDB");
return dataSource;
}
关于java - Servlet 3.0 Spring Java 配置 JNDI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16728677/