【发布时间】:2016-04-16 12:51:52
【问题描述】:
@SpringBootApplication
public class SampleTomcatJndiApplication {
public static void main(String[] args) {
SpringApplication.run(SampleTomcatJndiApplication.class, args);
}
@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
return new TomcatEmbeddedServletContainerFactory() {
@Override
protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
Tomcat tomcat) {
tomcat.enableNaming();
return super.getTomcatEmbeddedServletContainer(tomcat);
}
@Override
protected void postProcessContext(Context context) {
ContextResource resource = new ContextResource();
resource.setName("jdbc/myDataSource");
resource.setType(DataSource.class.getName());
resource.setProperty("driverClassName", "your.db.Driver");
resource.setProperty("url", "jdbc:yourDb");
context.getNamingResources().addResource(resource);
ContextEnvironment contextEnv = new ContextEnvironment();
contextEnv.setName("test/value");
contextEnv.setType("java.lang.String");
contextEnv.setOverride(false);
contextEnv.setValue("testing");
context.getNamingResources().addEnvironment(contextEnv);
}
};
}
@Bean(destroyMethod="")
public DataSource jndiDataSource() throws IllegalArgumentException, NamingException {
JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
bean.setJndiName("java:comp/env/jdbc/myDataSource");
bean.setProxyInterface(DataSource.class);
bean.setLookupOnStartup(false);
bean.afterPropertiesSet();
return (DataSource)bean.getObject();
}
在上面的代码中,有没有一种方法可以让我从 bean 访问 test/value(就像 Datasource Bean 一样)???
我尝试了很多方法,但似乎没有任何效果。我可以使用(new InitialContext().lookup("java:comp/env/test/value"))从控制器访问测试/值。
【问题讨论】:
-
为什么还要使用 JNDI。它添加了什么,您使用的是嵌入式容器,因此您可以通过将数据源声明为普通 bean 而不是 JNDI 来实现相同的目的。恕我直言,您正在使事情变得比需要的更复杂。
标签: java spring spring-boot jndi lookup