Spring Boot - JNDI值查找 [英] Spring Boot - JNDI value lookup

查看:254
本文介绍了Spring Boot - JNDI值查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@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的测试/值(就像数据源Bean一样)???

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

我尝试过很多方法,但似乎没什么用。我可以使用( new InitialContext()。lookup(java:comp / env / test / value))从控制器访问测试/值。

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") ).

推荐答案

有办法访问 test / value ...因为您在Spring上下文中初始化了嵌入式Tomcat容器,所以必须延迟初始化 InitialContext (直到设置了env变量)。

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

为实现这一目标,我使用了Spring的 @Lazy 注释。例如

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

不确定这是否违反任何最佳做法 - 也许Spring有更好的做法检索JNDI变量的方法??如果在服务器端更改了JNDI var(无论该值是否重新查找),不确定含义是什么?如果有人知道,请回到这里!

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!

这篇关于Spring Boot - JNDI值查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆