Spring Boot - JNDI value lookup

匿名 (未验证) 提交于 2019-12-03 09:14:57

问题:

@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();     } 

In the above code is there a way that I can access the test/value from a bean (Just as Datasource Bean Works) ???

I have tried many approaches but nothing seems to work. I am able to access the test/value from a controller using ( new InitialContext().lookup("java:comp/env/test/value") ).

回答1:

There is a way to access test/value... Because you're initialising your embedded Tomcat container within the Spring context, you have to delay the initialisation of your InitialContext (until the env vars have been setup).

To achieve this, I used Spring's @Lazy annotation. e.g.

SampleTomcatJndiApplication.java

import javax.naming.*;  @Bean @Lazy public Context environmentContext() throws NamingException {     Context ctx = new InitialContext();     Context envCtx = (Context) ctx.lookup("java:comp/env");     return envCtx; } 

SomeOtherComponent.java

@Lazy @Autowired private Context environmentContext;  public String getTestValue() throws NamingException {     return environmentContext.lookup("test/value").toString(); } 

Not sure if this violates any "best practices" - perhaps Spring has a better way of retrieving JNDI variables?? Not sure what the implications are if the JNDI var is changed on the server side (whether the value is re-looked-up)?? If anyone knows, please post back here!



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!