Spring boot 2 - 无法在嵌入式 tomcat 容器中注册和使用 JNDI

sah*_*has 5 spring hibernate spring-mvc tomcat8 spring-boot

我正在使用 spring boot 2.0.0.RELEASE 并尝试在嵌入式 tomcat 容器中注册 JNDI。下面是我的 Spring Boot 入门课程-

import org.apache.catalina.Context;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.descriptor.web.ContextResource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class MySpringBootApplication {

public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}

@Bean
public ServletWebServerFactory tomcatFactory() {
return new TomcatServletWebServerFactory() {

@Override
protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
tomcat.enableNaming();
return new TomcatWebServer(tomcat, getPort() >= 0);
}

@Override
protected void postProcessContext(Context context) {
ContextResource resource = new ContextResource();
resource.setName("java:comp/env/jdbc/OracleDSNonXA");
resource.setType(DataSource.class.getName());
resource.setProperty("driverClassName", "oracle.jdbc.driver.OracleDriver");
resource.setProperty("url", "DBURL");
resource.setProperty("username", "DBUSER");
resource.setProperty("password", "PASSWORD");
context.getNamingResources().addResource(resource);
}
};
}
}
Run Code Online (Sandbox Code Playgroud)

我的spring配置类如下-

@Configuration
@PropertySource("/hibernate-settings.properties")
@EnableTransactionManagement
public class ApplicationConfig {

/*Logic to load values from property file here*/

@Bean
public JpaVendorAdapter hibernateJpaVendorAdapter() {
return new HibernateJpaVendorAdapter();
}

@Bean
public LocalContainerEntityManagerFactoryBean myentityManagerFactory(DataSource mydataSource) {
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(mydataSource);
emf.setJpaVendorAdapter(hibernateJpaVendorAdapter());
emf.setPersistenceUnitName("mydbunit");

Properties jpaProperties= new Properties();
jpaProperties.setProperty("hibernate.dialect", dialectValue);
jpaProperties.setProperty("hibernate.transaction.manager_lookup_class", trxManagerLookupClass);
jpaProperties.setProperty("hibernate.generate_statistics", generateStatistics);
jpaProperties.setProperty("hibernate.show_sql", showSql);
jpaProperties.setProperty("hibernate.format_sql", formatSql);
emf.setJpaProperties(jpaProperties);
emf.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
return emf;
}



@Bean(destroyMethod = "")
    public DataSource jndiDataSource() throws IllegalArgumentException,
            NamingException {
        JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
        bean.setJndiName("java:comp/env/jdbc/OracleDSNonXA");
        bean.setProxyInterface(DataSource.class);
        bean.setLookupOnStartup(false);
        bean.afterPropertiesSet();
        return (DataSource) bean.getObject();
    }


@Bean
public BeanPostProcessor persistenceAnnotationBeanPostProcessor() {
return new PersistenceAnnotationBeanPostProcessor();
}

}
Run Code Online (Sandbox Code Playgroud)

我主要得到异常 - 引起:org.springframework.jndi.JndiLookupFailureException: JndiObjectTargetSource failed to get new target object; 嵌套异常是 javax.naming.NameNotFoundException: Name [jdbc/OracleDSNonXA] is not bound in this Context。找不到 [jdbc]

下面是详细的异常跟踪。

   2018-03-16 13:22:59.450  WARN 4892 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myentityManagerFactory' defined in class path resource [com/alj/mypage/config/ApplicationConfig.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: mydbunit] Unable to build Hibernate SessionFactory
    2018-03-16 13:22:59.452  INFO 4892 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
    2018-03-16 13:22:59.468  INFO 4892 --- [           main] ConditionEvaluationReportLoggingListener : 

    Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
    2018-03-16 13:22:59.474 ERROR 4892 --- [           main] o.s.boot.SpringApplication               : Application run failed

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myentityManagerFactory' defined in class path resource [com/alj/mypage/config/ApplicationConfig.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: mydbunit] Unable to build Hibernate SessionFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1710) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1085) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:858) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at com.alj.mypage.config.MySpringBootApplication.main(MySpringBootApplication.java:19) [classes/:na]
    Caused by: javax.persistence.PersistenceException: [PersistenceUnit: mydbunit] Unable to build Hibernate SessionFactory
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:970) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
Run Code Online (Sandbox Code Playgroud)

我对 Spring boot 很陌生,我不确定我错过了什么配置。