Glassfish 带Spring引导的嵌入式Tomcat JNDI映射

Glassfish 带Spring引导的嵌入式Tomcat JNDI映射,glassfish,spring-boot,jndi,embedded-tomcat-8,Glassfish,Spring Boot,Jndi,Embedded Tomcat 8,我们正在使用Glassfish,在这里我们设置了Map类型的JNDI资源,我们定义了一些Bean工厂,之后我们可以在代码中访问JNDI查找这个映射 我想用Spring Boot对嵌入式Tomcat进行同样的测试,但我不知道如何进行。在任何地方,他们只是参考如何添加JNDI数据源,而不是一些Hashmap。我试过这样的方法,但我猜是完全错误的 public TomcatEmbeddedServletContainerFactory tomcatFactory() { return new

我们正在使用Glassfish,在这里我们设置了Map类型的JNDI资源,我们定义了一些Bean工厂,之后我们可以在代码中访问JNDI查找这个映射

我想用Spring Boot对嵌入式Tomcat进行同样的测试,但我不知道如何进行。在任何地方,他们只是参考如何添加JNDI数据源,而不是一些Hashmap。我试过这样的方法,但我猜是完全错误的

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("jndiname");
                resource.setType(Map.class.getName());
                // for testing only
                resource.setProperty("testproperty", "10");

                context.getNamingResources().addResource(resource);
            }
        };
    }


    @Bean(destroyMethod="")
    public Map jndiDataSource() throws IllegalArgumentException, NamingException {
        JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
        bean.setJndiName("jndiname");
        bean.setProxyInterface(Map.class);
        bean.setLookupOnStartup(false);
        bean.setResourceRef(true);
        bean.afterPropertiesSet();
        return (Map)bean.getObject();
    }

很抱歉,我不知道在目标工厂该去哪里。使用嵌入式Tomcat是否可能?

首先要做的是创建一个可以返回映射的ObjectFactory实现:


不幸的是,我仍然得到的名称[myName]在此上下文中没有绑定。找不到[myName]。也许它与我使用的嵌入式TomcatI found有某种联系,但是黑客对meI不起作用,我认为问题是我在@Configuration中进行了查找,但即使我删除了它,不再使用那里的查找,它在运行时仍然会失败,同样的错误:如果您使用完全相同的名称绑定资源并查找它,那么它将不起作用。资源会自动绑定到java:comp/env/so resource.setNamefoo/myMap中,从JNDI检索时需要使用java:comp/env/foo/myMap。它是特定于Tomcat的吗?我们称之为'Context ic=newinitialcontext;obj=ic.lookupCONFIG_JNDI;`而且它对玻璃鱼很有效。也许Glassfish也会将JNDI名称映射到root,或者其他什么东西会试图在文档中找到一些东西。
public class MapObjectFactory implements ObjectFactory {

    @Override
    public Object getObjectInstance(Object obj, Name name,
            javax.naming.Context nameCtx, Hashtable<?, ?> environment)
            throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        // Configure the map as appropriate
        return map;
    }   
}
@Override
protected void postProcessContext(Context context) {
    ContextResource resource = new ContextResource();
    resource.setName("foo/myMap");
    resource.setType(Map.class.getName());
    resource.setProperty("factory", MapObjectFactory.class.getName());
    context.getNamingResources().addResource(resource);
}