Servlet 3.0 Spring Java配置JNDI

时间:2022-10-26 08:52:24

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

我正在尝试复制web的resource-ref属性。我的spring web apps WebApplicationInitializer中的xml来配置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?

在java配置中而不是在xml中?

1 个解决方案

#1


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.

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

#1


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.

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