12

I am trying to replicate the resource-ref attribute of web.xml in my spring web apps WebApplicationInitializer to configure JNDI.

How would I do this:

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

in java config rather than xml?

higuaro
  • 15,730
  • 4
  • 36
  • 43
ctrlspace
  • 519
  • 5
  • 15

1 Answers1

12

Looking into the spec for servlet 3.0 I found the @Resource annotation. Instead of in my WebApplicationInitializer class it's now in my WebConfig class.

@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;
}
higuaro
  • 15,730
  • 4
  • 36
  • 43
ctrlspace
  • 519
  • 5
  • 15